如何从自定义适配器访问视图?

时间:2013-04-06 15:09:25

标签: android custom-adapter

我有自定义适配器用于显示ListView。

public class CustomAdapter extends BaseAdapter implements Filterable {

private ArrayList<OItem> _data;
Context _c;
private int _res;

public TelefoonAdapter(ArrayList<OItem> data, int resource, Context c) {
    _data = data;
    _c = c;
    _res = resource;
}

public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    if (v == null)
    {
       LayoutInflater vi = (LayoutInflater)_c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
       v = vi.inflate(_res, null);
    }

//        TextView tvId = (TextView)v.findViewById(R.id.id);
//        TextView tvName = (TextView)v.findViewById(R.id.name);
//        TextView tvBatch = (TextView)v.findViewById(R.id.batch);

   return v;
}

public int getCount() {
    // TODO Auto-generated method stub
    return 0;
}

public Object getItem(int position) {
    // TODO Auto-generated method stub
    return null;
}

public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
}

public Filter getFilter() {
    // TODO Auto-generated method stub
    return null;
}
}

在活动中我有:

ArrayList<OItem> items = new ArrayList<OItem>();
...
ListView lvSimple = (ListView) findViewById(R.id.lvContent);
lvSimple.setAdapter(new CustomAdapter(items, R.layout.item_telefoon, ATelefoons.this));

问题是我无法在自定义适配器中找到ViewById。 当我执行“v.findViewById(R.id.Name)”时,我看到错误“名称无法解析或不是字段”

我有带有ID名称的TextView,我可以从Activity Class访问它。但不能从自定义适配器(

item_telefoon.xml:

    <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="5dp"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/id"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_gravity="top"
        android:textColor="#6E6E6E"
        android:textStyle="bold"
        android:textSize="18sp" >
    </TextView>

    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_gravity="top"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="2dp"
        android:textStyle="italic"
        android:textSize="12sp" >
    </TextView>
</LinearLayout>

如何从自定义适配器访问视图?

谢谢。

0 个答案:

没有答案