0

我正在使用此代码处理简单的列表视图,

public class RadioDemoActivity extends Activity {
ArrayList<String> list = new ArrayList<String>();
MyListAdapter mla;
ListView lv;
int position = -1;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    lv = (ListView) findViewById(R.id.listView1);

    list.add("one");
    list.add("two");
    list.add("three");
    list.add("four");
    list.add("five");
    list.add("six");
    list.add("seven");
    list.add("eight");
    list.add("nine");
    list.add("ten");
    list.add("eleven");
    list.add("twelve");
    list.add("thirteen");
    list.add("fourteen");
    list.add("fifteen");
    list.add("sixteen");
    list.add("seventeen");
    list.add("eightteen");
    list.add("nineteen");
    list.add("twenty");

    mla = new MyListAdapter(this);
    lv.setAdapter(mla);

}


public class MyListAdapter extends BaseAdapter {
    private LayoutInflater mInflater;

    public MyListAdapter(Context context) {
        mInflater = LayoutInflater.from(context);

    }

    public int getCount() {
        return list.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.cust_list, null);

            TextView tv = (TextView)convertView.findViewById(R.id.txt_title);
            ImageView iv = (ImageView)convertView.findViewById(R.id.imageView1);
            tv.setText(list.get(position));
            Log.v("log_tag","position In"+list.get(position));

        }
        return convertView;
    }
  }
}

但结果就像十一点后的图像中显示的一,一,二。请帮我 enter image description here

4

5 回答 5

2
@Override
    public View getView(int position, View convertView, ViewGroup parent) {
         ListHolder holder = null;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.cust_list, null);
            holder=new ListHolder();
            holder.tv = (TextView)convertView.findViewById(R.id.txt_title);
            holder.iv = (ImageView)convertView.findViewById(R.id.imageView1);
            Log.v("log_tag","position In"+list.get(position));
            convertView.setTag(holder);
        }
         else
        {
        holder=(ListHolder) convertView.getTag();
        }
        holder.tv.setText(list.get(position));
        return convertView;
    }
 static class ListHolder
{
    TextView tv;

    ImageView iv;
}

Always write else condition in holder.

于 2012-07-17T12:37:33.603 回答
1

将您更改getView()为:

public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.cust_list, null);
    }
    TextView tv = (TextView)convertView.findViewById(R.id.txt_title);
    ImageView iv = (ImageView)convertView.findViewById(R.id.imageView1);
    tv.setText(list.get(position));
    Log.v("log_tag","position In"+list.get(position));

    return convertView;
}
于 2012-07-17T12:30:00.620 回答
1

不能在“正确”的地方添加评论,但 Niteesh Mehra 上面关于将 LV 放在滚动视图中的建议是错误的。LV 是一个在垂直滚动列表中显示项目的视图,因此没有必要再这样做了。

至于您的问题-您的 getView() 实现是错误的。如果 convertView 不为空,您应该重用给定的视图,但重用意味着您已经获得了视图(布局),但您仍然必须使用该行的内容来填充它。因此,您应该执行以下操作:

if( convertView == null ) {
 convertView = <inflate view from XML>;
}

Then you your data in view's elements unconditionally.

于 2012-07-17T12:32:16.910 回答
1

I changed my getView() like this and problem solved.

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

        convertView = mInflater.inflate(R.layout.cust_list, null);

        TextView tv = (TextView) convertView.findViewById(R.id.txt_title);
        ImageView iv = (ImageView) convertView.findViewById(R.id.imageView1);

        tv.setText(list.get(position));
        tv.setTag(list.get(position));

        if (RadioDemoActivity.this.position==position)
            iv.setImageResource(R.drawable.greenround);

        return convertView;

    }
于 2012-07-18T09:15:36.963 回答
0

像这样改变getView方法

if (convertView == null) {
        convertView = mInflater.inflate(R.layout.cust_list, null);
        }
        TextView tv = (TextView)convertView.findViewById(R.id.txt_title);
        ImageView iv = (ImageView)convertView.findViewById(R.id.imageView1);
        tv.setText(list.get(position));
        Log.v("log_tag","position In"+list.get(position));
于 2012-07-17T12:30:14.497 回答