getting a string from a listview item in setOnItemClickListener
android, android-listview
Solution
Well, it depends on what you've populated your listview with. If those are TextView's then all you have to do is this:
((TextView)arg1).getText().toString()
Essentially `View arg1` is the clicked View. Given that you've populated that list, you know what that view is, because you're the one who created and returned it in(in your adapter):
public View getView(int position, View convertView, ViewGroup parent)
Therefore, whatever the type of that View, check its the documentation to see how to get the text out of it
Edit:
Well, the cursor adapter you're using returns a LinearLayout for each row. It has several TextViews, one for each column that's selected. To get the first column you do this:
LinearLayout row = (LinearLayout)((LinearLayout)arg1).getChildAt(0);
TextView column = (TextView)row.getChildAt(0);
String text = column.getText().toString();
If you want a different column just change the argument to `getChildAt(i)`
Problem
I was wondering how I can get a string from a clicked listview's item. For example I have an entry in this format. # - John Smith john@gmail.com 3451234532 New York I want to get John Smith. I can get the item on that position but I don't know the rest. My codes: ``` package com.example.deneme; import android.app.Activity; import android.database.Cursor; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.Button; import android.widget.ListView; import android.widget.SimpleCursorAdapter; import android.widget.TextView; import android.widget.Toast; public class Activity2 extends Activity { private SQLiteAdapter mySQLiteAdapter; ListView listContent; SimpleCursorAdapter cursorAdapter; Cursor cursor; Button buttonDeleteAll,buttonDeleteRow; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity2); buttonDeleteAll = (Button)findViewById(R.id.deleteall); buttonDeleteRow = (Button)findViewById(R.id.deleterow); listContent = (ListView)findViewById(R.id.contentlist); listContent.setClickable(true); buttonDeleteAll.setOnClickListener(buttonDeleteAllOnClickListener); buttonDeleteRow.setOnClickListener(buttonDeleteRowOnClickListener); mySQLiteAdapter = new SQLiteAdapter(this); mySQLiteAdapter.openToWrite(); cursor = mySQLiteAdapter.queueAll(); String[] from = new String[]{ SQLiteAdapter.COLUMN_NAME, SQLiteAdapter.COLUMN_EMAIL,SQLiteAdapter.COLUMN_PHONE, SQLiteAdapter.COLUMN_ADDRESS}; int[] to = new int[]{R.id.text1, R.id.text2,R.id.text3, R.id.text4}; cursorAdapter = new SimpleCursorAdapter(this, R.layout.row, cursor, from, to); listContent.setAdapter(cursorAdapter); listContent.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) { String str = ((TextView)arg1).getText().toString(); Toast.makeText(Activity2.this, str, Toast.LENGTH_SHORT).show(); } }); } Button.OnClickListener buttonDeleteRowOnClickListener = new Button.OnClickListener(){ public void onClick(View arg0) { // String n = inputContent1.getText().toString(); // mySQLiteAdapter.deleteRow(n); updateList(); Toast.makeText(Activity2.this, "Entry Deleted", Toast.LENGTH_SHORT).show(); } }; Button.OnClickListener buttonDeleteAllOnClickListener = new Button.OnClickListener(){ public void onClick(View arg0) { mySQLiteAdapter.deleteAll(); updateList(); Toast.makeText(Activity2.this, "All Entries Deleted", Toast.LENGTH_SHORT).show(); } }; private void updateList(){ cursor.requery(); } } ``` After using your way, I got hese errors: ``` 07-27 07:52:15.726: E/AndroidRuntime(856): FATAL EXCEPTION: main 07-27 07:52:15.726: E/AndroidRuntime(856): java.lang.ClassCastException: android.widget.LinearLayout 07-27 07:52:15.726: E/AndroidRuntime(856): at com.example.deneme.Activity2$3.onItemClick(Activity2.java:58) 07-27 07:52:15.726: E/AndroidRuntime(856): at android.widget.AdapterView.performItemClick(AdapterView.java:284) 07-27 07:52:15.726: E/AndroidRuntime(856): at android.widget.ListView.performItemClick(ListView.java:3513) 07-27 07:52:15.726: E/AndroidRuntime(856): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1812) ```