ListView modify other view from specific view - android

I have a ListView that contains 5 rows with 5 button:
When I click on first button, I replace it text with "B".
Then, I click on second button, I replace it text with "B", but how can I replace the previous clicked button (the first) with "A"?
This is what really happen:
This is my code for the button click:
private int resource;
private LayoutInflater inflater;
ViewHolder holder;
private Context ctx;
public RingtoneAdapter(Context context, int resourceId, List<Ringtone> objects) {
super(context, resourceId, objects);
this.ctx = context;
resource = resourceId;
inflater = LayoutInflater.from(context);
}
public View getView(final int position, View v, ViewGroup parent) {
// Recuperiamo l'oggetti che dobbiamo inserire a questa posizione
final Ringtone ringtone = getItem(position);
if (v == null) {
v = inflater.inflate(resource, parent, false);
holder = new ViewHolder();
holder.btnPlay = (Button) v.findViewById(R.id.btnPlay);
v.setTag(holder);
} else {
holder = (ViewHolder) v.getTag();
}
holder.btnPlay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Button btnadd = (Button) view;
btnadd.setText("B");
notifyDataSetChanged();
}
});
return v;
}
private static class ViewHolder {
Button btnPlay;
TextView txtTitolo;
TextView txtCategoria;
}

If I'm reading this correctly, you only want a single position to be 'selected' at a time. If that's the case, you could keep a reference to the selected position, and use the Adapter's functionality instead of having multiple buttons know about each other. e.g.
private int selectedPosition = -1;
#Override
public View getView(final int position, View convertView, ViewGroup parent){
// ... Your ViewHolder / Ringtone initialization
if(position == selectedPosition)
holder.btnPlay.setText("B");
else holder.btnPlay.setText("A");
holder.btnPlay.setOnClickListener(new View.OnClickListener(){
if(holder.btnPlay.getText().toString().equals("A"))
selectedPosition = position;
else selectedPosition = -1;
notifyDataSetChanged();
});
return convertView;
}
The key here is allowing notifyDataSetChanged() to internally call your getView method, which then really only needs to know how to render the data in its current state, instead of dealing with the complex logic required to map between multiple Views.

you need to reset the items which were not clicked,
btn.setText("B"); // if the item was clicked
btn.setText("A"); // for all other items
what you can do is, cycle through all elements setting the text for button like this btn.setText("A"), once this is done call btn.setText("B")
you should be using OnItemClickListener on the listview, it is easy that way, as you are setting the value in view holder, it does not give you access to the other items in the listview, but by using OnItemClickListener you can cover all the elements in the listview once you update things as you like call notifyDataSetChanged(), As this can be done there is another way to look at the problem
ListView listView = (ListView) findViewById(R.id.ListViewTestSevenActivity_listView);
ArrayList<Item> items = new ArrayList<>();
for (int i = 0; i < 5; i++) {
Item item = new Item("A");
items.add(item);
}
MyAdapter adapter = new MyAdapter(getApplicationContext(), R.layout.simple_list_item_1, items);
listView.setAdapter(adapter);
This will take care of initialization and rest of the code as you want it to work. code is also available on github
private class MyAdapter extends ArrayAdapter {
private Context a_context;
private ArrayList<Item> items;
private LayoutInflater a_layoutInflater;
public MyAdapter(Context context, int resource, ArrayList<Item> items) {
super(context, resource, items);
this.a_context = context;
this.items = items;
a_layoutInflater = LayoutInflater.from(this.a_context);
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View row = convertView;
ViewHolder holder = null;
if (row == null) {
row = a_layoutInflater.inflate(R.layout.single_item_listview_seven, parent, false);
holder = new ViewHolder();
holder.button = (Button) row.findViewById(R.id.ListViewTestSevenActivity_text_button);
row.setTag(holder);
} else {
holder = (ViewHolder) row.getTag();
}
final Item item = items.get(position);
System.out.println("" + item.getText());
holder.button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
for (Item current_item : items) {
current_item.setText("A");
}
item.setText("B");
notifyDataSetChanged();
}
});
holder.button.setText("" + item.getText());
return row;
}
private class ViewHolder {
Button button;
}
}
private class Item {
String text;
public Item(String text) {
this.text = text;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}

Related

Using getTag and setTag to hold ListItem Values

I am learning about getTag and setTag. I need explanation about these methods and want to know how they work.
I have made a simple CustomListView having two buttons and one textview.
Buttons are add and subtract which increment or decrements the value of counter but the problem is that only the last item value is changing. Please give me a solution as well as a good explanation
Thanks .
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_items, null);
add = (Button) convertView.findViewById(R.id.add);
sub = (Button) convertView.findViewById(R.id.sub);
textView = (TextView) convertView.findViewById(R.id.numberTV);
convertView.setTag(new ViewHolder(add, sub, textView , count));
}
holder = (ViewHolder) convertView.getTag();
textView = holder.textView;
count = holder.counter;
add = holder.add;
sub = holder.sub;
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
count +=1;
textView.setText(String.valueOf(count));
}
});
sub.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
count -=1;
textView.setText(String.valueOf(count));
}
});
return convertView;
}
}
class ViewHolder {
Button add, sub;
TextView textView;
int counter = 0;
public ViewHolder(Button add, Button sub, TextView textView , int counter) {
this.add = add;
this.sub = sub;
this.counter = counter;
this.textView = textView;
}
}
In your custom listview adapter what you are trying to implement is recycling of the single item (list_items) showed in the Listview. and because of which you require to use the getTag and setTag methods to that View.
So why do you require these method in the first place...
You are trying to recycle the list_items and view's inside it, to do that you are making the ViewHolder which will hold the inner references of views inside the list_items.
You don't want to call inflater and referencing on Views every-time getView is called so you save the ViewHolder's instance to the View, which will be used again without re-inflating the list_items and references inside it.
By setTag() you can attach as Object to the View
By getTag() you can get back the Object attached to the View
So this way you are recycling the same initialization of your list_items every time getView() is called
Seems like a lot of work, Use RecyclerView.
your solution for the code... you may check the code on gitHub
public class ListViewTestFiveActivity extends AppCompatActivity{
private ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_view_test_five);
initializeUI();
}
private void initializeUI() {
listView = (ListView)findViewById(R.id.ListViewTestFiveActivity_listView);
ArrayList<Item> items = new ArrayList<>();
for (int i = 0; i < 15; i++) {
Item item = new Item();
item.setCount(i);
items.add(item);
}
MyAdapter adapter = new MyAdapter(getApplicationContext(), R.layout.single_item_listview_five, items);
listView.setAdapter(adapter);
}
private class MyAdapter extends ArrayAdapter{
private ArrayList<Item> items;
private Context a_context;
private LayoutInflater a_layoutInflater;
public MyAdapter(Context context, int resource, ArrayList<Item> items) {
super(context, resource, items);
this.a_context = context;
this.items = items;
a_layoutInflater = LayoutInflater.from(this.a_context);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ViewHolder holder = null;
if (row == null) {
row = a_layoutInflater.inflate(R.layout.single_item_listview_five, parent, false);
holder = new ViewHolder();
holder.plus = (Button) row.findViewById(R.id.ListViewTestFiveActivity_plus_button);
holder.textView = (TextView) row.findViewById(R.id.ListViewTestFiveActivity_count_textView4);
holder.minus = (Button) row.findViewById(R.id.ListViewTestFiveActivity_minus_button);
row.setTag(holder);
} else {
holder = (ViewHolder) row.getTag();
}
final Item item = items.get(position);
holder.textView.setText("" + item.getCount());
holder.plus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int increase_count =item.getCount();
item.setCount(++increase_count);
notifyDataSetChanged();
}
});
holder.minus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int decrease_count =item.getCount();
item.setCount(--decrease_count );
notifyDataSetChanged();
}
});
return row;
}
private class ViewHolder{
Button plus;
TextView textView;
TextView minus;
}
}
private class Item {
int count;
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
}
}
Output

My ListView not working properly

I am getting one problem while adding ListView in layout. I have implemented one ListView in one page where we get list of items, in that when we click on some ListMember it change its color and again clicking on it will change it to previous color.Now imagine because of Item height one screen can hold maximum 5 List items,for next member to see you need to scroll down.
Now imagine List members are
Item 1
Item 2
Item 3
Item 4
Item 5
Item 6
Item 7
Among these use can only see 5 items at a time, now when I click on 'Item 1'(first member of first five members) its color is changing(say WHITE TO GREEN) but when I scroll down I see 'Item 6'(first member of first five members) is also changed its color(to GREEN),and when I click on 'Item 6' ,this time setOnItemClickListener for that member is getting actually triggered and trying changing its color to what it already changed.
this is code for setOnItemClickListener :
productList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
Log.i("imIn","Item Clicked");
v.animate();
if(listClicked[position]==0)
{
Log.i("***After*** ","Cyan Set ON");
v.setBackgroundColor(Color.parseColor("GREEN"));
listClicked[position]=1;
}
else if(listClicked[position]==1){
Log.i("***After*** ","Cyan Set OFF");
v.setBackgroundColor(Color.parseColor("WHITE"));
listClicked[position]=0;
}
}
});
AfterEdit:: this is my adapter
public class ProductListBaseAdapter extends BaseAdapter {
SharedPreferences sharedpreferences;
private static ArrayList<Product> searchArrayList;
private LayoutInflater mInflater;
ArrayList<TotalSelectedProduct> selectedProducts=new ArrayList<>();
final int[] listClicked;
public ProductListBaseAdapter(Context context, ArrayList<Product> totalProducts, int[] ClickedList) {
searchArrayList =totalProducts;
mInflater = LayoutInflater.from(context);
listClicked=ClickedList;
}
public int getCount() {
return searchArrayList.size();
}
public Object getItem(int position) {
return searchArrayList.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.item_list, null);
holder = new ViewHolder();
holder.txtItem = (TextView) convertView.findViewById(R.id.item_name);
holder.edit=(Button)convertView.findViewById(R.id.edit);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
/** I have tried inserting onClickListener in adapter also .but resulting same
*
holder.txtItem.setText(searchArrayList.get(position).getItemName());
final View.OnClickListener makeListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
v.animate();
if(listClicked[position]==0)
{
Log.i("***After*** ","Cyan Set ON");
v.setBackgroundColor(Color.parseColor("#ff2dbeff"));
listClicked[position]=1;
}
else if(listClicked[position]==1){
Log.i("***After*** ","Cyan Set OFF");
v.setBackgroundColor(Color.parseColor("#009933"));
listClicked[position]=0;
}
}
};
holder.txtItem.setOnClickListener(makeListener); */
return convertView;
}
static class ViewHolder {
TextView txtItem;
Button edit;
}
}
Why this is happening ?
to do what you want, you have to add an adapter to your listview and there control the on click method for each item.
UPDATE WITH EXAMPLE
public class ProductAdapter extends ArrayAdapter<Product> {
public PProductAdapter(Activity activity,
ArrayList<Product> products) {
super(activity, R.layout.item_product, products);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final Product p = getItem(position);
ViewHolder viewHolder;
if (convertView == null) {
viewHolder = new ViewHolder();
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.item_product, parent,
false);
viewHolder.name = (TextView) convertView.findViewById(R.id.tvName);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
convertView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
p.checked = !p.checked;
if (p.checked)
v.setBackgroundColor(Color.parseColor("#ff2dbeff"));
else
v.setBackgroundColor(Color.parseColor("#009933"));
}
});
viewHolder.name.setText(p.name);
return convertView;
}
private class ViewHolder {
TextView name;
}
}
public class Product{
public String name;
public boolean checked;
Product() {
name = "dummy name";
checked = false;
}
}
Do as following
1 Handle click event in your adapter not Activity
2 View for your click can be parent layout in item_list.xml
3 Don't use final int[] listClicked instead have boolean variable in Product class for example isChecked.
4 Set and unset isChecked on click and set background color

ListView item selection issue. Deselect others when one selected

I have a custom listview: http://i.stack.imgur.com/oBCCG.png
Up on clicking an item of 'ListView' the 'ImageView' with that "mark" becomes visible.
But the problem is that I need only ONE item to be marked. How can I deselect all others items up on click?
CustomList adapter:
private class TimeZoneItemAdapter extends ArrayAdapter<String> {
private Activity myContext;
private ArrayList<String> datas;
public TimeZoneItemAdapter(Context context, int textViewResourceId, ArrayList<String> objects) {
super(context, textViewResourceId, objects);
myContext = (Activity) context;
datas = objects;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder viewHolder;
if (convertView == null) {
LayoutInflater inflater = myContext.getLayoutInflater();
convertView = inflater.inflate(R.layout.listview_item, null);
viewHolder = new ViewHolder();
viewHolder.postNameView = (TextView) convertView.findViewById(R.id.listview_item);
viewHolder.postImageView = (ImageView) convertView.findViewById(R.id.image_mark);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.postNameView.setText(datas.get(position));
viewHolder.postNameView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//How to deselect all other items here?
if (viewHolder.postImageView.getVisibility() != View.VISIBLE) {
viewHolder.postImageView.setVisibility(View.VISIBLE);
} else {
viewHolder.postImageView.setVisibility(View.INVISIBLE);
}
}
});
return convertView;
}
class ViewHolder {
TextView postNameView;
ImageView postImageView;
}
}
setting the adapter:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_time_zone, container, false);
// Set the adapter
mListView = (ListView) view.findViewById(R.id.listView_timezones);
imageCheck = (ImageView) view.findViewById(R.id.image_mark);
mListView.setAdapter(new TimeZoneItemAdapter(
getActivity(),
R.layout.listview_item,
dataTimezones.getZonesList()
));
return view;
}
Thanks for any help!
You might want to keep track of the currently selected list view item position in a dedicated integer variable. Then you would only need to call notifyDataSetChanged() on the adapter each time an item is selected and set the ImageView as visible in getView() if yourSelectedPosition == position.
Also check out the setOnItemClickListener() method on ListView.
EDIT:
Here's some code.
First in the Activity attach a listener to the ListView:
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick (AdapterView<?> parent, View view, int position, long id) {
timeZoneItemAdapter.selectedPosition = position;
timeZoneItemAdapter.notifyDataSetChanged();
}
});
and the adapter should look something like this:
private class TimeZoneItemAdapter extends ArrayAdapter<String> {
private Activity myContext;
private ArrayList<String> datas;
public int selectedPosition = -1;
public TimeZoneItemAdapter(Context context, int textViewResourceId, ArrayList<String> objects) {
super(context, textViewResourceId, objects);
myContext = (Activity) context;
datas = objects;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null)
convertView = myContext.getLayoutInflater().inflate(R.layout.listview_item, null);
TextView postNameView = (TextView) convertView.findViewById(R.id.listview_item);
ImageView imageMark = (ImageView) convertView.findViewById(R.id.image_mark);
postNameView.setText(datas.get(position));
imageMark.setVisibility(selectedPosition == position ? View.VISIBLE : View.GONE);
return convertView;
}
}
This solution gets rid of the ViewHolder, but would it work for you?

Android - Gridview, custom layout onclicklistener

I have a GridView with two TextViews inside it, When the GridView is populated, an OnClickListener is set which returns the position of the item that was selected. I want to trigger a method when one of the TextViews is selected.
Is this possible? If yes, how do I set this up?
EDIT 3:
Inside my activity whch populated the GridView: I retrieve a String-Array from my Strings.xml, a for loop examines each item inside the Array and searches for a condition based on the Item's name inside the SharedPreferences, this for loop is solely for counting how many "true" conditions there are, so it retrieves a int saved inside the count. Then a new String[] is created, this required an exact length to be given before items can be added to it, so I check count if it's more than 0, it will give the String[] a length of count and then another for loop will add each true to the String[] list that we just created. If count is 0 (no true conditions found in the first for loop) then only 1 Item is added to the String[] and is given the value "No Favourites Added".
Then you have the GridView's OnItemClickListener().
String s[] = getResources().getStringArray(R.array.FullList);
int count = 0;
for(int i = 0; i < s.length; i++) {
SharedPreferences sP = getActivity().getSharedPreferences("fav", MODE_PRIVATE);
Boolean b = sP.getBoolean(s[i], false);
if (b == true) {
count++;
}
}
String[] newList;
if (count > 0) {
newList = new String[count];
count = 0;
for(int i = 0; i < s.length; i++) {
SharedPreferences sP = getActivity().getSharedPreferences("fav", MODE_PRIVATE);
Boolean b = sP.getBoolean(s[i], false);
if (b == true) {
newList[count] = s[i];
count++;
}
}
} else {
newList = new String[1];
newList[0] = "No favourites added";
}
GridView FavGV = (GridView) getActivity().findViewById(R.id.sexp_fav);
FavGV.setAdapter(new Tab01_FavAdapter(getActivity(), newList));
FavGV.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView arg0,
View arg1, int position, long arg3) {
//Intent i = new Intent(getActivity(), PosPreview_Gestures.class);
//i.putExtra("position", position);
//startActivity(i);
}
});
So that's the code inside the Activity which populates the GridView. The Adapter in it's original, functioning form: This simply populates the GridView with Favourite Items (their names from the String[]) and adds a TextView with "Remove" which when pressed, shows a Toast: "Remove".
public class Tab01_FavAdapter extends BaseAdapter {
private Context mContext;
private LayoutInflater mInflator;
String mEntries[];
public Tab01_FavAdapter (Context c, String[] entries) {
mContext = c;
mInflator = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mEntries = entries;
}
#Override
public int getCount() {
return mEntries.length;
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null) {
convertView = mInflator.inflate(R.layout.favitemlayout, parent, false);
}
TextView tx = (TextView) convertView.findViewById(R.id.favgridremoveitem);
OnClickListener oCL = new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(mContext,"Remove",Toast.LENGTH_SHORT).show();
}
};
tx.setOnClickListener(oCL);
return convertView;
}
}
I am assuming that you are using a custom adapter for populating this GridView, and passing the Context as an argument to the constructor.
In the custom adapter, you should add onClickListeners to the TextViews. Using the context, you can call methods from your activity:
((CallingActivityName)context).methodYouWishToCall(parameters);
This would go inside the onClickListeners.
Edit: Added some code:
public class MyGridAdapter extends BaseAdapter {
private final List<MyObjectClass> mEntries;
private final LayoutInflater mInflater;
private final Context mContext;
public static class ViewHolder {
public TextView tx;
}
public MyGridAdapter(CallingActivityName context, List<MyObjectClass> entries) {
super();
mEntries = entries;
mContext = context;
mInflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return mEntries.size();
}
#Override
public Object getItem(int position) {
return mEntries.get(position);
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
convertView = mInflator.inflate(R.layout.favitemlayout, parent, false);
holder = new ViewHolder();
holder.tx = (TextView) convertView
.findViewById(R.id.favgridremoveitem);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
final MyObjectClass info = mEntries.get(position);
holder.tx.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
((CallingActivityName)mContext).favRemove(info);
notifyDataSetChanged();
}
});
return convertView;
}
}
So, CallingActivityName is the name of the Activity where you initiate the adapter and where the method you need to call resides. info is the object held at position position of the gridview. MyObjectClass is the class name of the objects in the List mEntries.

What better way to implement OnClickListner on row Item in List Adabpter

I have implemented custom adapter and having multiple views (ImageView, TextView) in List row. I want to set listener on each of these.
This what I have done
if (containerRow == null) {
viewHolder.item.setOnClickListener(new OnImageViewClickListener(position, context, viewHolder));
}
Now this gives me wrong position in OnImageViewClickListener as viewHolder.item.setOnClickListener is getting called only once when (containerRow == null). If I do this in else part than lot of listener object creation for multiple items (ImageView, TextView) and that number of rows (Am I right?)
As per my understanding I cannot achieve this is in ListView's setOnItemClickListener as there I cannot get the item of the on which user has clicked.
Please suggest me some neat way to implement listener on these row items and to receive right position.
Set the position as a TAG for each view and set the listener on each clickable view
Define the listener as inner class, and get the tag from the view
if (containerRow == null) {
....
....
viewHolder.txv.setOnClickListener(clickListener);
viewHolder.img.setOnClickListener(clickListener);
.....
.....
}else{
viewHolder = containerRow.getTag();
}
viewHolder.txv.setTag(position);
viewHolder.img.setTag(position);
The OnClickListener:
private View.OnClickListener clickListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
Integer position = (Integer) v.getTag();
// you have the correct position
switch(v.getId()){
......
......
}
}
};
This seems to work where ListItem is a POJO containing the data for the list item...
public class ListItemAdapter extends ArrayAdapter<ListItem>
{
private static final String TAG = "ListItemAdapter";
private Activity mContext;
private int mLayoutResourceId;
private List<ListItem> mItems = null;
static class ViewHolder
{
public TextView text1;
public TextView text2;
public ImageView image;
}
public ListItemAdapter(Activity context, int textViewResourceId, List<ListItem> items)
{
super(context, textViewResourceId, items);
mContext = context;
mLayoutResourceId = textViewResourceId;
mItems = items;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
View v = convertView;
if (v == null)
{
LayoutInflater inflater = mContext.getLayoutInflater();
v = inflater.inflate(mLayoutResourceId, parent, false);
ViewHolder viewHolder = new ViewHolder();
viewHolder.text1 = (TextView) v.findViewById(android.R.id.text1);
viewHolder.text1.setOnClickListener(new ItemClickListener(position));
viewHolder.text2 = (TextView) v.findViewById(android.R.id.text2);
viewHolder.text2.setOnClickListener(new ItemClickListener(position));
viewHolder.image = (ImageView) v.findViewById(android.R.id.icon);
viewHolder.image.setOnClickListener(new ItemClickListener(position));
v.setTag(viewHolder);
}
ListItem item = mItems.get(position);
Log.v(TAG, item.toString());
ViewHolder holder = (ViewHolder) v.getTag();
String text = item.getText1();
holder.text1.setText(text);
text = item.getText2();
holder.text2.setText(text);
Drawable img = item.getImage();
Log.v(TAG, "image : " + img);
holder.image.setImageDrawable(img);
return v;
}
class ItemClickListener implements View.OnClickListener
{
int position;
public ItemClickListener(int pos)
{
this.position = pos;
}
public void onClick(View v)
{
Toast.makeText(mContext, "Item clicked in row - " + position, Toast.LENGTH_SHORT).show();
};
}
}

Categories

Resources