【问题标题】:Android: ListView not displaying as intended?Android:ListView 未按预期显示?
【发布时间】:2014-04-04 17:51:51
【问题描述】:

在下面的代码中,我打算在项目行和子项目行上生成一个输出,但代码只是给我带来了一个空的列表视图。我该如何纠正这个问题?

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

.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>

【问题讨论】:

    标签: android xml listview loops map


    【解决方案1】:
    int[] ids={android.R.id.text1,android.R.id.text2}
    

    并从布局中移除两个TextView

    【讨论】:

    • 我如何在 simpleAdapter 中实现这个?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多