事实上,我有一个列表视图,它有“两个图像+两个Textviews”作为项目。
我想要做的是,我想在用户点击图片时更改textview的文字。
public class ListViewCustomAdapter extends BaseAdapter implements OnClickListener{
`ViewHolder holder;`
`ArrayList<Object> itemList;`
`public Activity context;`
`public LayoutInflater inflater;`
`public ListViewCustomAdapter(Activity context,ArrayList<Object> itemList) {`
`super();`
`this.context = context;`
`this.itemList = itemList;`
`this.inflater =` `(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);`
`}`
`@Override`
`public int getCount() {`
`// TODO Auto-generated method stub`
`return itemList.size();`
`}`
`@Override`
`public Object getItem(int position) {`
`// TODO Auto-generated method stub`
`return itemList.get(position);`
`}`
`@Override`
`public long getItemId(int position) {`
`// TODO Auto-generated method stub`
`return 0;`
`}`
`public static class ViewHolder`
`{`
`ImageView plus;`
`ImageView minus;`
`TextView question;`
`TextView score;`
`}`
`@Override`
`public View getView(int position, View convertView, ViewGroup parent) {`
`// TODO Auto-generated method stub`
`if(convertView==null)`
`{`
`holder = new ViewHolder();`
`convertView = inflater.inflate(R.layout.listitem_row, null);`
`holder.plus = (ImageView) convertView.findViewById(R.id.plus);`
`holder.minus = (ImageView) convertView.findViewById(R.id.minus);`
`holder.plus.setOnClickListener(this);`
`holder.minus.setOnClickListener(this);`
`holder.question = (TextView) convertView.findViewById(R.id.question);`
`holder.score = (TextView) convertView.findViewById(R.id.score);`
`convertView.setTag(holder);`
`}`
`else`
`holder=(ViewHolder)convertView.getTag();`
`ItemBean bean = (ItemBean) itemList.get(position);`
`holder.minus.setImageResource(bean.getminus());`
`holder.plus.setImageResource(bean.getplus());`
`holder.question.setText(bean.getquestion());`
`holder.score.setText(bean.getscore());`
`return convertView;`
`}`
`@Override`
`public void onClick(View v) {`
`switch(v.getId()) {`
`case R.id.minus:`
`{`
`int score = Integer.getInteger(holder.score.getText().toString());`
`holder.score.setText(Integer.toString(score-1));`
`break;`
`}`
`case R.id.plus:`
`{`
`int score = Integer.getInteger(holder.score.getText().toString());`
`holder.score.setText(Integer.toString(score+1));`
`break;`
`}`
`}}`
}
我得到空指针异常。 我认为问题是,程序无法理解它所针对的文本视图,例如此列表视图中有多个项目,并且每个项目都包含此Score Textview项目。
Plz帮助我!!
答案 0 :(得分:1)
最好的办法是在创建ListItem时为其设置onClickListener。然后,您可以指定ListItem在单击时执行的操作,以便您可以告诉它更改它的TextView文本。
这是一个关于创建该设置的非常好的教程: http://www.ezzylearning.com/tutorial.aspx?tid=1351248&q=handling-android-listview-onitemclick-event
答案 1 :(得分:1)
而不是convertView.setTag(holder); holder =(ViewHolder)convertView.getTag();` 使用convertView.setId(holder);和holder =(ViewHolder)convertView.getId();
答案 2 :(得分:1)
谢谢大家。
但问题的解决方案非常简单明了。我已经解决了。
我使用holder.minus.setTag(持有人)附上了持有人;针对listview的每一行的每个项目(button / imageview),然后在该特定按钮/ imageView的Onclick Listener中我使用holder =(ViewHolder)v.getTag();使用特定的持有人为ListView的项目:)玩得开心
@覆盖 public View getView(int position,View convertView,ViewGroup parent){
int type = getItemViewType(position);
System.out.println("getView " + position + " " + convertView + " type = " + type);
if (convertView == null) {
holder = new ViewHolder();
switch (type) {
case TYPE_ITEM:
convertView = mInflater.inflate(R.layout.item, null);
holder.score = (TextView)convertView.findViewById(R.id.score);
holder.question = (TextView)convertView.findViewById(R.id.question);
holder.plus = (ImageView)convertView.findViewById(R.id.plus);
holder.minus = (ImageView)convertView.findViewById(R.id.minus);
holder.plus.setTag(holder); holder.minus.setTag(holder);
holder.minus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
holder = (ViewHolder)v.getTag();
String test =holder.score.getText().toString();
}
}
});
holder.plus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
holder = (ViewHolder)v.getTag();
String test =holder.score.getText().toString();
}
});
break;
case TYPE_SEPARATOR:
convertView = mInflater.inflate(R.layout.heading, null);
holder.score = (TextView)convertView.findViewById(R.id.textSeparator);
holder.score.setText(mData.get(position));
break;
}
convertView.setTag(holder);
} else {
holder = (ViewHolder)convertView.getTag();
}
switch (type)
{
case TYPE_ITEM:
String temp = mData.get(position);
break;
case TYPE_SEPARATOR:
holder.score.setText(mData.get(position));
break;
}
return convertView;
}
}
答案 3 :(得分:0)
不要使用else替换下面的代码
`if(convertView==null)`
`{`
`holder = new ViewHolder();`
`convertView = inflater.inflate(R.layout.listitem_row, null);`
`holder.plus = (ImageView) convertView.findViewById(R.id.plus);`
`holder.minus = (ImageView) convertView.findViewById(R.id.minus);`
`holder.plus.setOnClickListener(this);`
`holder.minus.setOnClickListener(this);`
`holder.question = (TextView) convertView.findViewById(R.id.question);`
`holder.score = (TextView) convertView.findViewById(R.id.score);`
`convertView.setTag(holder);`
`}
`holder=(ViewHolder)convertView.getTag();`
`ItemBean bean = (ItemBean) itemList.get(position);`
`holder.minus.setImageResource(bean.getminus());`
`holder.plus.setImageResource(bean.getplus());`
`holder.question.setText(bean.getquestion());`
`holder.score.setText(bean.getscore());`
`return convertView;`
`}`
答案 4 :(得分:0)
首先,您没有将 onClick 侦听器分配给Base适配器中的任何视图。
其次,我不建议在行中使用 onClick 事件,而是建议您使用 OnItemClickListener 作为ListLiew。这非常直接,更适合用于行项目点击
例如:
private OnItemClickListener lstClick = new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//do whatever you want here
}
};
并将其设置为ListView:
listView.setOnItemClickListener(lstClick);