Android:
Hello, i am trying to get index of the item that is clicked but i don't know how i can do it.
I want if i click on any item in listview it will open SmsManager with number of this item(Phone Number).
Can somebody tell me how i can do it?
There is source code:
list1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View view, int position,
long id) {
SmsManager m = SmsManager.getDefault();
Uri uriSMSURIs = Uri.parse("content://sms/inbox");
Cursor cc = getContentResolver().query(uriSMSURIs, null, null, null, null);
String phoneNumber = cc.getString(cc.getColumnIndex("address"));
m.sendTextMessage(phoneNumber , null, phoneNumber , null, null);
}
});
The "index of the item witch [sic] is clicked" is the position parameter supplied to onItemClick().
Implement
onListItemClick
#Override
protected void onListItemClick(ListView l, View v, final int position, long id) {
super.onListItemClick(l, v, position, id);
Log.i("the Item clicked is :: ", position);
}
Related
I Need to get the id of a selected spinner value from sqlite database in android
public void onItemSelected(AdapterView<?> parent, View view, int position,long id) {
String label = parent.getItemAtPosition(position).toString();
Log.d("label:", label);
}
I need to return the id of the id of the selected value
Try this code
int getId(String label)
{
SqliteDatabase db=this.getWritableDatabase();
Cursor cur=db.rawQuery("SELECT ID FROM TABLENAME WHERE NAME=\""+label+"\"");
cur.moveToFirst();
String ID=cur.getString(0);
return Integer.valueOf(ID);
}
In your MAin activity try this code for your spinner
spin_category.setOnItemSelectedListener(new OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3)
{
category_id=arg2;
int my_id=db.getID(category_id);
}
public void onNothingSelected(AdapterView<?> arg0)
{
}
});
In You Database class write this function
public int getID(int id)
{
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_NAME, new String[] { KEY_ID, KEY_NAME}, KEY_ID+"=?",
new String[] { String.valueOf(id) }, null);
if(cursor!=null)
if (cursor.getCount() > 0)
{
cursor.moveToFirst();
}
int s = cursor.getInt(cursor.getColumnIndex(KEY_ID));
cursor.close();
db.close();
return s;
}
The first function will get id of selected item in spinner and will pass it to database method and second function will search for that id and return id of that id. As I am not very clear with your requirement. Please comment if you want it in different way.
If your adapter is a SimpleCursorAdapter, you already have the id of the selected one provided by the parameter 'id'. If it is a custom adapter, override the adapter's getItemId() method to return the correct id for a selected row.
You don't really need to do this either:
String label = parent.getItemAtPosition(position).toString();
...since the parameter 'view' already provides the selected item within the parent, i.e. you can do this instead:
String label = view.toString();
public void onItemSelected(AdapterView<?> parent, View view, int position,long id) {
String label = parent.getItemAtPosition(position).toString();
Log.d("label:", label);
}
spinner_referrer_to_type.getSelectedItem().toString();
protected void onListItemClick(ListView l, View v, int position, long id)
{
super.onListItemClick(l, v, position, id);
int selection = position;
switch (selection)
{
case 0:
{
String toast=" 1 clicked";
DisplayToast(toast);
}
break;
case 1:
{
String toast=" 2 clicked";
DisplayToast(toast);
}
break;
}
}
This is the code I am using for my OnListItemClick() in List Activity.
My Problem is my List item is populated dynamically and hence I don't know how many items would be there in the list.
I cannot use switch statement in that case.So. How can I distinguish which item was clicked in a dynamically changing list.
Set an onItemClickListener like this:
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapter, View v, int position, long id) {
Toast.makeText(MainActivity.this, adapter.getItemAtPosition(position).toString(), Toast.LENGTH_SHORT).show();
}
});
So you can get the text of your currently selected item by getItemAtPosition(position).toString().
Or if you make your own ArrayAdapter, you can implement the getItem(position) method, which can return with anything about your adapter item.
This will get the Text from the selected textview and display it in a Toast.
protected void onListItemClick(ListView l, View v, int position, long id)
{
super.onListItemClick(l, v, position, id);
String toast= ((TextView)v).getText();
DisplayToast(toast);
}
Following is the code
String toast = " " + ++position + " clicked";
DisplayToast(toast);
I have listview that binds from sqlite and groups by KEY_TITLE(field) so i need to get the name of the item that is clicked
#Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
super.onListItemClick(l, v, position, id);
Intent i=new Intent(this,detail.class);
i.putExtra(DatabaseIN.KEY_TITLE,SOMETHING THAT I NEED!);
//startActivity(i);
startActivityForResult(i,ACTIVITY_EDIT);
}
// maybe something like this string selectedIteme = l.getSelectedItem().getSOMETNIG?
check this tutorial might help you
http://www.vogella.de/articles/AndroidListView/article.html
you can pick the selected item value like this
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
Cursor c = (Cursor) arg0.getAdapter().getItem(arg2);
Intent i=new Intent(getApplicationContext(),Xyz.class);
i.putExtra("abc", c.getString(1));
startActivity(i);
}
Try this in your onClick method..
Map<String, String> selection = (Map<String, String>) listView.getItemAtPosition(position);
String count = selection.get("count");
String title = selection.get("title");
i create a ListView with content from a Database.
I want to get the Text from one Item with:
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
String item = l.getItemAtPosition(position).toString();
}
But i get some text: "android.database.sqlite.SQLiteCurser#45765"
Can anyone please help me?
What exactly you retrieved from the database. If it's only set of Strings you can try the below code.
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
// Get the item that was clicked
Object o = this.getListAdapter().getItem(position);
String keyword = o.toString();
Toast.makeText(this, "You selected: " + keyword, Toast.LENGTH_LONG).show(); }
this should work...
On onItemClick :
String text = parent.getItemAtPosition(position).toString();
Following is my code :
public class TestActivity extends ListActivity {
private Cursor cursor;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
fillData();
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
// Get the item that was clicked
Object o = this.getListAdapter().getItem(position);
String keyword = o.toString();
Toast.makeText(this, "You selected: " + keyword, Toast.LENGTH_LONG)
.show();
}
private void fillData() {
TermsData_DataHelper termsDataHelper = new TermsData_DataHelper(
TestActivity.this);
cursor = termsDataHelper.fetchAllCategories();
startManagingCursor(cursor);
String[] names = new String[] { "name" };
int[] to = new int[] { R.id.label };
// Now create an array adapter and set it to display using our row
SimpleCursorAdapter categories = new SimpleCursorAdapter(this,
R.layout.terms_row, cursor, names, to);
setListAdapter(categories);
}
}
When I click on a row in the listview, I don't see any Toast. Can anybody tell me where am I making a mistake.
Thanks in advance.
I was having a similar problem and found that after removing android:clickable="true" from my list items, the onListItemClick() was being triggered just fine. I hope this helps.
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
String mString = parent.getItemAtPosition(position).toString();
//will give you the text of current line or
int i = parent.getItemAtPosition(position);
//if you want just position number
}
The solution is to override protected void onListItemClick (ListView l, View v, int position, long id) method.
I had this problem also. I solved it by Removing this from my TextView in the the layout XML.
android:textIsSelectable="true"