How I can delete the item from list view - android

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();
}
});

Related

how to remove a list item of ListView from onitemclick listener

i know how to remove a list item by using list adapter.But i want to remove a list item from a actvity which showing the list view.i am using onitemClick Listener for getting data from list item.after getting data i need to remove that item from a list view.how to do that?
You can try to this code..
ListView lv = (ListView) findViewById(R.id.lv);
TestAdapter adpter = new TestAdapter(Test.this, arralist);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
#Override
public void onItemClick (AdapterView < ? > adapterView, View view,int position, long l){
arralist.remove(position);
adapter.notifyDataSetChanged();
}
}
);
While not best practice you can also call the change on the adapter.
view.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
adapter.remove(adapter.getItem(position));
}
});

Android listview OnItemClickListener getting -1

when i am clicking on listview item i am getting item position as -1. This is my code.
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
int index = listView.getSelectedItemPosition();
System.out.println("benbenarji:"+index);
}
});
Value -1 is for INVALID_POSITION, this means there is no selection on the list. You can use position from on onItemClick to learn which item was clicked.

How Can I Load the Questions for different subject in 'listview' OnClick List Item

How Can I Load the Questions for different subject in listview item's click. For e.g; if Android is clicked it should load Android MCQ in another Activity
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
T item = parent.getAdapter().getItem(position);
switch (item) { //or if, else
// logic here
}
}
});

Single item selection from list view

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

Arrays and ListView in Android

I have a ListView that displays every item in an array called, "Facts_Array". What I would like to do is display the count of the item clicked on. Right now I have this:
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
Toast.makeText(getApplicationContext(), ((TextView) view).getText(),Toast.LENGTH_SHORT).show();
}
When an item is clicked in the listview, it displays it using Toast. What I would like to change is what the toast displays. I would like it to find the number in which the item comes. If I click on the second item in the list, I want it to display number 2. Thanks!
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
Toast.makeText(getApplicationContext(), ":D "+(position+1),Toast.LENGTH_SHORT).show();
}

Categories

Resources