adapter android自定义,Android:自定义ListAdapter
android:id=”@+id/Layout_result”
xmlns:android=”http://schemas.android.com/apk/res/android“
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
android:background=”@drawable/papero3”>
android:id=”@+id/resultListView”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”>
android:id=”@+id/empty”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”>
列表项.xml
xmlns:android=”http://schemas.android.com/apk/res/android“
android:id=”@+id/listItem”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:orientation=”horizontal”
android:paddingLeft=”10px”
android:paddingRight=”10px”
android:paddingTop=”5px”
android:paddingBottom=”5px”>
android:id=”@+id/result_item_name_object”
android:layout_height=”wrap_content”
android:singleLine=”false”
android:gravity=”left”
android:textSize=”18sp” android:layout_width=”260px”>
android:id=”@+id/result_item_distance”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:gravity=”right”
android:textSize=”15sp” android:paddingLeft=”10px”>
主代码:
ListView resultList = (ListView)findViewById(R.id.resultListView);
ItemAdapter m_adapter = new ItemAdapter(this, R.layout.list_item, itemsFound);
resultList.setAdapter(m_adapter);
viewItems = new Runnable(){
@Override
public void run() {
searchItems();
}
};
Thread thread = new Thread(null, viewItems, “Searching”);
thread.start();
private class ItemAdapter extends ArrayAdapter{
private ArrayList items;
public ItemAdapter(Context context, int textViewResourceId,
ArrayList objects) {
super(context, textViewResourceId, objects);
this.items = objects;
}
public View getView(int position, View convertView, ViewGroup parent){
View v = convertView;
if(v == null){
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.list_item, null);
}
Item item = (Item) items.get(position);
if (item != null){
TextView resName = (TextView) v.findViewById(R.id.result_item_name_object);
TextView resDistance = (TextView) v.findViewById(R.id.result_item_distance);
if (resName != null){
resName.setText(item.name);
}
if (resDistance != null){
resDistance.setText(item.distance);
}
}
return v;
}
}
private Runnable returnRes = new Runnable() {
@Override
public void run() {
if(itemsFound != null && itemsFound.size() > 0){
m_adapter.notifyDataSetChanged();
int count = itemsFound.size();
for(int i=0; i < count; i++)
m_adapter.add(itemsFound.get(i));
}
m_ProgressDialog.dismiss();
m_adapter.notifyDataSetChanged();
}
};
第一个问题是result.xml布局窗口中的“AdapterView不支持addView(View,LayoutParams)”,但当我从resut.xml中的listview清除textview时,问题消失了,但出现了一个新问题:
11-10 10:39:42.623: ERROR/AndroidRuntime(720): android.content.res.Resources$NotFoundException: String resource ID #0x304
11-10 10:39:42.623: ERROR/AndroidRuntime(720): at android.content.res.Resources.getText(Resources.java:200)
/*and so on*/
还没有评论,来说两句吧...