【问题标题】:Android - How to iterate through a listview?Android - 如何遍历列表视图?
【发布时间】:2011-07-17 19:43:02
【问题描述】:

我有一个 ListActivity,列表中的每一行都包含 2 个 TextViews 和 4 个 RadioButtons。

这 2 个 TextView 由 SimpleCursorAdapter 从我的数据库中的表中提取数据填充,而 4 个 RadioButton 只是放入 xml 代码中(它们的 id 被分配给 Java 代码中的 4 个 RadioButton 变量。

基本上我想做的是检查每一行中每个单选按钮的状态,以相应地更新我的数据库。

如何访问每一行 RadioButtons 以查看它们是否被选中?

如果这含糊不清,我深表歉意,如果需要,我会尝试添加更多细节。

非常感谢。

private void processAttendance(){
    int index = 0;
    this.allStudentsCursor = mDbHelper.fetchEnrolledStudents(mRowId);

    lvList=(ListView)findViewById(android.R.id.list);

    this.allStudentsCursor.moveToFirst(); 

    while (allStudentsCursor.isAfterLast() == false) { 


         this.radGroup = (RadioGroup) lvList.getChildAt(index).findViewById(R.id.attendanceGroup);
         this.mAttended = (RadioButton)lvList.getChildAt(index).findViewById(R.id.presentRadio);    
         this.mLate = (RadioButton)lvList.getChildAt(index).findViewById(R.id.lateRadio);    
         this.mExcused = (RadioButton)lvList.getChildAt(index).findViewById(R.id.excusedRadio);    
         this.mMissed = (RadioButton)lvList.getChildAt(index).findViewById(R.id.absentRadio);
         this.mStudentId = allStudentsCursor.getLong(allStudentsCursor.getColumnIndexOrThrow("_id")); 
         this.mResult = "";





    if(mAttended.isChecked()){  
        this.mResult ="attended";
        radGroup.clearCheck();
    }
    else if(mExcused.isChecked()){  
        this.mResult ="excused";        
    }

    else if(mMissed.isChecked()){   
        this.mResult ="missed";     
    }
    else {  
        this.mResult ="late";       
    }

    Calendar cal = Calendar.getInstance(); 
    SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy"); 
    String date = dateFormat.format(cal.getTime());

    this.mDbHelper.addAttendance(date, this.mResult, this.mStudentId, mRowId);
     allStudentsCursor.moveToNext(); 
     index++;
    }




    finish();
}

【问题讨论】:

    标签: android listview android-activity radio-button loops


    【解决方案1】:

    您必须遍历Cursor

    myCursor.moveToFirst();
    /* Loop over all items */
    while (myCursor.isAfterLast() == false) {
        //get radio button
        RadioButton myRadioButton = (RadioButton) findViewById(R.id.radiobutton);
        /*
         Edit or Check myRadioButton
         */
        myCursor.moveToNext();
    }
    

    这是一个简单的示例,仅显示了一个 RadioButton,但您明白了。

    【讨论】:

    • 太棒了!谢谢你的快速反应。我现在想知道如何通过光标从迭代中的当前位置设置我的 Long rowId 变量?有任何想法吗?我相信你可以告诉我,我对 Android 开发还很陌生,所以很抱歉,如果我的问题既愚蠢又乏味 :)
    • 你会使用rowId = myCursor.getLong(myCursor.getColumnIndexOrThrow("_id"));
    • 这个 long 值似乎在每次迭代时都保持为 0,我在调试器中检查它,您认为这是什么原因?如果它为空,那么程序就会崩溃,因为我认为长值不可能为空?
    • 啊,是的,我忘记了。我将我的实例变量更改为 Long 而不是 long,现在它可以工作了。非常感谢您的所有帮助 - 您为我节省了数小时的挫败感 :)
    • 您好,我在使用单选按钮时遇到问题。问题是在遍历游标时。列表中的第一行很好,但对于任何后续行,值都是从第一行中单击的任何单选按钮中获取的。因此,如果在第一行中选中了 4 个单选按钮中的第一个,它将通过将其余行添加到数据库中,说明第一个单选按钮已被选中,即使它没有被选中。例如,我如何访问在第三行中选中了哪个单选按钮?
    【解决方案2】:

    这从根本上是有缺陷的——ListView 不会为每一行膨胀不同的布局视图层次结构,它只保留足够填满屏幕所需的内容。当一个滚动离开边缘时,它被重新用于显示下一个滚动进入的数据。

    列表中每一行内的数据必须来自游标中的数据。如果不是,它会在滚出屏幕时丢失。

    【讨论】:

      【解决方案3】:

      你不能。这些视图在列表视图中被回收,因此如果您在列表中有 20 个项目,您实际上可能只有 5 个 RadioButton 实例。一旦视图离开屏幕,它就会被回收。

      您需要做的是,每当单击 RadioButton 时,更改列表后面的数据,以确定是否需要检查单选。

      【讨论】:

      • 啊,是的,你是对的。更改列表后面的数据以阻止这种情况发生的最佳方法是什么?我想我可能需要重新考虑整个活动的设计?我是 Android 新手,所以我仍在寻找自己的方式(我相信你可以从我的问题中看出!)
      猜你喜欢
      • 1970-01-01
      • 2010-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-14
      • 1970-01-01
      相关资源
      最近更新 更多