我正在使用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