0

这是布局xml

           <ListView
                    android:id="@+id/titleListView"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent" >
                </ListView>

我的活动是:

public class DummyActivity extends Activity {

private ListView titleListView;

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

    titleListView=(ListView)findViewById(R.id.titleListView);

    showAssignmentTitlesInList();
}



private void showAssignmentTitlesInList()
{

    String[] monthsArray = { "JAN", "FEB", "MAR", "APR", "MAY", "JUNE", "JULY",
             "AUG", "SEPT", "OCT", "NOV", "DEC" };

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(DummyActivity.this,
                android.R.layout.simple_list_item_1 ,monthsArray);
            titleListView.setAdapter(adapter);
            Log.d("TAG", monthsArray.length+"");
}
}

这是我的数组数组适配器代码...

我有一个名为“Months array”的字符串数组,我需要将其显示为列表视图。

4

1 回答 1

0

而不是这个:

ArrayAdapter adapter = new ArrayAdapter(this,
                    android.R.layout.simple_list_item_1, monthsArray);
                titleListView.setAdapter(adapter);

尝试这个 :

ArrayAdapter adapter = new ArrayAdapter(//// Your Activity Name.this /////,
                    android.R.layout.simple_list_item_1, monthsArray);
                titleListView.setAdapter(adapter);

如果还没有在 onCreate 中初始化 ListView。

您可以使用 BaseAdapter。它看起来很复杂,但是一旦你理解了它,它就很容易实现

于 2013-09-18T07:42:06.503 回答