我想知道如何从点击的listview项目中获取字符串。例如,我有一个这种格式的条目。
# - John Smith
john@gmail.com
3451234532
纽约
我想要约翰史密斯。我可以在该位置上获得该项目,但我不知道其余的。
我的代码:
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();
}
}
用完你的方式后,我得到了错误:
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)
答案 0 :(得分:3)
嗯,这取决于你在listview中填充的内容。如果那些是TextView,那么你所要做的就是:
((TextView)arg1).getText().toString()
基本上View arg1
是点击的视图。鉴于您已填充该列表,您知道该视图是什么,因为您是(在您的适配器中)创建并返回它的人:
public View getView(int position, View convertView, ViewGroup parent)
因此,无论该视图的类型如何,请查看其文档以了解如何从中获取文本
好吧,你正在使用的游标适配器为每一行返回一个LinearLayout。它有几个TextView,每个列都选择一个。要获得第一列,请执行以下操作:
LinearLayout row = (LinearLayout)((LinearLayout)arg1).getChildAt(0);
TextView column = (TextView)row.getChildAt(0);
String text = column.getText().toString();
如果您想要其他列,只需将参数更改为getChildAt(i)
答案 1 :(得分:3)
listContent.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id) {
Cursor cursor = (Cursor) parent.getItemAtPosition(position);
String name = cursor.getString(cursor.getColumnIndex(SQLiteAdapter.COLUMN_EMAIL));
Toast.makeText(Activity2.this, name, Toast.LENGTH_SHORT).show();
}
});
答案 2 :(得分:0)
要获取文字,只需使用
即可String item = ((TextView)arg1).getText().toString();
另一方面,如果重命名您实现的函数的参数,将会很有帮助:
public void onItemClick(AdapterView<?> parent, View view, int position,
long id)
现在您可以清楚地看到每个参数的作用。