无法在列表视图中显示项目

时间:2014-01-22 10:11:05

标签: java android xml listview android-arrayadapter

我对android很新,我试图在ListView中显示一些统计信息。 根据在微调器中选择的选项,我的StatsModel.java类(其控制器是StatsController.java)中生成一个列表。我查看了有关获取ListView显示错误的其他问题,但我无法找到问题的解决方案。

以下是尝试显示列表的活动的代码:

- 我使用Toasts来确保StatsModel中的列表实际上正在更新

-stController.getCurrentList()从statsModel.java返回当前列表

StatsActivity.java

public class StatisticsActivity extends Activity {

    //private TwoColStatsAdapter statsListAdapter;
    private ArrayAdapter<String> statsListAdapter;
    private String mClickerName;
    private StatisticsController stController = new StatisticsController();
    final ClickerListController clController = new ClickerListController();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_statistics);

        Intent i = getIntent();
        mClickerName = i.getStringExtra("clickerName");
        stController.setClickDateList(clController.getClickers().get(mClickerName).getClickTimes());
        stController.clearStats();

        /*
         * STATS_SPINNER ------------------------------------------------------------------------
         */
        Spinner statsSpinner = (Spinner) findViewById(R.id.stats_spinner);
        ArrayAdapter<CharSequence> spinnerAdapter = ArrayAdapter.createFromResource(getApplicationContext(),
                R.array.stats_spinner, R.layout.stats_spinner_item);
        spinnerAdapter.setDropDownViewResource(R.layout.stats_spinner_item);
        statsSpinner.setAdapter(spinnerAdapter);

        statsSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parentView,
                    View selectedItemView, int position, long id) {
                stController.clearStats();
                switch (position) {
                case 1:
                    stController.setHourlyStatistics();
                    Toast.makeText(getApplicationContext(), stController.getCurrentList().get(0), Toast.LENGTH_SHORT).show();
                    break;
                case 2:
                    stController.setDailyStatistics();
                    Toast.makeText(getApplicationContext(), stController.getCurrentList().get(0), Toast.LENGTH_SHORT).show();
                    break;
                case 3:
                    stController.setWeeklyStatistics();
                    Toast.makeText(getApplicationContext(), stController.getCurrentList().get(0), Toast.LENGTH_SHORT).show();
                    break;
                case 4:
                    stController.setMonthlyStatistics();
                    Toast.makeText(getApplicationContext(), stController.getCurrentList().get(0), Toast.LENGTH_SHORT).show();
                    break;
                default:
                    break;
                }
                statsListAdapter.notifyDataSetChanged();

            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {

            }

        });

        /*
         * STATS_LIST --------------------------------------------------------------------------
         */
        final ListView statsList = (ListView) findViewById(R.id.ListView1);
        /*statsListAdapter = new TwoColStatsAdapter(getApplicationContext(), R.layout.two_column_list, stController.getCurrentList());
        statsList.setAdapter(statsListAdapter);
        statsList.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                // TODO Auto-generated method stub

            }
        });*/
        statsListAdapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, stController.getCurrentList());
        statsList.setAdapter(statsListAdapter);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.statistics, menu);
        return true;
    }

}

activity_statistics.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".StatisticsActivity" >

    <Spinner
        android:id="@+id/stats_spinner"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:prompt="@string/stats_spinner_prompt"
        android:textColor="#000" />

    <ListView
        android:id="@+id/ListView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/stats_spinner"
        android:layout_centerHorizontal="true" >
    </ListView>

</RelativeLayout>

我不确定我在这里缺少什么,但我很好奇是否必须对列表项定义为函数这一事实做一些事情:stController.getCurrentList()。但我不认为这是问题,因为我已经尝试定义一个本地列表并让它获取当前列表但它仍然没有显示所述列表。让我知道我错过了什么......谢谢。

随意指出我在其他地方

1 个答案:

答案 0 :(得分:0)

尝试选择ui元素以显示文本:

statsListAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, stController.getCurrentList());