我已经按照其他问题完成了他们提到的但没有得到结果。我的getTag()给了我ArrayIndexOutofBound错误。
private final Home context;
private LayoutInflater inflater;
private int position;
private ListView mListView;
private ArrayList<Task> tasks;
public ListViewAdapter(Home context,ArrayList<Task> tasks) {
super();
this.context=context;
inflater = LayoutInflater.from(this.context);
this.mListView=mListView;
this.tasks=tasks;
}
private static class ViewHolder {
ImageView vendor;
ImageButton accept;
ImageButton reject;
TextView date;
TextView time;
TextView companyName;
TextView taskName;
TextView taskType;
TextView description;
TextView status;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
System.out.println("GET VIEW");
ViewHolder viewHolder = null;
if(convertView == null) {
convertView = inflater.inflate(R.layout.task_item_layout,null);
viewHolder = new ViewHolder();
viewHolder.vendor = (ImageView)convertView.findViewById(R.id.vendor);
viewHolder.accept = (ImageButton)convertView.findViewById(R.id.yes);
viewHolder.reject = (ImageButton)convertView.findViewById(R.id.no);
viewHolder.date = (TextView) convertView.findViewById(R.id.date);
viewHolder.time = (TextView) convertView.findViewById(R.id.time);
viewHolder.companyName = (TextView) convertView.findViewById(R.id.companyName);
viewHolder.taskName = (TextView) convertView.findViewById(R.id.taskName);
viewHolder.taskType = (TextView) convertView.findViewById(R.id.taskType);
viewHolder.description = (TextView) convertView.findViewById(R.id.description);
viewHolder.status=(TextView)convertView.findViewById(R.id.responseMsg);
convertView.setTag(viewHolder);
}
else
{
//System.out.println("getTag");
viewHolder = (ViewHolder)convertView.getTag();
}
Resources resources = this.context.getResources();
if(Home.taskObjList.get(position).providerType.Facebook==true)
{
System.out.println("facebook");
viewHolder.vendor.setImageDrawable(resources.getDrawable(R.drawable.facebook));
}
else if(Home.taskObjList.get(position).providerType.Twitter==true)
{
System.out.println("twitter");
viewHolder.vendor.setImageDrawable(resources.getDrawable(R.drawable.twitter));
}
viewHolder.status.setVisibility(View.INVISIBLE);
viewHolder.reject.setVisibility(View.INVISIBLE);
viewHolder.accept.setVisibility(View.INVISIBLE);
if(tasks.get(position).status.accepted==true)
{
System.out.println("A");
//viewHolder.accept.setImageResource(R.drawable.yes);
viewHolder.reject.setVisibility(View.INVISIBLE);
viewHolder.accept.setVisibility(View.INVISIBLE);
viewHolder.status.setVisibility(View.VISIBLE);
viewHolder.status.setText("Accepted");
viewHolder.status.setTextColor(Color.parseColor("#006600"));
}
else if(tasks.get(position).status.rejected==true)
{
System.out.println("R");
// viewHolder.reject.setImageResource(R.drawable.no);
viewHolder.reject.setVisibility(View.INVISIBLE);
viewHolder.accept.setVisibility(View.INVISIBLE);
viewHolder.status.setVisibility(View.VISIBLE);
viewHolder.status.setText("Rejected");
viewHolder.status.setTextColor(Color.RED);
// notifyDataSetChanged();
}
else
{
System.out.println("N");
viewHolder.status.setVisibility(View.GONE);
viewHolder.reject.setVisibility(View.VISIBLE);
viewHolder.accept.setVisibility(View.VISIBLE);
// viewHolder.accept.setImageResource(R.drawable.yes);
// viewHolder.reject.setImageResource(R.drawable.no);
}
String arrayString[] = tasks.get(position).startDate.toString().split("\\s+");
viewHolder.date.setText(arrayString[0].toString());
viewHolder.time.setText(arrayString[1].toString());
//viewHolder.companyName.setText(Home.taskObjList.get(position).companyName.toString());
viewHolder.taskName.setText(tasks.get(position).taskName.toString());
viewHolder.description.setText(tasks.get(position).description.toString());
//viewHolder.taskType.setText(Home.taskObjList.get(position).task.toString());
//viewHolder.category.setText(events.get(position).getCategory());
viewHolder.accept.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v){
// TODO Auto-generated method stub
System.out.println("accept clicked");
int position = (v.getTag() == null) ? -1 : (Integer) v.getTag();
System.out.println(position);
tasks.get(position).status.accepted=true;
ListViewAdapter.this.notifyDataSetChanged();
}
});
viewHolder.reject.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v){
// TODO Auto-generated method stub
System.out.println("reject clicked");
int position = (Integer) v.getTag();
tasks.get(position).status.rejected=true;
ListViewAdapter.this.notifyDataSetChanged();
}
});
return convertView;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return tasks.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return tasks.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return tasks.indexOf(getItem(position));
}
在我的onclick()函数子句中,我正在更新我的适配器的日期集,我需要在我的列表中更新该项目的位置编号:
viewHolder.accept.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v){
// TODO Auto-generated method stub
System.out.println("accept clicked");
int position = (v.getTag() == null) ? -1 : (Integer) v.getTag();
System.out.println(position);
tasks.get(position).status.accepted=true;
ListViewAdapter.this.notifyDataSetChanged();
}
});
viewHolder.reject.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v){
// TODO Auto-generated method stub
System.out.println("reject clicked");
int position = (Integer) v.getTag();
tasks.get(position).status.rejected=true;
ListViewAdapter.this.notifyDataSetChanged();
}
});
FATAL EXCEPTION: main
Process: com.bisota, PID: 8917
java.lang.ArrayIndexOutOfBoundsException: length=27; index=-1
java.util.ArrayList.get(ArrayList.java:310)
com.bisota.utility.ListViewAdapter$1.onClick(ListViewAdapter.java:173)
at android.view.View.performClick(View.java:4761)
at android.view.View$PerformClick.run(View.java:19767)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5312)
答案 0 :(得分:3)
viewHolder.reject.setTag(position);
viewHolder.accept.setTag(position);
您是否在适配器getview代码中设置了标记?不,这样做。
在此行Resources resources = this.context.getResources();
之前添加上述代码。