如何从选定的列表视图项中获取文本
how take text from selected listview item
我正在使用由数据库填充的列表视图。我应该从选定的 listView 项目中获取文本,但我不知道应该使用什么方法。你能解释一下吗?
这是我的代码:
public class UserActivity extends ActionBarActivity{
public static ArrayList<String> ArrayofName = new ArrayList<String>();
private ListView listView;
public static String item_name = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.user_activity);
DataHandler db = new DataHandler(this);
List<Data> items = db.getTitle();
listView = (ListView) findViewById(R.id.listView1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, ArrayofName);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
//here i should take text from selected item
}
});
}
}
试试这个:
@Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
String text = ArrayofName.get(position);
//do whatever you want with the object "text"
}
我正在使用由数据库填充的列表视图。我应该从选定的 listView 项目中获取文本,但我不知道应该使用什么方法。你能解释一下吗?
这是我的代码:
public class UserActivity extends ActionBarActivity{
public static ArrayList<String> ArrayofName = new ArrayList<String>();
private ListView listView;
public static String item_name = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.user_activity);
DataHandler db = new DataHandler(this);
List<Data> items = db.getTitle();
listView = (ListView) findViewById(R.id.listView1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, ArrayofName);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
//here i should take text from selected item
}
});
}
}
试试这个:
@Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
String text = ArrayofName.get(position);
//do whatever you want with the object "text"
}