[英]Listview not showing on orientation Change
我以前做過一個listview,並且無論是以橫向還是縱向顯示列表。 但是,在我的新應用中,當我決定更改方向時,無論是橫向還是縱向,都不會再次加載。 我正在嘗試使用來自sqlite數據庫的項目填充listview。 這是我的代碼:
景點.java
public class Attractions extends ListActivity {
DataBaseHandler db = new DataBaseHandler(this);
ArrayList<Contact> imageArry = new ArrayList<Contact>();
ContactImageAdapter adapter;
int ctr, loaded;
int [] landmarkImages={R.drawable.oblation,R.drawable.eastwood,R.drawable.ecopark,R.drawable.circle};
String []landmarkDetails = { "Oblation", "Eastwood", "Ecopark", "QC Circle"};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_attractions);
ctr = db.checkContact(landmarkDetails[loaded]);
db.deleteAll();
// get image from drawable
/**
* CRUD Operations
* */
// Inserting Contacts
Log.d("Insert: ", "Inserting ..");
for(loaded=0; loaded <landmarkDetails.length;loaded++){
Bitmap image = BitmapFactory.decodeResource(getResources(),
landmarkImages[loaded]);
// convert bitmap to byte
ByteArrayOutputStream stream = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte imageInByte[] = stream.toByteArray();
Log.d("Going to load images", "Image "+ loaded);
Log.d("Goind to load objects", "loading");
if(ctr == 0){
Log.d("Nothing Loaded", "Loading Now");
db.addContact(new Contact(landmarkDetails[loaded], imageInByte));}
Log.d(landmarkDetails[loaded], "Loaded!");
image.recycle();
}
// Reading all contacts from database
List<Contact> contacts = db.getAllContacts();
for (Contact cn : contacts) {
String log = "ID:" + cn.getID() + " Name: " + cn.getName()
+ " ,Image: " + cn.getImage();
// Writing Contacts to log
Log.d("Result: ", log);
//add contacts data in arrayList
imageArry.add(cn);
}
adapter = new ContactImageAdapter(this, R.layout.screen_list,
imageArry);
ListView dataList = (ListView) findViewById(android.R.id.list);
dataList.setAdapter(adapter);
}
更新
我在縱向模式下運行該應用程序:切換為橫向模式時顯示列表:不顯示任何內容。 我再次切換到肖像:列表再次出現。 感謝您的幫助!
更新2
當我改變方向時,這些行不會被執行:
List<Contact> contacts = db.getAllContacts();
for (Contact cn : contacts) {
String log = "ID:" + cn.getID() + " Name: " + cn.getName()
+ " ,Image: " + cn.getImage();
// Writing Contacts to log
Log.d("Result: ", log);
//add contacts data in arrayList
imageArry.add(cn);
}
adapter = new ContactImageAdapter(this, R.layout.screen_list,
imageArry);
ListView dataList = (ListView) findViewById(android.R.id.list);
dataList.setAdapter(adapter);
我設置的消息不會顯示在日志貓上。 請幫忙。 :(
打開您的manifest.xml文件,並如下更改Activity <>標記:然后檢查
<
活動android:configChanges =“ orientation”>
這樣可以避免重新創建活動。
我大概知道,那是什么。 此代碼用於添加數據庫元素:
if(ctr == 0){
Log.d("Nothing Loaded", "Loading Now");
db.addContact(new Contact(landmarkDetails[loaded], imageInByte));}
第一次,您的DB是干凈的。 checkContact結果為0,條件(ctr == 0)滿足,因此執行addContact。
第二次,您的數據庫與上一次運行“不友好”(請注意,在檢查聯系人之后會調用db.deleteAll)。 由於DB不干凈,因此ctr!= 0,因此永遠不會執行以上代碼。 因此,db.getAllContacts()返回空數據,並且永遠不會觸發最后一個循環。
由於deleteAll等原因,您的數據庫第三次再次干凈!
一個可愛的錯誤,無法想到更優雅的東西。 :)
如果您阻止重新激活活動,則錯誤將消失,並且,除非外觀取決於方向(例如,縱向和橫向的幾種布局),否則應該沒問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.