我正在使用此代码
处理简单的List视图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;
}
}
}
但结果就像在Image中一样 十一点之后它给我看了一,二,二。 请帮我
答案 0 :(得分: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;
}
始终在持有人中写下其他条件。
答案 1 :(得分: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;
}
答案 2 :(得分:1)
无法在“正确”的地方添加评论,但Niteesh Mehra建议将LV置于scrollview中是错误的。 LV是一个在垂直滚动列表中显示项目的视图,因此没有必要再次这样做。
至于你的问题 - 你的getView()实现是错误的。如果convertView不为null,则应该重用视图,但重用意味着您已经烘焙了视图(布局),但是您仍然必须使用此行的内容填充它。所以你应该这样做:
if( convertView == null ) {
convertView = <inflate view from XML>;
}
然后,您在视图元素中的数据无条件。
答案 3 :(得分:1)
我改变了我的getView()并解决了问题。
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;
}
答案 4 :(得分: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));