【问题标题】:How to increment so can I get the next column value not the new row in list view?如何增加以便我可以获得下一列值而不是列表视图中的新行?
【发布时间】:2018-05-14 10:26:33
【问题描述】:

如何获取下一列值而不是新行?

我正在使用 listview 从微调器中显示所选用户的信息。

显示的内容:名字、名字、名字

应该改为:名字、姓氏、电子邮件等。

我正在考虑添加 i++,但似乎不起作用

  public void myMethod(String user) {

    ArrayList<String> theList = new ArrayList<>();

    Cursor data = db.getListViewAccountrequestinfo();
    if (data.getCount() == 0) {

    } else {

        while (data.moveToNext()) {


            theList.add( data.getString(0) );
            ListAdapter listAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, theList);
            lvinfo.setAdapter(listAdapter);
        }
    }
}

【问题讨论】:

  • 您在循环内设置适配器。那是一场彻头彻尾的灾难。将该代码放在循环之外。并在每次迭代时增加一个局部变量,并进一步使用它来解决您的问题。
  • 请说明问题所在或添加错误(如果有)。

标签: java android listview increment android-database


【解决方案1】:

Listview 应该显示数据库中的行,而不是列。

因为data.getString()将参数作为int columnIndex

所以当你增加它的值(columnIndex)时,它会给下一列值而不是新行。

参考Cursor.getString()

【讨论】:

    【解决方案2】:

    我已经拿到了。这是 4 小时后我的新工作代码 LOL

     public void myMethod() {
    
        ArrayList<String> theList = new ArrayList<>();
    
    
        Cursor data = db.getListViewAccountrequestinfo(spnpending.getSelectedItem().toString());
        if (data.getCount() == 0) {
    
        } else {
    
    
            if (data.moveToFirst()) {
                do {
                    theList.add("Username: " + spnpending.getSelectedItem());
                    theList.add("First name: " + data.getString(1));
                    theList.add("Last name: " + data.getString(2));
                    theList.add("Contact: " + data.getString(3));
                    theList.add("Email: " + data.getString(4));
    
                    ListAdapter listAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, theList);
                    lvinfo.setAdapter(listAdapter);
    
                } while (data.moveToNext());
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-01
      • 2022-01-18
      • 1970-01-01
      • 2019-05-17
      • 1970-01-01
      • 2016-08-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多