gpt4 book ai didi

android - 在 ListView 项目上单击打开一个对话框

转载 作者:行者123 更新时间:2023-11-29 22:29:15 24 4
gpt4 key购买 nike

我想在每次单击 ListView 项时打开一个 Dialog

此代码无效,我真的找不到错误。请帮忙!

 private void loadFeed(){
try{
BaseFeedParser parser = new BaseFeedParser();
messages = parser.parse();
List<String> descriptions = new ArrayList<String>();
List<String> titles = new ArrayList<String>(messages.size());
for (Message msg : messages){
descriptions.add(msg.getDescription());
titles.add(msg.getTitle() + "\n" +msg.getDate());
}
ArrayAdapter<String> adapter =
new ArrayAdapter<String>(this, R.layout.row,titles);
this.setListAdapter(adapter);
} catch (Throwable t){
Log.e("AndroidNews",t.getMessage(),t);
}
}


@Override
protected void onListItemClick(ListView descriptions,
View v, int position, long id) {
super.onListItemClick(descriptions, v, position, id);
String description = descriptions.get(position);
Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.single);
dialog.setTitle("Blog");
dialog.setCancelable(true);
TextView text = (TextView) dialog.findViewById(R.id.TextView1);
text.setText(description);
dialog.show();

}

使用此代码,应用程序正在运行,对话框显示描述,但描述也显示在列表项中。

messages = parser.parse();
List<String> titles = new ArrayList<String>(messages.size());
for (Message msg : messages){
titles.add(msg.getTitle() + "\n" +msg.getDate() + "\n\n" + msg.getDescription());
}
ArrayAdapter<String> adapter =
new ArrayAdapter<String>(this, R.layout.row,titles);
this.setListAdapter(adapter);

protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
String selection = l.getItemAtPosition(position).toString();
Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.row2)
dialog.setCancelable(true);
TextView text = (TextView) dialog.findViewById(R.id.SinglePost);
text.setText(selection);
dialog.show();

}

最佳答案

这不会起作用,因为 get(int i) 方法对于类型 ListViewArrayAdapter 都不存在。

编辑:

你似乎混淆了 ListView类和 List界面。这是两个完全不同的东西!

实现 List 接口(interface)的类(如您的案例中的 ArrayList)包含对象,ListView 类是和显示 的 Android 小部件>以列表形式查看

我真的建议您完成 Hello Views教程部分,以便您在深入了解更复杂的内容之前对 Android View 有一个基本的了解。

关于android - 在 ListView 项目上单击打开一个对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4838246/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com