Single item selection from list view - android

I am displaying a listview with a text view and image view. Image view is hidden. What i want is that when user selects an item from listview he sees the image in that listview item. But when he selects another item, then he should see, that image in that particular list view item. I am dng this,:::
final String[] TYPE = new String[] { "Movie Top 100", "TV Top 100", "All" };
ListView listview = (ListView) findViewById(R.id.typescreenlistview);
listview.setAdapter((new ArrayAdapter(this, R.layout.typelist, R.id.heading, TYPE)));
listview.setTextFilterEnabled(true);
listview.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView arg0, View arg1, int arg2, long arg3)
{
ImageView click = (ImageView) arg1.findViewById(R.id.click);
click.setImageResource(R.drawable.play);
}
});
What condition i should use??
Thanx in advance.

In your ListView, whenever a user clicks on an item, make the ImageView in that item visible, like so:
listview.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView arg0, View arg1, int arg2, long arg3)
{
ImageView click = (ImageView) arg1.findViewById(R.id.click);
click.setVisibility(View.VISIBLE);
}
});
Also, I would suggest using a customized adapter, so that in getView you can set those listview items that are not clicked to INVISIBLE. See Custom ListView items and adapters for a tutorial on using custom adapter for ListView

Related

How to obtain the text color of a single item within an Android ListView

My ListView contains items that are different text colors. I need to grab the color state of each item to to be saved within the onSaveInstanceState of my fragment.
ListView lvItems = (ListView) getActivity().findViewById(R.id.lvItems);
lvItemsArray.add("Testing1234");
lvItemsAdapter = new ListViewAdapter(getActivity(), lvItemsArray);
lvItems.setAdapter(lvItemsAdapter);
LitView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Log.d("TextView",""+arg1.TextView.getTextcolor());
});
Found it from another user on here, thanks.
View view = ListAdapter.getView(0, null, null);
TextView textView = (TextView) view.findViewById(R.id.myitemRow);
ColorStateList mlist = textView.getTextColors();
int color = mlist.getDefaultColor();

How I can delete the item from list view

I have a listview and a button in my layout file.
I'm adding items to listview on click of that button.
I need a way that make my item in the list disappear when I click the item.
set up an onitemclicklistener on you list view, remove the item when it is onclick and then refresh the list
mListView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
/* remove the item at position */
notifyDataSetChanged();
}
});

I can't select the correct row while using CheckedTextView in ListView

SimpleAdapter adapter = new SimpleAdapter(this, list, R.layout.contact_item, new String[] { "name" }, new int[] { R.id.name });
listView.setAdapter(adapter);
listView.setItemsCanFocus(false);
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
CheckedTextView checkedTextView = (CheckedTextView) view.findViewById(R.id.name);
checkedTextView.toggle();
System.out.println(checkedTextView.getText().toString());
}
});
While I click the first item in the ListView, but the CheckedTextView will selected the last item in the ListView, and the System.out is correct. I don't know why, and how can I fix it?
Views used in the actual rows of ListView instances are often recycled: typically the display layer will create as many View objects as there are visible rows and then will cycle through them. Selection of rows where you are using an embedded View are often tricky: when you click on a specific View within a row the text may be correct but the system may not know which row to select. Try calling setSelection with the position to force the right row to be selected.
EDIT: no guarantees but try doing this and see if it works.
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
CheckedTextView checkedTextView = (CheckedTextView) view.findViewById(R.id.name);
checkedTextView.toggle();
System.out.println(checkedTextView.getText().toString());
getListView().setSelection(position);
}
});

listview in android

I have a list view in which I show data. When I click on a row of the list view, I want to display data for that row from web services. How do I do that?
What you do is set the ListView's OnItemClickListener with the setOnItemClickListener method. You then obtain the "item object" for the clicked item with the getItemAtPosition method:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parentView, View itemView, int position, long id) {
ListView listView = (ListView) parentView;
Object o = listView.getItemAtPosition(position);
//...
}
});
The type of the "item object" (o in the example above) depends on the ListAdapter that the list view is using. One common choice is CursorAdapter, in which case the type of the item object is conveniently the Cursor to the record corresponding to the list item that was clicked.
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
Toast.LENGTH_SHORT).show();
}
});

display item of listview

I have a list view having several items and it is multichoice list.
I want to display the checked item of list view.
how i can do this.can anyone help me???
Try this
m_cObjList.setOnItemClickListener( new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// This will highlight the select view
m_cObjAdapter.setSelected(arg2);
}
});

Categories

Resources