2

我有一个自定义ArrayAdapter设置为listView. 在每个元素中,listView我都有一个与我创建的 .xml 匹配的项目。代码如下所示:

列表内容.xml

<RelativeLayout
[... some code ommited]
    <ImageView
    [...]
    <TextView
    android:id="@+id/listViewText"
    [...]

Custom Array Adapter getView(int position, View convertView, ViewGroup parent)

if (position < 4)
{
    convertView = inflater.inflate(R.layout.listContent,null);

    TextView tv = (TextView) findViewById(R.id.listViewText);

    tv.setTextSize(18);
    if (position == 0) tv.setText("0");
    else if (position == 1) tv.setText("1");
    else if (position == 2) tv.setText("2");
    else if (position == 3) tv.setText("3");

    return convertView;
}
return null;

我似乎无法让它工作,我总是遇到一些错误。为了编辑TextView我的 listView 项目内部,我需要做什么?

4

2 回答 2

1

将您的自定义阵列适配器 getView 代码更改为:

@Override
public View getView(int position, View convertView, ViewGroup parent){

 View row = convertView;

 if(row==null){
  LayoutInflater inflater=getLayoutInflater();
  row=inflater.inflate(R.layout.listContent, parent, false);
 }

 TextView tv=(TextView)row.findViewById(R.id.listViewText);


tv.setTextSize(18);
    if (position == 0) tv.setText("0");
    else if (position == 1) tv.setText("1");
    else if (position == 2) tv.setText("2");
    else if (position == 3) tv.setText("3");

 return row;
}
于 2012-12-12T12:54:48.860 回答
1

改变

TextView tv = (TextView) findViewById(R.id.listViewText);

TextView tv = (TextView) convertView.findViewById(R.id.listViewText);

并删除这个

if (position < 4)
于 2012-12-12T13:00:58.340 回答