How to implement onClick in a Listview - android

I guess this is simple.
I want to toast the text in the rows when I click on it. And here is my code:
public class AndroidSQLite extends Activity { private SQLiteAdapter
mySQLiteAdapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView listContent = (ListView)findViewById(R.id.contentlist);
mySQLiteAdapter = new SQLiteAdapter(this);
mySQLiteAdapter = new SQLiteAdapter(this);
mySQLiteAdapter.openToRead();
Cursor cursor = mySQLiteAdapter.queueAll();
startManagingCursor(cursor);
String[] from = new String[]{SQLiteAdapter.KEY_NOME};
int[] to = new int[]{R.id.text};
SimpleCursorAdapter cursorAdapter =
new SimpleCursorAdapter(this, R.layout.row, cursor, from, to);
listContent.setAdapter(cursorAdapter);
mySQLiteAdapter.close();
}
}
Thank You!

just write down
listContent.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapter, View arg1,
int position, long id) {
Log.i("get ItemIDPosition",
"" + adapter.getItemIdAtPosition(position));
Log.i("get ItemATPosition", "" + adapter.getItemAtPosition(position));
}
});

You can use
listContent.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
}
});

Like this:
listContent.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
String item = ...;
Toast.makeText(getBaseContext(), item, Toast.LENGTH_LONG).show();
}
});

Try this code for Item click
listContent.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
String text = <Get Your from database is here in this string>;
Toast.makeText(getBaseContext(), text, Toast.LENGTH_LONG).show();
}
});

Use onitemclick evetn and not onclick.
listview.setOnItemClickListener(...);
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
arg2 is the index in the list.

Related

Setting ClickListener on Activity using CursorAdapter

EDIT:
i solved it:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
});
-------------------------------------------------------------------------------------------------------
I need to set a itemClickListener on ListView which content is set by CursorAdapter in normal Activity in my case it's AppCompatActivity and not ListActivity. I saw some examples but every of them was about ArrayAdapter or Activity was ListActivity
Here's Java code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_workout_list);
listView = findViewById(R.id.workout_listview);
try {
SQLiteOpenHelper sqLiteOpenHelper = new TrainingDatabaseHelper(this);
db = sqLiteOpenHelper.getReadableDatabase();
cursor = db.query("WORKOUT",
new String[]{"_id", "NAME"},
null,
null,
null,
null,
"NAME ASC");
CursorAdapter cursorAdapter = new SimpleCursorAdapter(this,
R.layout.item_list,
cursor,
new String[]{"NAME"},
new int[]{android.R.id.text1},
0);
listView.setAdapter(cursorAdapter);
} catch (SQLiteException e) {
...catching...
}
}
Of course i set a listView, cursor etc
Just set itemclicklistener on your listview
listView.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3)
{
Toast.makeText(SuggestionActivity.this, "" + position, Toast.LENGTH_SHORT).show();
}
});

get selected Value in listView

I have two list in android. One of them is title and the other is ID. I showed the title list in listView. Each title has it's own id. I want to get the id of the each title that I checked. how can I get the id of each title?
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_remark);
context = getApplicationContext();
sh = new SchemaHelper(context);
// -- Display mode of the ListView
ListView listview= getListView();
// listview.setChoiceMode(listview.CHOICE_MODE_NONE);
// listview.setChoiceMode(listview.CHOICE_MODE_SINGLE);
listview.setChoiceMode(listview.CHOICE_MODE_MULTIPLE);
//-- text filtering
listview.setTextFilterEnabled(true);
final List<String> textList= new ArrayList<String>();
final List<String> idList= new ArrayList<String>();
Cursor c= sh.getAllRemark();
for(int i=0; c.moveToNext();i++){
String caption= c.getString(c.getColumnIndex(P_RemarkTable.REM_CAPTION.toString()));
String id= c.getString(c.getColumnIndex(P_RemarkTable.REM_ID.toString()));
textList.add(caption);
idList.add(id);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_checked, textList);
listview.setAdapter(adapter);
listview.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
// idList.get(i).toString()
public void onItemClickListener(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(getApplicationContext(),idList.get(i).toString() , Toast.LENGTH_SHORT).show();
// barrierID=Integer.parseInt(idList.get(i));
}
public void onItemChecked(AdapterView<?> adapterView, View view, int i, long l){
Toast.makeText(getApplicationContext(), idList.get(i).toString(), Toast.LENGTH_SHORT).show();
}
public void onListItemClick(AdapterView<?> adapterView, View view, int i, long l){
// super.onListItemClick(adapterView, view, i, l);
Toast.makeText(getApplicationContext(),idList.get(i).toString(), Toast.LENGTH_SHORT).show();
}
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(getApplicationContext(), idList.get(i).toString(), Toast.LENGTH_SHORT).show();
// barrierID=Integer.parseInt(idList.get(i));
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
// do nothing
}
});
}
}
}
Non of this method works.
What should I do?

I have an array and i want to get the equivalent index of specific item(selected from autocomplettextview)

this is my mainactivity
TestAdapter mdh = new TestAdapter(this);
mdh.open();
ArrayList<String> songs = mdh.getAllSongs();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, songs);
AutoCompleteTextView textView = (AutoCompleteTextView)
findViewById(R.id.autocomplete_songs);
textView.setAdapter(adapter);
textView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
}
});
As you can see.. adapter has all the songs and pass down to the autocompletetextview
what i want is when i click an item on the autocompletetextview..i will get its index number and store it on an integer..i want its index from the arraylist and not its index from the dropdown..please help me...
TestAdapter mdh = new TestAdapter(this);
mdh.open();
ArrayList<String> songs = mdh.getAllSongs();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, songs);
AutoCompleteTextView textView = (AutoCompleteTextView)
findViewById(R.id.autocomplete_songs);
textView.setAdapter(adapter);
textView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Log.i("GiveTag", arg2); // it will give you what ever item clicked position /index
}
});
Try this way
textView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int pos, long l) {
String str = songs.get(pos);
}
});
Check if this works
textView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
String text = textView.getText().toString();
int position = songs.indexOf(text);
}
});
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Object data = arg0.getItemAtPosition(arg2);
int pos = java.util.Arrays.asList(songs).indexOf(data);
}
});

onListItemClick callback isn't activated

public class List_Items extends ListActivity{
private ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_item);
List <ImageAndText> total_list=new ArrayList<ImageAndText>();
ListView lv = (ListView) this.findViewById(android.R.id.list);
lv.setAdapter((ListAdapter) new ImageAndTextListAdapter(this, total_list));
getListView().setTextFilterEnabled(true);
//////////////////////////////////////////////////////
Button btn=(Button) findViewById(R.id.button_sync);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast message=Toast.makeText(getApplicationContext(), "click the list_item", Toast.LENGTH_LONG);
message.show();}
});
}
public void onListItemClick(ListView parent, View v, int position, long id)
{
Toast.makeText(getApplicationContext(), "You have selected " +(position+1)+"th item",
Toast.LENGTH_SHORT).show();}
}
I have this list_item which is extends the listactivity. However, when I click on one of the row, the callback onListItemClick didn't get activated. Why is that? I don't need to anything with the adapter on this right?
}
You have to use the following in all the widgets of xml which you are inflating for your custom ListView.
android:focusable="false"
android:focusableInTouchMode="false"
Hope it helps
u havent register ur onItemClick with Listview.
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
Log.i("List Clicked ....", "List Clicked...");
}
});
or
listView.setOnItemClickListener(listener);
public OnItemClickListener listener = new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Log.i("List Clicked ....", "List Clicked...");
}
};
Can you try this
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Log.i("You clicked the list");
}

How to put Intent on setOnItemClickListener in Android?

hope you can help me...
I have a class, in that class there is a listview from database.
My question is how can i put setOnItemClickListener, so if i click the item in listview there going to go to
Intent(android.content.Intent.ACTION_VIEW,Uri.parse("http://maps.google.com/maps?f=d&saddr=&daddr="));
and the daddr get the latt and long value from database.
Here is my coding...
public class HotelList extends Activity{
hotelHelper dbHotelHelper;
protected Cursor cursor;
protected ListAdapter adapter;
ListView numberList;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.hotellist);
numberList = (ListView)findViewById(R.id.hotelListView);
dbHotelHelper = new hotelHelper(this);
try{
dbHotelHelper.createDataBase();
}
catch (Exception ioe){
Log.e("err","Unable to create database");
}
view();
}
private void view() {
// TODO Auto-generated method stub
SQLiteDatabase db = dbHotelHelper.getReadableDatabase();
try{
cursor = db.rawQuery("SELECT * FROM hoteltbl", null);
adapter = new SimpleCursorAdapter(
this,
R.layout.hotelview,
cursor,
new String[]{"name"},
new int[] {R.id.hotelname}
);
numberList.setAdapter(adapter);
}
catch (Exception e){
Log.e("error",e.toString());
}
}
}
I really appreciate who is giving me the answer...
try this way
numberList.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> arg0, View view, int position,long id) {
startActivity(new Intent(android.content.Intent.ACTION_VIEW,Uri.parse("http://maps.google.com/maps?f=d&saddr=&daddr=1.481198,124.83572")));
}
});
try this:
numberList.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View view, int position,
long id) {
//fire new intent here
}
});
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View view, int position,
long arg3)
{
//do stuff using position and view
}
});
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.hotellist);
numberList = (ListView)findViewById(R.id.hotelListView);
numberList.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// Intent shuold start here
Intent intent = Intent(android.content.Intent.ACTION_VIEW,Uri.parse("http://maps.google.com/maps?f=d&saddr=&daddr=1.481198,124.83572"));
startActivity(intent);
}
});
dbHotelHelper = new hotelHelper(this);
try{
dbHotelHelper.createDataBase();
}
catch (Exception ioe){
Log.e("err","Unable to create database");
}
view();
}
numberList .setOnItemClickListener(new SelectionListener())
private class SelectionListener implements OnItemClickListener{
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
if(position == 0){ Intent i = new Intent(android.content.Intent.ACTION_VIEW,Uri.parse("http://maps.google.com/maps?f=d&saddr=&daddr=1.481198,124.83572"));
startActivity(i)
}
}
see this
listview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,int position, long id) {
switch (position){
case 0: Intent(android.content.Intent.ACTION_VIEW,Uri.parse("http://maps.google.com/maps?f=d&saddr=&daddr="));
startActivity(animalsGridViewIntent);
break;
//// depending on positions add intent's here..
}

Categories

Resources