android listview item value - android

I want to know how to get the listview item value like this
item1
item2
I want to get first item value item1, like this code
if (listview part of value == item1)
{
// do something
}
Can anyone help me to solve this problem?

Your question is very sparse on details so I'll take my best guess at interpreting it (consider expanding your question).
You want to get an item from the listadapter at a given position, yes? If so...
yourListView.getAdapter().getItem(position);
See also:
http://developer.android.com/reference/android/widget/ListView.html#getAdapter()
http://developer.android.com/reference/android/widget/ListAdapter.html
http://developer.android.com/reference/android/widget/Adapter.html

I'm trying to guess your question.
You want, maybe, that when user clicks on an item execute some code.
If this is your case then you can do in this way:
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parentAdapter, View view, int position,
long id) {
// do your code here
}
}
where lv is your Listview. If you want to have more information go to my blog here

Related

Calling method from Activity inside an adapter

Hi guys this is rather a silly question because i'm confused. what i'm trying to do is to call a method(share to facebook) from an activity inside my adapter.
this is the illustration:
Activity:
postToFacebook(Pets dog);
listview that holds list of dogs.
Adapter:
adapter in which i use to create custom view for better looking.
problem is that i want to set an onLongClickListener on the LinearLayout that holds all the dog's info (this way user can hold any part of the info) to be shared to facebook.
is there a way to do this?
thanks a lot!
If I understand your question you are complety on bad way
In your activity you have an ArrayList (or List) with Dogs information
Something like
ArrayList<Pets> listDogs;
now, you should do this (myListView is your ListView obj)
myListView.setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int pos, long id) {
postToFacebook(listDogs.get(pos));
return true;
}
});
In this way you get the position of element selected and call your method from activity and not from adapter.
listDogs.get(pos)
this line return the element of the row selected.

Android:Checked Listview Item Goes at Last

My ListView looks like this:
[CheckBox] [TextView]
My question is, how can I change the item position when CheckBox is checked? I mean to say, if the user checked any item from ListView, then the checked item goes to the end, so its position changes from current to last.
Thanks in Advance.
Use Custom adapter for listView. here is an example. Now from your ListActivity class
final ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
HashMap<String, Object> map = (HashMap<String, Object>)
lv.getItemAtPosition(position);
// get the item Position from here and make the nacessary changes
// and update the listview again.
}
Without posting any code, this will have to be a brief overview of the pseudo steps you need to take
You simply need to update the ordering of your data set being used by your adapter (usually an arraylist or array of objects), and then called
notifyDataSetChanged()
on your adapter.
So in your case, you want to take the element at the position that was clicked, and put it at the end of your arrayList of objects.
If you post some code, you may get more detailed answers however
Here is the step you can follow. I have not tested it but you can try this.
// your value array you are binding with listview
private final String[] values;
// put default value to "n". size will be the same as for values array.
private final String[] valuesChecked;
onClick of Checkbox Listview
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//get checkbox is checked or not
if it is checked then{
valuesChecked[position]="y";
}else{
valuesChecked[position]="n";
}
// short your values array by first "n" and after "y".
// call your adapter.notifyDataSetChanged();
}
Good Luck :)

Automatic click on ListView if has only one item

I am trying to search for some data in Mysql database, then display it Listview
When more than 1 result is found, clicking on any one of them will take you to its respective details page.
But.. I am looking to go to details page directly if there is only one result in list view. Put simply, how would I call onItemClick on the list view automatically when there is only one item in the list.
My Listview's onClickItemListener looks like this:
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// when a student is selected then the following methods are called
DetailsFragment.setLUNCH_TYPE(searchResult.get(position).lunchtype);
Log.d("Lunch type selected", ""+DetailsFragment.getLUNCH_TYPE());
((HomeActivity) getActivity()).startStudentSale(searchResult
.get(position).name.toString(),searchResult
.get(position).isStudent,searchResult.get(position).id);
// it was comming null here when running this this need to find out why
// ((HomeActivity)getActivity()).setmSaleType(SALE_TYPE.SEARCHED_STUDENT);
}
When you know the count of the resultSet, say cnt and if it is 1, i.e.
if(cnt==1){
Intent intent=new Intent(getApplicationContext(),DetailActivity.class);
startActivity(intent);
}

Working with LazyList by Fedor onitemclick implementation

I will sound stupid to most of you by asking this, but I haven't been able to figure it out on my own and can't pass to my next task until I found an answer.
So far I have downloaded the LazyList project from https://github.com/thest1/LazyList made by Fedor, I'm trying to understand how it works so I can implement it on my own project. My problem is that I don't know where to implement the onitemclicklistener part:
AdapterView.OnItemClickListener onitemClick = new AdapterView.OnItemClickListener()public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
noteId= //the item id from the list
Toast.makeText(getApplicationContext(), noteId, 2000).show();
}
I have try to add it on my mainActivity but then I can't find how to connect to my LazyAdapter to find the item id to be displayed in a toast. My project will contain some other information in the list (as a table several columns) so I want to be able to access an specific columns from that rows using the item id. and testing with a simple toast will help.
Thanks, I hope you don't laugh to much and help me a little.
I think you are going about it in the wrong manner: AdapterView.OnItemClickListener is an interface and the onItemClick() method takes four parameters... I assume this is what you are trying to do:
ListView listView = (ListView) findViewById(R.id.list);
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(), id + "", 2000).show();
}
});

Android ListView: get data index of visible item

I have an Android ListView created with a SimpleAdapter that has more items in it than fit in the screen. After the list has been scrolled, I need to get the position in the data model of the first visible item in the list.
Basically I want a function like: listView.getChildAt(0).getPositionInDataModel().
Adapter has a few functions in it, like getItemId(position) that looked useful; however, the SimpleAdapter implementation just returns the passed in position, not a row id like I'd hoped.
A brute force solution would be to get the View at index 0, and compare it to the view for each item in the adapter. However, there doesn't seem to be an easy way to get the view for a particular position from the adapter.
Anyone have any thoughts?
It's very easy. Just use ListView.getFirstVisiblePosition() + indexYouWant. For instance, to get the position in the adapter of the 2nd child displayed in the ListView, just use getFirstVisiblePosition() + 1.
No need for all the scary stuff shown in the reply above :)
listView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> parent,View view, int pos, long id)
{
AisleId= parent.getSelectedItemId();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
In this we will get list item Id parent.getSelectedItemId();
Simply use the getPositionForView(View) (see documentation). The main advantage of this method is it also works with descendant Views of an item.

Categories

Resources