adapter android自定义,Android:自定义ListAdapter

约定不等于承诺〃 2022-10-16 07:35 339阅读 0赞

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*/

发表评论

表情:
评论列表 (有 0 条评论,339人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Android定义View

    前几天在郭霖大神的博客上看了自定义View的知识,感觉受益良多,大神毕竟大神。在此总结一下关于Android 自定义View的用法: 首先,自定义View可以由基本控件或者组

    相关 Android定义View

    如何自定义控件 1. 自定义属性的声明和获取 2. 测量onMeasure:测量自定义控件的尺寸 3. 绘制onDraw:绘制自定义控件 4. 状态的存储与恢复:

    相关 定义Adapter

    在进行安卓开发的时候使用arrayadapter和simpleadapter显示数据的方式比较固定,如果我们想对listview进行更加丰富的展示,那么我们就要使用自定义ada