【问题标题】:Getting text from a clicked listview item从单击的列表视图项中获取文本
【发布时间】:2013-04-23 04:00:52
【问题描述】:

我的 ListView 是从我的数据库中提取的食谱列表。我正在尝试在我的 ListView 中获取单击项目的文本。 ListView 通过数据库调用和 cursoradapter 填充。我想使用所选项目的文本在另一个活动中进行另一个数据库调用。这是代码块

listView.setClickable(true);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View v, int position,
                long id) {
            final String text = ((TextView)v).getText().toString();
            recipeid = myDBAdapter.getRecipeID(text);
            Intent intent = new Intent(ListAllRecipes.this, DisplayRecipe.class);
            intent.putExtra("recipeid", recipeid);
            startActivity(intent);

        }

    });

当我运行我的代码时,我得到了

04-22 14:08:37.022: E/AndroidRuntime(25206): FATAL EXCEPTION: main
04-22 14:08:37.022: E/AndroidRuntime(25206): java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.TextView
04-22 14:08:37.022: E/AndroidRuntime(25206):    at com.example.ketorecipes.ListAllRecipes$1.onItemClick(ListAllRecipes.java:47)

当我点击 ListView 中的一个项目时。

这是一个块中的 Activity:

public class ListAllRecipes extends Activity{
private DBAdapter myDBAdapter;
private ListView listView;
private int recipeid;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list_all);



    myDBAdapter = new DBAdapter(this);
    myDBAdapter.openToRead();
    Cursor c = myDBAdapter.getValues();

    listView = (ListView)findViewById(R.id.listView1);
    String[] from = new String[] {"_id"};
    int[] to = new int[] {R.id.name_entry};
    SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this, R.layout.list_entry, c, from, to);
    listView.setAdapter(cursorAdapter);



    listView.setClickable(true);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View v, int position,
                long id) {
            final String text = ((TextView)v).getText().toString();
            recipeid = myDBAdapter.getRecipeID(text);
            Intent intent = new Intent(ListAllRecipes.this, DisplayRecipe.class);
            intent.putExtra("recipeid", recipeid);
            startActivity(intent);

        }

    });
}

}

【问题讨论】:

  • 您应该使用v.findViewById(your_textview_id) 来检索文本,而不是将底层布局转换为TextView
  • 感谢您的回复。我对 Android 还是很陌生,而且我一开始就不是一个非常强大的程序员。你能详细说明一下吗?
  • 我假设您正在使用基于 RelativeLayout 的自定义布局填充 ListView?现在单击 ListView 将为您提供此布局主视图作为 View 参数。但是您可以使用 View.findViewById(resource_id_assigned_for_TextView) 检索底层 TextView

标签: android listview


【解决方案1】:

您将 _id 与 R.id.name_entry 相关联,因此您的列表包含 _id 的任何值。

String[] from = new String[] {"_id"};
int[] to = new int[] {R.id.name_entry};  

如果是这样,您可以通过 _id 获取名称

Cursor c = cursorAdapter.getCursor();
String text = c.getString(c.getColumnIndex("_id"));

【讨论】:

  • 谢谢!我成功地得到了字符串,现在我只需要弄清楚我在哪里搞砸了我的 SQL!
【解决方案2】:

我建议使用position 字段onItemClickparent.getItemAtPosition(position) 获取项目,这将返回代表您刚刚单击的项目的条目(可能需要转换为正确的类型),应该随心所欲,无需手动从 UI 元素中获取值。

【讨论】:

    【解决方案3】:

    我很确定返回的视图 (v) 不是文本视图。 您的列表项是自定义视图,因此您需要通过 id 查找视图来获取视图。

    【讨论】:

      猜你喜欢
      • 2018-05-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多