虽然有很多关于适配器的问题,但我已经阅读了所有问题,但我的问题没有解决。我有一个基本适配器,我从控制台一次更新了我的适配器我可以看到它每次调用getView
时都在调用notifyDatasetChanged()
。但是我的onclick()
还没有更改视图项.......看起来问题出在我分配给列表的位置编号
package com.bisota.utility;
import java.util.ArrayList;
import com.bisota.R;
import com.bisota.activity.Home;
import com.bisota.model.Task;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class ListViewAdapter extends BaseAdapter{
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.position=0;
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;
int position;
}
@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);
viewHolder.position=0;
convertView.setTag(viewHolder);
}
else
{
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));
}
//another section
if(tasks.get(position).status.accepted==true)
{
System.out.println("A");
//viewHolder.accept.setImageResource(R.drawable.yes);
viewHolder.reject.setVisibility(View.GONE);
viewHolder.accept.setVisibility(View.GONE);
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.GONE);
viewHolder.accept.setVisibility(View.GONE);
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());
this.position=position;
viewHolder.accept.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v){
// TODO Auto-generated method stub
System.out.println("accept clicked");
tasks.get(ListViewAdapter.this.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");
tasks.get(ListViewAdapter.this.position).status.rejected=true;
ListViewAdapter.this.notifyDataSetChanged();
}
});
return convertView;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return Home.taskObjList.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 0;
}
private void updateItemAtPosition(int position) {
int visiblePosition = mListView.getFirstVisiblePosition();
View view = mListView.getChildAt(position - visiblePosition);
mListView.getAdapter().getView(position, view, mListView);
}
}
但我的目的是通过检查适配器中的if-else来改变按钮视图:
if(tasks.get(position).status.accepted==true)
{
System.out.println("A");
//viewHolder.accept.setImageResource(R.drawable.yes);
viewHolder.reject.setVisibility(View.GONE);
viewHolder.accept.setVisibility(View.GONE);
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.GONE);
viewHolder.accept.setVisibility(View.GONE);
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);
}
好像我的问题不在notifyDatasetChanged
或视图中,问题在于我提供给我的列表中的位置编号。如何检索该特定行的位置编号?
答案 0 :(得分:1)
首先做
viewHolder.status.setVisibility(View.GONE);
viewHolder.reject.setVisibility(View.GONE);
viewHolder.accept.setVisibility(View.GONE);
然后编写可见性逻辑
if(tasks.get(position).status.accepted==true)
{
System.out.println("A");
//viewHolder.accept.setImageResource(R.drawable.yes);
viewHolder.reject.setVisibility(View.GONE);
viewHolder.accept.setVisibility(View.GONE);
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.GONE);
viewHolder.accept.setVisibility(View.GONE);
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);
}
答案 1 :(得分:0)
您不应在适配器本身上实现onClick()
方法。同样应该在它之外调用notifyDataSetChanged()
。
请在此处查看此示例:
How to correctly implement BaseAdapter.notifyDataSetChanged() in Android