我想知道如何从单击的列表视图的项目中获取字符串。例如,我有一个这种格式的条目。
# - 约翰·史密斯
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)