Skip to content Skip to sidebar Skip to footer

Android - How To Implement Getlistitemclick() In This Case?

When I click on an item from the list, I want a TextView inside it to change visibility from 'gone' to 'visible' and display the item position that was clicked. This should be done

Solution 1:

Unlike my previous answer I'll assume that you want each TextView to display different data, so let's add a new member to your adapter and appropriate getters and setters to your Adapter:

privateSparseArray<String> secondary = newSparseArray<String>();

publicStringgetSecondary(int position) {
    return secondary.get(position, "");
}
publicvoidsetSecondary(int position, String value) {
    secondary.put(position, value);
    notifyDataSetChanged();  // Updates the ViewGroup automatically!
}

SpareArrays are better at sets without a predictable index, but you can use a List, HashMap, etc if you want something different. Now tweak onListItemClick() to use the new methods:

protectedvoidonListItemClick(ListView l, View v, int position, long id) {
    StringclickedPosition="Clicked position = " + position;
    mAdapter.setSecondary(position, clickedPosition);
}

Lastly update getView() to show textClickedPosition only when secondary has data:

TextView textView = (TextView) view.findViewById(R.id.textPosition);
textView.setText(event);

textView = (TextView) view.findViewById(R.id.textClickedPosition);
String string = getSecondary(position);
if(!string.isEmpty()) {
    textView.setText(string);
    textView.setVisibility(View.VISIBLE);
}
else// You must hide the view otherwise you will see odd behavior for the recycle method
    textView.setVisibility(View.GONE);

return view;

On a final note, you really should watch the Google I/O lectures like Turbo Charge your UI they contain a wealth of knowledge!

Post a Comment for "Android - How To Implement Getlistitemclick() In This Case?"