光标仅输出最后一个值

时间:2014-06-16 00:10:11

标签: android

日志显示所有数据,但它只输出textView中的最后一个值,有关原因的任何想法吗?

    if (c.moveToFirst()) {

        //Log to test cursor data
        Log.i("Cursor", DatabaseUtils.dumpCursorToString(c));


        do {
            String cow = c.getString(c.getColumnIndex("cow"));
            String score = c.getString(c.getColumnIndex("score"));

            textViewOutputcow.setText(cow + "\n");
            textViewOutputscore.setText(score + "\n");
        } while (c.moveToNext());

    }
    c.close(); 

2 个答案:

答案 0 :(得分:2)

每次循环都会设置文本内容,而不是添加文本内容。你可以使用StringBuffer这样做:

StringBuffer cowBuffer=new StringBuffer();
StringBuffer scoreBuffer=new StringBuffer();

do {
    String cow = c.getString(c.getColumnIndex("cow"));
    String score = c.getString(c.getColumnIndex("score"));

    cowBuffer.append(cow + "\n");
    scoreBuffer.append(score + "\n");
    }
while (c.moveToNext());


textViewOutputcow.setText(cowBuffer.toString());
textViewOutputscore.setText(scoreBuffer.toString());

答案 1 :(得分:0)

那是因为你在每次迭代后都会不断更新textview的内容。实际上,您只能看到光标中最后一行的值。