私はアンドロイドに不慣れです。誰かが私が間違っているのは何ですか?listViewが更新されません!画像を含むリストビューが必要です。ImageList.java
ここで、メインクラスとしてLazyLoader.java
BaseAdapterを拡張するフローについて説明します。ここで、画像はローカルホストに保存されているxmlファイルから取得されます。動作するのはこれです。10.0.2.2を使用したローカルホストには問題はなく、LogCatでxmlを取得します。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クラスはここにありますここにあります。BaseAdapterLazyAdapter.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>