我正在使用Android Studio IDE提供的默认导航抽屉与默认列表项目,并且我想从列表中查看我的项目View,我试过这样做TextView txt = (TextView) mDrawerListView.getItemAtPosition(3);
,但应用程序在午餐中崩溃。我想知道什么是错误,以及是否有其他解决方案。如何在Android中使用getItemAtPosition获取项目引用?
我的适配器:
mDrawerListView.setAdapter(new ArrayAdapter<String>(getActionBar().getThemedContext(), android.R.layout.simple_list_item_1, android.R.id.text1,
new String[] {
getString(R.string.title_section1),
getString(R.string.title_section2),
getString(R.string.title_section3),
getString(R.string.title_section4),
getString(R.string.title_section5),
getString(R.string.title_section6)}) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView textView = (TextView) super.getView(position, convertView, parent);
// textView.setTextColor(getResources().getColor(R.color.dark_grey));
//To Set the icones
Drawable image = getResources().getDrawable(R.drawable.spots);
Drawable image2 = getResources().getDrawable(R.drawable.sessions);
TextView txt = (TextView) mDrawerListView.getItemAtPosition(3);
txt.setCompoundDrawablesWithIntrinsicBounds(image2, null, null, null);
return textView;
}
});
感谢
更新:它是正确的吗?
mDrawerListView.setAdapter(new ArrayAdapter<String>(getActionBar().getThemedContext(), android.R.layout.simple_list_item_1, android.R.id.text1,
new Info[] {
new Info(getString(R.string.title_section1), getResources().getDrawable(R.drawable.spots)),
new Info(getString(R.string.title_section2), getResources().getDrawable(R.drawable.sessions)),
new Info(getString(R.string.title_section3), getResources().getDrawable(R.drawable.calendar)),
new Info(getString(R.string.title_section4), getResources().getDrawable(R.drawable.clubs_pages)),
new Info(getString(R.string.title_section5), getResources().getDrawable(R.drawable.cv)),
new Info(getString(R.string.title_section6), getResources().getDrawable(R.drawable.cv_life))
}
'getItemAtPosition'在返回该位置的数据集的元素。将其转换为TextView将使您的应用程序崩溃,并带有'ClassCastException'。 – Blackbelt 2014-09-22 08:03:49
好的,我怎样才能得到这个元素的文本? – 2014-09-22 08:07:38
'String fourthString =(String)getItem(3)' – Blackbelt 2014-09-22 08:09:00