I am trying to figure out the solution for the following listed problem. I have a Listview
generated using Simpleadapter
. When I click on a row in the listview I want to make a Layout with an id colorful
as visible. I am able to do this. But my problem starts here. When I click on another row say row number 5 the colorful layout is visible, but the layout is also visible for the previously clicked row too. What I want to do is make the layout colorful visible for only the clicked row (i.e it should be visible for only one row at a time i.e is currently clicked row and hidden for all the remaining rows) and the layout should get invisible for the previously clicked rows. I tried doing with viewholder
but it doesn't help. My code snippet is below. Guide me step by step as I am very new to Android.
final BaseAdapter k=new SimpleAdapter(getActivity(),val,R.layout.mytaskdata,new String[]{"sname","heading","desc","id","path","receiver","sender"},new int[]{R.id.textView1,R.id.textView2,R.id.textView3,R.id.hide1,R.id.hide2,R.id.hide3,R.id.hide4})
{
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
final View v = super.getView(position, convertView, parent);
TextView myname=(TextView)v.findViewById(R.id.textView1);
TextView mydes=(TextView)v.findViewById(R.id.textView2);
TextView mytopic=(TextView)v.findViewById(R.id.textView3);
ImageView edit=(ImageView)v.findViewById(R.id.ImageView03);
sent.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
RelativeLayout r=(RelativeLayout)arg1.findViewById(R.id.colorful);
// r.setVisibility(arg1.VISIBLE);
int temp=sent.getCheckedItemPosition();
Log.i("itemposition",""+temp);
Toast.makeText(getActivity(),"pos"+arg2+"hii"+positionThatSaysHi,1000).show();
if(arg2!=positionThatSaysHi)
{
r.setVisibility(arg1.VISIBLE);
positionThatSaysHi = arg2;
notifyDataSetChanged();
}
else
{
r.setVisibility(arg1.GONE);
notifyDataSetChanged();
}
});
Take advantage of choice mode in ListView ( see setChoiceMode()), and set it to CHOICE_MODE_SINGLE.
Use the selection state to toggle the visibility of your colorful layout. If possible, the easiest way to do this is to have the colorful bits in a selector drawable, with selected=true. That way it will show automatically, and you don't have to worry about hiding and showing views.
I would suggest modifying the
OnClickListener
to just record the selected row, then callnotifyDataSetChanged()
. This will cause the ListView to redraw its items by calling the adapter. Therefore, you just need to check this value ingetView()
to determine whether the "colorful" view should be visible or not.So the updated code would be something like:
That way, you ensure that only one view will be highlighted (while also simplifying your code).