这是布局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”的字符串数组,我需要将其显示为列表视图。
答案 0 :(得分: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。它看起来很复杂,但一旦你理解它,它就很容易实现