【问题标题】:Listview not showing on orientation Change列表视图未显示方向更改
【发布时间】:2013-11-11 03:06:25
【问题描述】:

我之前做过一个列表视图,无论是横向还是纵向都显示列表。但是,在我的新应用程序中,当我决定改变方向时,无论是横向还是纵向,它都不会再次加载。我正在尝试使用 sqlite 数据库中的项目填充列表视图。这是我的代码:

Attractions.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);

我设置的消息没有出现在 logcat 上。请帮忙。 :(

【问题讨论】:

  • 你在 Manifest 中提到过 android:configChanges="orientation" 吗??

标签: android listview android-listview


【解决方案1】:

打开您的 manifest.xml 文件并更改 Activity 标签如下:然后检查

&lt;activity android:configChanges="orientation">

这将避免重新创建 Activity。

【讨论】:

  • 它仍然应该工作,当重新创建活动时,只要代码是正确的。 db.checkContact() - 这似乎是罪魁祸首。在纵向它返回 0,以便添加联系人,在横向它返回非零,不添加联系人,因此您的最后一个循环永远不会触发。因为我不知道 checkContact 是做什么的,所以我不能更具体。
【解决方案2】:

我大概知道,它是什么。此代码用于添加数据库元素:

 if(ctr == 0){
        Log.d("Nothing Loaded", "Loading Now");
        db.addContact(new Contact(landmarkDetails[loaded], imageInByte));}

第一次,你的数据库是干净的。 checkContact 结果为 0,条件 (ctr==0) 满足,因此 addContact 被执行。

第二次,你的数据库在之前的运行中是“脏的”(注意,在检查联系人之后调用 db.deleteAll)。由于 DB 不干净,ctr != 0,上面的代码永远不会执行。因此 db.getAllContacts() 返回空数据,并且最后一个循环永远不会触发。

由于 deleteAll 等操作,您的数据库第三次再次清理干净!

一个漂亮可爱的错误,想不出更优雅的东西。 :)

如果您阻止活动重新激活,错误就会消失,除非外观取决于方向(例如纵向和横向的多个布局),否则应该没问题。

【讨论】:

  • 感谢兄弟指出。我昨天在调试时忘记删除deleteAll。也许我应该暂停一下。 :)
猜你喜欢
  • 1970-01-01
  • 2013-08-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多