I’ve listview on which when a user clicks an item I show an image that user has clicked this item.The listview gets populated from notifications.
Suppose an item gets added in listview.Now I click that item and image is shown in front of it.Now when I enter the new item in the listview nothing will happen(there are 2 items in the listview now). When I add the third item in the listview the image from the item at position 0 hides and is shown in position 1. Why this is happening?
Now to add an image in the listview item. I have to first perform some checks on the next opening activity and if those checks are successful then I place an image in front of that clicked item in the activity.
So to do this what i did is I am sending the clicked item position in the next activity and perform those checks, if those checks are successful I place and image on the onActivityResult function of the previous activity by using that position which I sent to this activity.Meaning that I send that position value back to the previous activity in the onActivityResult function where I place the Image.
The placement of image works fine.But while I recieve notification I add the item in the listview which results in the getView function being called for each item in the listview again to populate the listview.Here the problem occurs i.e the image from the position 0 item in the list goes to the position 1 item.
Please ask if more explanation is needed or something is not clear.
Here’s the code for listview Item Click:
TextView tv=(TextView)view.findViewById(R.id.clientip);
Intent i=new Intent(getApplicationContext(),ChatPageActivity.class);
i.putExtra("AdminEmail",ClientListActivity.AdminEmail);
i.putExtra("ClientIp",tv.getText().toString());
i.putExtra("ChildAt", position);
startActivityForResult(i, 200);
ChildAt is the position I am sending.
After performing checks in the next activity.
@Override
protected void onActivityResult(int requestCode,int resultCode,Intent data)
{
try {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode!= 23)
{
Integer integer=data.getExtras().getInt("ChildAt");
ImageView imageView=(ImageView)lv.getChildAt(integer).findViewById(R.id.onlineImg);
imageView.setImageResource(R.drawable.online);
}
}catch(Exception e)
{
Log.e("ClientList_onActivityResult",e.getMessage());
}
}
This is the onActivityResult if the code is not equal to 23 it means all the checks in the next activity are correct and you can place the image at the position which is sent back again to this activity from the next activity.
Here’s the getView code of my BaseAdapter class:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view=convertView;
if(convertView==null)
view=inflater.inflate(R.layout.activeclientlist,null);
TextView ip=(TextView)view.findViewById(R.id.clientip);
String clientip=this.iplist.get(position);
ip.setText(clientip);
return view;
}
In the baseADapter I insert item in existing listview using this piece of code..
this.iplist.add(ip);
this.notifyDataSetChanged();
Important:
Also I wanted to mention that this behaviour happens in a pattern i.e as i’ve told above the image position changes when i insert the third item.After that it changes again when I’ll add the fifth item and so on.
You should put the code to update the image view within the adapter itself.
The adapter should also be knowing all the info he needs to draw the data. So instead of having it store a list of Strings, store a list of objects like:
You then have to change your adapter:
Then just change the code to update the image when the connection status changes:
This way you keep all the code related to representing your data within the adapter. Side effect, it should be correcting your code.
Also have a look at the ViewHolder pattern to avoid doing findViewById on each row when you have a convertView available.