ListView没有更新

时间:2012-11-01 09:29:43

标签: android android-asynctask

我是android新手。任何人都可以告诉我,我做错了什么。我的listView不会更新! 我想要一个带有图像的列表视图。 我将解释这里的流程 我有一个ImageList.java作为我的主要类,而LazyLoader.java扩展了BaseAdapter。 这里从存储在我的localhost上的xml文件中检索图像。 这是有用的, 我使用10.0.2.2的localhost没有问题,我在LogCat中获取了我的x​​ml。 一切都在logcat中没有错误但视图没有更新。 我想我的思考膨胀方式和工作方式存在一些问题。所以开始的一个好处是看LazyLoader.java 如果代码中的内容不清楚,请告诉我我将添加更多解释。

我已按照本教程对asynctask进行了更改 http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/

ImageList.java 这是我的onCreate

@Override
protected void onCreate(Bundle savedInstanceState) {
 try{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
new loadSite().execute();
}
catch(Exception exp){
    Log.e("error", "ERROR RAISED HERE "+exp);
    exp.printStackTrace();
     }
}

Async就在这里

class loadSite extends AsyncTask<String, String, String>{



        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        pDialog = new ProgressDialog(ImageList.this);
            pDialog.setMessage("Loading websites ...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }

        @Override
        protected String doInBackground(String... args) {
            try{
                ArrayList<HashMap<String,String>> articlesList = new ArrayList<HashMap<String,String>>();
                XMLParser parser = new XMLParser();

                String xml = parser.getXmlFromUrl(URL);

                Document doc = parser.getDomElement(xml);

                NodeList nl = doc.getElementsByTagName(KEY_SONG);

                for(int i=0;i<nl.getLength();i++){
                     HashMap<String,String> map = new HashMap<String, String>();
                     Element elm = (Element) nl.item(i);
                     map.put(KEY_TITLE, parser.getValue(elm, KEY_TITLE));
                     map.put(KEY_THUMB_URL, parser.getValue(elm, KEY_THUMB_URL));
                     Log.e("URL",parser.getValue(elm, KEY_THUMB_URL));
                     articlesList.add(map);

                }
                list = (ListView)findViewById(R.id.list);
                //ListAdapter adapter = new SimpleAdapter(ImageList.this,articlesList, R.layout.list_row, new String[] {"list_image"},new int[]{R.id.list_image});
                adapter = new LazyAdapter(ImageList.this,articlesList);
                list.setAdapter(adapter);
                }
            catch(Exception exp){
                Log.e("Error", "EXCEPTION RAISED IN ASYNC "+exp);
                exp.printStackTrace();
            }

    return null;
        }
    @Override
    protected void onPostExecute(String result) {
         pDialog.dismiss();
         adapter.notifyDataSetChanged();
         Log.e("Notify","Dataset change notified");
    }
}

LazyAdapter类就在这里。它扩展了BaseAdapter 的 LazyAdapter.java

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View vi = convertView;
    if(convertView==null){
        vi=inflater.inflate(R.layout.list_row,null);

    }
    ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image);
    TextView title = (TextView)vi.findViewById(R.id.title);
    HashMap<String,String> article = new HashMap<String, String>();
    article = data.get(position);
    title.setText(article.get(ImageList.KEY_TITLE));
    imageLoader.DisplayImage(article.get(ImageList.KEY_THUMB_URL), thumb_image);
    return vi;

}

XML,如果有人需要看到的话。

main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
 <include layout="@layout/header"/>
<ListView
    android:id="@+id/list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:divider="#b5b5b5"
    android:dividerHeight="1dp"
    />
</LinearLayout>

list_row.xml

<?xml version="1.0" encoding="utf-8"?>
  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="5dip" >
    <LinearLayout android:id="@+id/thumbnail"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:padding="3dip"
       android:layout_alignParentLeft="true"
       android:layout_marginRight="5dip">

    <ImageView
        android:id="@+id/list_image"
        android:layout_width="fill_parent"
        android:layout_height="50dip"
        android:scaleType="centerCrop"  
        android:src="@drawable/nature"/>

</LinearLayout>
 <TextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Rihanna Love the way lie"
    android:textColor="#040404"
    android:typeface="sans"
    android:textSize="15dip"
    android:textStyle="bold"/>
</RelativeLayout>

1 个答案:

答案 0 :(得分:3)

您不应该初始化LazyAdapter并将其设置为 doInBackground 方法中的ListView,因为操作视图必须在UI线程和 doInBackground上完成在一个单独的线程上运行。

相反,我建议您将<{1}}(适配器的数据集)从 doInBackground 返回到 onPostExecute 方法并初始化适配器并将其设置为您的列表因为 onPostExecute 总是在UI线程上运行 - 所以允许你操作视图。