-1

I get data from my server in json format. I pass the data to my Listview and create my list using the custom Adapter below

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

I want to read the data of TAG_CONFIRMATION and if it is equal to "pending" to set visible on an Imageview in the specific row of my Listview. However the Imageview is enabled in all the rows. What should I change in my code? And how is is possible to customize only one specific row in a Listview.

jjm
  • 6,028
  • 2
  • 24
  • 27
makis.k
  • 432
  • 6
  • 23
  • you shouldnt be concerned with a specific row, if the underlying data changes in the adapter you should use `notifiyDatasetChanged` so that the list updates with the new data – tyczj Jan 16 '15 at 17:25
  • But i want to initialize it from the moment i get my data to listview. – makis.k Jan 16 '15 at 17:36
  • do you actually know why you are doing `View vi=convertView;`? – njzk2 Jan 16 '15 at 17:38
  • i got the code from an online tutorial. Can you please explain if i should have that? – makis.k Jan 16 '15 at 17:41
  • You were right that was the mistake. Thank you! – makis.k Jan 16 '15 at 17:43
  • `View vi=convertView;` is part of the recycling process at the heart of the `AdapterView` architecture. The idea is that you don't need to re-inflate a view, you can reuse one that already exists. It means that this view comes with the properties previously set for another item, such as the visibility of your imageView. Which is why you need to explicitly make it invisible again. See http://stackoverflow.com/questions/11945563/how-listviews-recycling-mechanism-works for example – njzk2 Jan 16 '15 at 17:50

2 Answers2

0

You need to use the position variable in the parameters of the getView function.

In the getItem(int position) you need to return the item at that index in the list, not the position that you pass in. So it should return a HashMap at the index in the member variable list.

Then what you can do is call getItem(position) from the getView() function and check if that is the item that the imageView should be enabled on. If not then turn it off, if so turn it on. You must always specify either on or off, otherwise when the view is recycled, if you didn't turn it off, it will still be on.

If you already know the exacts number or position of the specific rom to enable the imageView on, then simply check for it

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

You should also look into the ViewHolder pattern.

WIllJBD
  • 6,144
  • 3
  • 34
  • 44
0

You are not setting the view to View.INVISIBLE Add an else statement to your if(confirmation.getText().toString().equals("pending") ) that will set the ImageView to invisible (or gone).

Chris Margonis
  • 935
  • 11
  • 16