如何在Android中使用getItemAtPosition获取项目引用?

时间:2014-09-22 07:55:17

标签: android

我正在使用Android Studio IDE提出的默认导航抽屉,使用默认列表项,我想从列表视图中获取i项目,我已尝试执行此操作TextView txt = (TextView) mDrawerListView.getItemAtPosition(3);但是应用程序在午餐时坠毁。我想知道错误是什么,以及是否有其他解决方案。

我的适配器:

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))
                         }

1 个答案:

答案 0 :(得分:0)

而不是拥有ArrayAdapter,如果你想为ListView的每个子项自定义文本和图标,我宁愿使用一个名为Info的bean对象,它包含那些信息 (ArrayAdapter<Info>)。例如

class Info {

  String mText;
  Drawable mDrawable;
  public Info(String text, Drawable dra) {
     mText = text;
     mDrawable = dra;
  }
}

和适配器:

new ArrayAdapter<Info>(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.first_drawable),
// other elements

和getView

TextView textView = (TextView) super.getView(position, convertView, parent);
Info info = (Info) getItem(position);
textView.setText(info.mText);
textView.setCompoundDrawablesWithIntrinsicBounds( info.mDrawable, null, null, null);

请检查拼写错误