2012-01-05 144 views
-3

我有一個用於列表視圖的適配器,每當它到達.add行時,它都會崩潰,並且我已經將它設置爲一個arrat適配器和一個文本視圖以及所有內容,爲什麼它會崩潰?爲什麼adapter.add崩潰?

+0

這是什麼有崩潰? – fge 2012-01-05 20:49:05

+6

您需要發佈一些代碼。 – EboMike 2012-01-05 20:51:46

+0

並詳細說明你的意思是「崩潰」,它是否會拋出任何異常? – Guillaume 2012-01-05 22:13:35

回答

2

沒有給你的代碼很難檢查什麼是錯的。

以下鏈接爲您提供了有關設置列表視圖http://developer.android.com/resources/tutorials/views/hello-listview.html的教程,但它實現了ListActivity。

如果你只實現你的代碼看起來應該

package my.test; 

import java.util.ArrayList; 
import android.app.Activity; 
import android.os.Bundle; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 

public class MyActivity extends Activity { 
    ListView lvList; // the listview 
    ArrayAdapter<String> adapter; // the adapter responsible for the rendering 
    ArrayList<String> listItems = new ArrayList<String>(); // the list of items 
    String myString = "Text to add"; // the text to add 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     lvList = (ListView) findViewById(R.id.mylistview); 
     adapter = new ArrayAdapter<String>(this, 
       android.R.layout.simple_list_item_1, listItems); 
     lvList.setAdapter(adapter); 

      //add the text to the list of items 
     listItems.add(myString); 

      //tell the adapter that the list have change and the 
      //rendering should be refreshed 
     adapter.notifyDataSetChanged(); 
    } 
} 

活動其實我給的例子是字符串,但你可以爲整數或創建任何類做到這一點。

2

我有類似像你這樣,負載更多的數據與舊設備與舊Android版本的問題......我就是第一個加入

objAdapter.addAll(loadMoreArray);

和呼叫

objAdapter.notifyDataSetChanged();

但這並不是很好的解決方案,如果您在objectAdapter中添加新的數據列表,則會出現較舊的設備崩潰應用。但解決方案是將新數據加載到新列表中,並在舊列表中添加新數據列表

arrayListOfData.addAll(newArrayListOfData);

和呼叫

objAdapter.notifyDataSetChanged();

希望我幫助......