【问题标题】:Repopulate listview by correct id?通过正确的 id 重新填充列表视图?
【发布时间】:2017-09-06 17:34:23
【问题描述】:

(首先,对不起我的英语不好,请改正)

嗨,我可以从数据库中填充列表视图,如果我点击任何列表视图项,它会通过相关的“id”转到“Detail.class”。

问题是当我重新填充课程时(课程=“WHERE deck='”+课程+“'”;),列表视图正确填充但列表视图索引/ID?没有改变,当我单击 listview 项目时,“Detail.class”正在打开但 ID 错误。

----- 数据库助手类---

    if (lesson.equals("")) {
        lesson= "";

    } else {
        lesson= " WHERE deck='" + MainActivity.lesson + "'";

    }


    String selectQuery = "select word, id,word,typ from TAB" + lesson + " group by id"; 

---- MainActivity 类----

private void loadListview() {
    Database db = new Database(getApplicationContext());
    List<String> labels = db.makeList();

    dataAdapter = new ArrayAdapter<String>(this, R.layout.listview_spc, labels);

    Listview1 = (ListView) findViewById(R.id.liAll);
    Listview1.setAdapter(dataAdapter);

}


    Listview1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                long arg3) {

                Intent intent = new Intent(getApplicationContext(), Detail.class); 
                intent.putExtra("id", (int) word_ids[arg2]); // I think the problem is here.
                startActivity(intent);
                  }
    });

【问题讨论】:

    标签: android listview populate


    【解决方案1】:

    将clicklistner代码替换为下面,

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                            Intent intent = new Intent(getActivity(), Detail.class);
                            intent.putExtra("Id",labels.get(position).getId());
                            startActivity(intent);
                        }
                    });
    

    【讨论】:

    • 有什么问题吗?@Tunc
    • 谢谢,但是是的。 getId() 给出“无法解析方法”错误。还有为什么要把“id”改成“Id”。
    • 好的。你可以做 labels.get(position); 来获取你点击的元素的位置
    • 抱歉不起作用。也许问题出在其他地方。我会考虑的。
    • 实际上你想要获得点击项目的位置,而不是函数中的 (int position) 会给你位置,基于你可以从你的列表中获取数据,你可以告诉我你是否被卡在某个地方
    猜你喜欢
    • 1970-01-01
    • 2015-09-20
    • 1970-01-01
    • 1970-01-01
    • 2017-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-05
    相关资源
    最近更新 更多