Retrieve data from SQLite in a ListView - android

Hi guys so I have this doubt. I've created an app that stores data in a SQLite and its displayed on a Listview in the Main Activity. What I'm trying to do is once I click one item from the list it should open an image from sd card. So my question is how can I use onClick here since I don't know the longitude of the list? And may I include the onclick for the list in the switch I've got?
UPDATED:
I forgot to say that that I would like to open an image stored in the sdcard when I click a item of the list.
That's what I've got for now:
Contact_listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Cursor cursor = (Cursor) parent.getItemAtPosition(position);
String lat = cursor.getString(cursor.getColumnIndexOrThrow("lat"));
Toast.makeText(getApplicationContext(), lat, Toast.LENGTH_SHORT).show();
}
});
All comments or suggestions are welcomed. Thanks!

Use setOnItemClickListener() method:
Contact_listview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, final int selectedIndex, long arg3) {
}
);

Related

ListView example program which switches to new activity

Could anyone possibly give me an entire example for demonstrating a ListView.
Each item in listview needs to fire a new activity.
For ex, if I use 'Subjects' as content of ListView then after pressing particular a subject from listview , a new screen with its title and some content of that subject gets displayed.
I know this is too much to ask, but i have been looking for ListView Ex. since quite long and nothing has helped me so far. PLease Help
Thank you
yourlistview.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
}
});
then use bundle to send some content to your next activity.
then getActionBar.setTitle(sometext); this text is come from your bundle.
This contains:
http://www.androidbegin.com/tutorial/android-search-listview-using-filter/
you can refer following link for listview demo code.
http://www.androidhive.info/2011/10/android-listview-tutorial/
and for opening new activity on selecting any item, you can use,
listCoupon.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
// here you can compare selected item by its position and then start a new activity
// arg2 is selected item position in your arraylist
}
});

How to know when a listitem in a listview is focused(highlighted) in Android?

I searched a lot but I couldn't find a straight way for that. All I need is when I clicked on a menu-item I can realize that which on of items in the list is focused?
For example in the blow picture how can I know which item is highlighted(when i clicked on a button for example on the Back Button)?
You must implement an OnItemClickListener
myList.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
YourClass yourObject = (YourClass) arg0.getItemAtPosition(arg2);
}
}
This should do it:
http://developer.android.com/reference/android/widget/AdapterView.html#getSelectedItemPosition()
or keep a reference of the current selection.

How to get a particular row in listview to perform its own operation?

I am new to android programming. I need some help here. I have used this site example on creating a listview. What I want to achieve is when the user clicks a particular row, the row clicked will perform its respective action. (Eg. When clicked row 1 will show a toast. When clicked row 2 will direct the user to another new view, etc.)
I have set a OnItemClickListener to the listview but am lost on how to do it. Any help will be appreciated. Thanks!
Below is my code:
.......
final ListView list = new ListView(this);
list.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(Adapter<?> arg0, View v, int i, long l){
// At implementation
}
});
.......
It looks like you're on the right track, everything should be clearer with onItemClick's parameter names :
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(getApplicationContext(), "Clicked row " + position,
Toast.LENGTH_SHORT).show();
}
So you actually get the position of the item clicked, and can have a different action for each item.
To get the index of the selected item:
int selected_item = myListView.getPositionForView(view);
or to get the string:
String chosen_item = (myListView.getItemAtPosition(selected_item).toString());
If your class is extending ListActivity replace "myListView" with "this".

How can I detect selection on an Android ListView?

I have a simple ListView that gets populated using an array of SimpleAdapters.
How can I detect selection on this ListView?
Sample code is appreciated...
Try ListView.setOnItemClickListener():
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
... do something based on position ...
}
});

How to get the selected item from ListView?

in my Android app I have created a ListView component called myList, and filled it with objects of my own custom type:
class MyClass{
private String displayName;
private String theValue;
... //here constructor, getters, setters and toString() are implemented
}
I used the ArrayAdapter to bound the ArrayList theObjects with myList:
ArrayAdapter<MyClass> adapter=
new ArrayAdapter<MyClass>(this, R.layout.lay_item, theObjects);
myList.setAdapter(adapter);
This works fine, the list is populated and etc., but when I'm trying to access the selected item, i receive a Null object. I've done this using
myList.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> adapter, View v, int position, long id) {
MyClass selItem = (MyClass) myList.getSelectedItem(); //
String value= selItem.getTheValue(); //getter method
}
What seems to be the problem? Thank you
By default, when you click on a ListView item it doesn't change its state to "selected". So, when the event fires and you do:
myList.getSelectedItem();
The method doesn't have anything to return. What you have to do is to use the position and obtain the underlying object by doing:
myList.getItemAtPosition(position);
You are implementing the Click Handler rather than Select Handler. A List by default doesn't suppose to have selection.
What you should change, in your above example, is to
public void onItemClick(AdapterView<?> adapter, View v, int position, long id) {
MyClass item = (MyClass) adapter.getItem(position);
}
Since the onItemClickLitener() will itself provide you the index of the selected item, you can simply do a getItemAtPosition(i).toString(). The code snippet is given below :-
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String s = listView.getItemAtPosition(i).toString();
Toast.makeText(activity.getApplicationContext(), s, Toast.LENGTH_LONG).show();
adapter.dismiss(); // If you want to close the adapter
}
});
On the method above, the i parameter actually gives you the position of the selected item.
On onItemClick :
String text = parent.getItemAtPosition(position).toString();
myList.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> adapter, View v, int position, long id) {
MyClass selItem = (MyClass) adapter.getItem(position);
}
}
Using setOnItemClickListener is the correct answer, but if you have a keyboard you can change selection even with arrows (no click is performed), so, you need to implement also setOnItemSelectedListener :
myListView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {
MyObject tmp=(MyObject) adapterView.getItemAtPosition(position);
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
// your stuff
}
});
In touch mode, there is no focus and no selection. Your UI should use a different type of widget, such as radio buttons, for selection.
The documentation on ListView about this is terrible, just one obscure mention on setSelection.
Though I am using kotlin, the following code answered your question. This return selected item:
val item = myListView.adapter.getItem(i).toString()
The following is the whole selecteditem Listener
myListView.setOnItemClickListener(object : OnItemClickListener {
override fun onItemClick(parent: AdapterView<*>, view: View, i: Int,
id: Long) {
val item = myListView.adapter.getItem(i).toString()
}
})
The code returns the item clicked by its index i as shown in the code
MyClass selItem = (MyClass)
myList.getSelectedItem(); //
You never instantiated your class.

Categories

Resources