Android:ListView没有按预期显示?

时间:2014-04-04 17:51:52

标签: android xml listview loops map

在下面的代码中,我打算在项目行和子项目行上生成一个输出,但代码只是给我一个空的列表视图。我怎么能纠正这个?

游戏是乘法时间表游戏,其意图是当用户输入答案时(在prev活动上),他们输入的答案显示在项目中(即12个项目),并且每个子项中显示每个子项的正确答案-Item。

.Java代码:

public class Results extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.results);

        ListView itemList = (ListView) findViewById(R.id.lvresults);


        //gets array from prev act
        int[] results = getIntent().getIntArrayExtra("results");

        int numberPassed = getIntent().getIntExtra("numberPassed", 0);


        ArrayList < HashMap <String, String> > list = new ArrayList < HashMap <String, String> > ();


        // loop to give list view
        for (int i = 1; i <= 12; ++i)
        {
            int userAnswer = results[i - 1];

            int expectedAnswer = numberPassed * i;

            String userString = numberPassed + "x" + i + "=" + userAnswer;

            String expectedString = "" + expectedAnswer;

            HashMap <String, String> map = new HashMap <String, String> ();

            map.put("user", userString);
            map.put("expected", expectedString);

            list.add(map);
        }


        String[] keys = {"user", "expected"};

        int[] ids = {R.id.user_answer, R.id.expected_answer};

        SimpleAdapter adapter = new SimpleAdapter(this, list, android.R.layout.simple_list_item_2, keys, ids);

        itemList.setAdapter(adapter);
    }
}

XML代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ListView 
       android:id="@+id/lvresults"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content" >

    </ListView>

    <TextView
        android:id="@+id/user_answer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="15dp" />

    <TextView
        android:id="@+id/expected_answer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="15dp" />

</LinearLayout>

1 个答案:

答案 0 :(得分:0)

int[] ids={android.R.id.text1,android.R.id.text2}

并从布局中删除两个TextView