我有一個用於列表視圖的適配器,每當它到達.add行時,它都會崩潰,並且我已經將它設置爲一個arrat適配器和一個文本視圖以及所有內容,爲什麼它會崩潰?爲什麼adapter.add崩潰?
-3
A
回答
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();
希望我幫助......
相關問題
- 1. 爲什麼adapter.add()創建一個無限循環並崩潰?
- 2. adapter.add()崩潰應用程序
- 3. android adapter.add()崩潰我的應用程序
- 4. 爲什麼RSA_size會崩潰?
- 5. 爲什麼代碼崩潰?
- 6. 爲什麼SDL_DisplayFormat崩潰?
- 7. 爲什麼程序崩潰?
- 8. enable_shared_from_this。爲什麼會崩潰?
- 9. 爲什麼rightBarButtonItem崩潰?
- 10. execv爲什麼會崩潰?
- 11. 爲什麼OpenSSL崩潰?
- 12. 爲什麼Ext.grid.Panel會崩潰?
- 13. 爲什麼程序崩潰?
- 14. 爲什麼nspredicate會崩潰?
- 15. SurfaceView崩潰 - 爲什麼?
- 16. PyObject_Print爲什麼會崩潰?
- 17. 爲什麼std string ==崩潰?
- 18. Android:爲什麼BroadcastReceiver崩潰?
- 19. 爲什麼我的程序崩潰末
- 20. NSMutableArray,removeFromArray和release,爲什麼會崩潰?
- 21. 爲什麼sublimetext2崩潰在Windows 7
- 22. 爲什麼這個HTML崩潰IE?
- 23. Android爲什麼RecyclerView適配器崩潰?
- 24. 爲什麼我的程序崩潰(java)?
- 25. 爲什麼UIPickerView didSelectRow方法崩潰?
- 26. 爲什麼我的代碼崩潰?
- 27. Java eclipse,爲什麼JVM崩潰?
- 28. 這是爲什麼會崩潰? - C
- 29. 爲什麼NSMutableURLRequest實例導致崩潰?
- 30. 爲什麼get_category_by_slug()崩潰WordPress 4.8
這是什麼有崩潰? – fge 2012-01-05 20:49:05
您需要發佈一些代碼。 – EboMike 2012-01-05 20:51:46
並詳細說明你的意思是「崩潰」,它是否會拋出任何異常? – Guillaume 2012-01-05 22:13:35