Android ListView使用服务器数据使用自定义适配器单行更改

时间:2015-01-16 17:19:03

标签: android listview android-listview android-custom-view listviewitem

我以json格式从服务器获取数据。我将数据传递给我的Listview并使用下面的自定义适配器创建我的列表

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;
    }   
}

我想阅读TAG_CONFIRMATION的数据,如果它等于"pending"Imageview的特定行中的Listview上设置可见内容。但是,所有行都启用了Imageview。我应该在代码中更改什么?如何才能自定义Listview中的一个特定行。

2 个答案:

答案 0 :(得分:0)

您需要在getView函数的参数中使用位置变量。

在getItem(int position)中,你需要返回列表中该索引处的项,而不是你传入的位置。所以它应该在成员变量列表的索引处返回一个HashMap。

然后你可以做的是从getView()函数调用getItem(position)并检查是否应该启用imageView的项目。如果没有,请将其关闭,如果是这样,请将其打开。您必须始终指定打开或关闭,否则当视图被回收时,如果您没有将其关闭,它仍然会打开。

如果你已经知道特定rom的确切数量或位置以启用imageView,那么只需检查它

if (position == rowIAmLookingFor && TAG_CONFIRMATION.equals("pending"))
   imageView.setVisibility(View.VISIBLE);
else 
   imageView.setVisibility(View.GONE);

您还应该查看ViewHolder模式。

答案 1 :(得分:0)

您没有将视图设置为View.INVISIBLEelse添加if(confirmation.getText().toString().equals("pending") )语句,将ImageView设置为不可见(或已消失)。