我是新来的android。谁能告诉我,我做错了什么?我的listView不会更新! 我想要一个带有图像的列表视图。 我将在这里解释流程 我有一个ImageList.java
作为我的主类,而LazyLoader.java
它扩展了BaseAdapter。 这里的图像是从存储在我的本地主机上的xml文件中检索的。 这是什么工作, 本地主机我用10.0.2.2没有问题,我得到我的XML在LogCat中。 在logcat中一切正常,但视图不会更新。 我想我在考虑膨胀的方式和它的工作方式有一些问题。所以一个好的开始就是看看LazyLoader.java
如果事情不明确,请告诉我,我会补充更多的解释。为asynctask
http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/ListView不更新
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();
}
}
而异步是这里
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>
如果我runonuithread不会得到一个错误?我之前纠正过。我曾经得到一个NetworkOnMainThreadException,所以这就是为什么编辑。 ImageList是应用程序启动时调用的类 – Tanmay
我不知道'LazyAdapter'内部发生了什么,但也许您需要修复它,以便它在单独的线程上执行网络操作。 – waqaslam
上面提到的getView是在LazyAdapter中。因为它扩展了BaseAdapter,我也有必要的方法。如果您可以在我上面提供的链接中查看LazyAdapter,那将会很棒。 – Tanmay