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

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

Implementing OnClickListener to list items of a list

I used list view to list the items of my SQLite database as follows:
listView = (ListView) findViewById(R.id.listView1);
db.open();
Cursor c = db.getAllHistory();
ArrayList<String> temparr = new ArrayList<String>();
if (c.moveToFirst()) {
do {
temparr.add(c.getString(0)+'\t'+c.getString(1)+'\t'+c.getString(2)+'\t'+c.getString(3)+'\t'+c.getString(4)+'\t');
} while(c.moveToNext());
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, temparr);
listView.setAdapter(adapter);
} else {
db.close();
}
Now I want to access the specific list item by using OnItemClickListener. How should i do it?
To get your perticular data-
Implement setOnItemClickListener()-
and position will return list's item position number:-
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
String yourData = temparr.get(position);
}
});
Just call listView.setOnItemClickListener() with your implementation of the listener.
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
// your code
// Toast.makeText(context,temparr.get(position),Toast.LENGTH_SHORT).show();
}
});
parent -> The AdapterView where the click happened.
view -> The view within the AdapterView that was clicked (this will be a view provided by the adapter)
position -> The position of the view in the adapter.
id->The row id of the item that was clicked.
You can set the listener like this:
Put this code in onCreate()
TextView disp = (TextView) findViewById(R.id.textView1);
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String temp = (String) listView.getItemAtPosition(position);
disp.setText(temp);// display on screen
}
});
EDIT:
As you want to display the listItem on screen in a permenant sort of way rather than displaying a toast, the easiest way that you can do this is by adding a TextView in your layout. See the updated code
Hope this helps.

how to get object from arraylist in setOnItemClickListener in listview? [duplicate]

I have added arraylist in arrayadapter which contains objects each consists of two elements/items, I have successfully set that adapter for setListAdapter, now i want to get those items in setOnItemClickListener of listview.
here is my code
TweetListAdaptor adaptor = new TweetListAdaptor(this,R.layout.list_item, tweets);
setListAdapter(adaptor);
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view,int position, long id)
{
//here i want to get the items
}
});
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
int color = parent.getAdapter().getItem(position);
}
public void onItemClick(AdapterView<?> parent, View view,int position, long id){
something = tweets[position];
}
You want to get the items and do what with them?
For example, you can make a Toast message like this.
public void onItemClick(AdapterView<?> parent, View view,int position, long id)
{
Toast.makeText(getApplicationContext(), tweets[position], Toast.LENGTH_SHORT).show();
}
Hope this helps.
and if you have the listview ready with all datas and want get a value of a objet only use:
ViewGroup row = (ViewGroup) listprod.getChildAt(0);
TextView tvTest = (TextView) row.findViewById(R.id.textnomprod);
Where my listview is "listprod" and i want get the value in the position 0, where textnomprod is my objet and im saving in my variable tvTest
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapter, View view, int position, long id) {
Windows clickedObject = adapter.get(position);
}
}
Consider in the example above,
object used in the listview is named Windows,
the object of the item clicked in the listview is named clickedObject,
the arraylist used is called adapter.
Also, make sure to prefix your ArrayList with final.
I solved this problem by using its adapter that was set to it.
TweetListAdaptor adaptor = new TweetListAdaptor(this,R.layout.list_item, tweets);
setListAdapter(adaptor);
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view,int position, long id)
{
//here i want to get the items
adaptor.getItem(position); // this is your object
}
});
Just keep in mind that the adapter must be initialized and set to the ListView.
In this way, you can access the properties of the object you want.
getListView().getItemAtPosition(position) will return an Object in your TweetListAdaptor

how to get object from listview in setOnItemClickListener in android?

I have added arraylist in arrayadapter which contains objects each consists of two elements/items, I have successfully set that adapter for setListAdapter, now i want to get those items in setOnItemClickListener of listview.
here is my code
TweetListAdaptor adaptor = new TweetListAdaptor(this,R.layout.list_item, tweets);
setListAdapter(adaptor);
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view,int position, long id)
{
//here i want to get the items
}
});
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
int color = parent.getAdapter().getItem(position);
}
public void onItemClick(AdapterView<?> parent, View view,int position, long id){
something = tweets[position];
}
You want to get the items and do what with them?
For example, you can make a Toast message like this.
public void onItemClick(AdapterView<?> parent, View view,int position, long id)
{
Toast.makeText(getApplicationContext(), tweets[position], Toast.LENGTH_SHORT).show();
}
Hope this helps.
and if you have the listview ready with all datas and want get a value of a objet only use:
ViewGroup row = (ViewGroup) listprod.getChildAt(0);
TextView tvTest = (TextView) row.findViewById(R.id.textnomprod);
Where my listview is "listprod" and i want get the value in the position 0, where textnomprod is my objet and im saving in my variable tvTest
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapter, View view, int position, long id) {
Windows clickedObject = adapter.get(position);
}
}
Consider in the example above,
object used in the listview is named Windows,
the object of the item clicked in the listview is named clickedObject,
the arraylist used is called adapter.
Also, make sure to prefix your ArrayList with final.
I solved this problem by using its adapter that was set to it.
TweetListAdaptor adaptor = new TweetListAdaptor(this,R.layout.list_item, tweets);
setListAdapter(adaptor);
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view,int position, long id)
{
//here i want to get the items
adaptor.getItem(position); // this is your object
}
});
Just keep in mind that the adapter must be initialized and set to the ListView.
In this way, you can access the properties of the object you want.
getListView().getItemAtPosition(position) will return an Object in your TweetListAdaptor

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