Android ListView使用自定义适配器使用服务器数据进行单行更改
我从服务器获取json格式的数据。我将数据传递到我的Android ListView使用自定义适配器使用服务器数据进行单行更改,android,listview,android-listview,android-custom-view,listviewitem,Android,Listview,Android Listview,Android Custom View,Listviewitem,我从服务器获取json格式的数据。我将数据传递到我的列表视图,并使用下面的自定义适配器创建我的列表 public class AppointmentAdapter extends BaseAdapter { private Activity activity; private ArrayList<HashMap<String, String>> data; private static LayoutInflater inflater=null;
列表视图
,并使用下面的自定义适配器创建我的列表
public class AppointmentAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;
public AppointmentAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
data=d;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//imageLoader=new ImageLoader(activity.getApplicationContext());
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.appointments_list_item, null);
TextView aid = (TextView)vi.findViewById(R.id.aid); // id
TextView apptitle = (TextView)vi.findViewById(R.id.apptitle); // title
TextView starts_date = (TextView)vi.findViewById(R.id.starts_date); // created_at
TextView starts_time = (TextView)vi.findViewById(R.id.starts_time);
TextView contact = (TextView)vi.findViewById(R.id.contact);
TextView confirmation = (TextView)vi.findViewById(R.id.confirmation);
ImageView new_appointment = (ImageView)vi.findViewById(R.id.new_appointment);
//CheckBox check = (CheckBox)vi.findViewById(R.id.tododone); // checkbox
HashMap<String, String> todo = new HashMap<String, String>();
todo = data.get(position);
// Setting all values in listview
aid.setText(todo.get(AppointmentsFragment.TAG_AID));
apptitle.setText(todo.get(AppointmentsFragment.TAG_APPTITLE));
starts_date.setText(todo.get(AppointmentsFragment.TAG_STARTDATE));
starts_time.setText(todo.get(AppointmentsFragment.TAG_STARTTIME));
contact.setText(todo.get(AppointmentsFragment.TAG_CONTACT));
confirmation.setText(todo.get(AppointmentsFragment.TAG_CONFIRMATION));
//String test = confirmation.getText().toString();
//Log.d("CONFIRMATION: ", confirmation);
if (confirmation.getText().toString().equals("pending") ){
Log.d("PASS CONFIRMATION: ", confirmation.getText().toString());
new_appointment.setVisibility(View.VISIBLE);
}
return vi;
}
}
公共类指定适配器扩展了BaseAdapter{
私人活动;
私有数组列表数据;
专用静态充气机=空;
公共任命适配器(活动a,阵列列表d){
活动=a;
数据=d;
充气器=(LayoutInflater)activity.getSystemService(Context.LAYOUT\u充气器\u SERVICE);
//imageLoader=新的imageLoader(activity.getApplicationContext());
}
public int getCount(){
返回data.size();
}
公共对象getItem(int位置){
返回位置;
}
公共长getItemId(int位置){
返回位置;
}
公共视图getView(int位置、视图转换视图、视图组父视图){
视图vi=转换视图;
if(convertView==null)
vi=充气机。充气(R.layout.Appoints\u list\u项,空);
TextView-aid=(TextView)vi.findViewById(R.id.aid);//id
TextView-apptitle=(TextView)vi.findViewById(R.id.apptitle);//title
TextView开始日期=(TextView)vi.findviewbyd(R.id.starts\u date);//在
TextView开始时间=(TextView)vi.findViewById(R.id.starts\u时间);
TextView联系人=(TextView)vi.findViewById(R.id.contact);
TextView确认=(TextView)vi.findViewById(R.id.confirmation);
ImageView新预约=(ImageView)vi.findViewById(R.id.new\u预约);
//CheckBox check=(CheckBox)vi.findviewbyd(R.id.tododone);//CheckBox
HashMap todo=新的HashMap();
todo=data.get(位置);
//在listview中设置所有值
aid.setText(todo.get(appointsFragment.TAG_-aid));
setText(todo.get(AppointsFragment.TAG_apptitle));
starts_date.setText(todo.get(appointsFragment.TAG_STARTDATE));
starts_time.setText(todo.get(appointsFragment.TAG_STARTTIME));
contact.setText(todo.get(appointsFragment.TAG_contact));
confirmation.setText(todo.get(appointsFragment.TAG_confirmation));
//字符串测试=confirmation.getText().toString();
//日志d(“确认:”,确认);
if(confirmation.getText().toString().equals(“待定”)){
Log.d(“传递确认:”,CONFIRMATION.getText().toString());
新建_appointment.setVisibility(View.VISIBLE);
}
返回vi;
}
}
我想读取标记确认的数据,如果它等于“待定”
在我的列表视图的特定行中的Imageview
上设置可见。但是,所有行中都启用了Imageview
。我应该在代码中更改什么?如何在列表视图中仅自定义一个特定行
您需要在getView函数的参数中使用位置变量
在getItem(int位置)中,需要返回列表中该索引处的项,而不是传递的位置。因此,它应该在成员变量列表的索引处返回一个HashMap
然后,您可以从getView()函数调用getItem(position),并检查是否应该在该项上启用imageView。如果不是,则将其关闭,如果是,则将其打开。必须始终指定“打开”或“关闭”,否则在回收视图时,如果未将其关闭,视图仍将处于打开状态
如果您已经知道启用imageView的特定rom的确切编号或位置,那么只需检查一下即可
if (position == rowIAmLookingFor && TAG_CONFIRMATION.equals("pending"))
imageView.setVisibility(View.VISIBLE);
else
imageView.setVisibility(View.GONE);
您还应该查看ViewHolder模式。您没有将视图设置为视图。不可见
将else
语句添加到if(confirmation.getText().toString().equals(“pending”)
中,该语句将ImageView
设置为不可见(或消失)。您不应该关心特定的行,如果适配器中的基础数据发生更改,则应使用NotifyDatasetChanged
,以便列表使用新数据更新,但我希望从将数据获取到listview的那一刻起对其进行初始化。您是否确实知道为什么要这样做View vi=convertView代码>?我从在线教程中获得了代码。你能解释一下我是否应该这样做吗?你是对的,那是个错误。非常感谢。ImageView被设置为对我的xml文件不可见。然后可能会回收一个已经可见ImageView的视图。检查将失败,但可见性将保持可见。