LebGeeks

A community for technology geeks in Lebanon.

You are not logged in.

#1 March 2 2014

guigo170
Member

working with setTag on android to choose a listview item

hello i have implement a layout containing a list with a toggle button an imageview and a textview on each row. i want to change the image of the row with the pressed button
i am stuck on the code and unsure how to fix it

		setListAdapter(new MyAdapter(this, android.R.layout.simple_list_item_1,
                R.id.textView1,
                (String[]) myArr.toArray(new String[]{})));
	}
	
		 class MyAdapter extends ArrayAdapter<String>{
			 	
				private int which;

				public MyAdapter(Context context, int resource,
					int textViewResourceId, String[] string) {
					super(context, resource, textViewResourceId, string);
					// TODO Auto-generated constructor stub
				}
				
				@Override
				public View getView(int position, View convertView, ViewGroup parent) {
					LayoutInflater inflater =  (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
					View row = inflater.inflate(R.layout.custom_view, parent,false);			
					final TextView tv = (TextView) row.findViewById(R.id.textView1);
					final ImageView iv = (ImageView) row.findViewById(R.id.imageView1);
					final ToggleButton tb = (ToggleButton) row.findViewById(R.id.toggleButton1);
					
					tv.setTag(position);
					iv.setTag(position);
					tb.setTag(position);
					
					tv.setText(myArr.get(position).toString());
					
					tb.setOnClickListener(new View.OnClickListener() {

			            @Override
			            public void onClick(View v) {
			            	if(tb.isChecked()){
			            		which=(Integer) tb.getTag();
			            		
			            		
			            	}else{
			            		
			            	}
			            	
			              
			            }
			        });
					
					return row;
				}
		}
		 
}

Offline

Board footer