【问题标题】:Setting image to a listView error将图像设置为 listView 错误
【发布时间】:2014-10-31 00:32:33
【问题描述】:

应用将用户添加到SQLite database,然后将在数据库中找到的所有用户检索到ListView

我想检查用户的性别,然后给他设置一个标志。

使用logcat 给出这个:

09-06 05:17:24.295: D/sexlogo(25590): sexlogo >>>android.widget.ImageView@424d2618
09-06 05:17:24.295: D/s(25590):  s>>>>>Male
09-06 05:17:24.295: D/sexxx(25590):  sexxx>>>>android.widget.LinearLayout@424d07f8

所以所有数据都被很好地检索到了,所有缺少的只是将它们设置在imageView

这是 User.xml :

 <ImageView
        android:id="@+id/sex"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:layout_marginTop="-67dp"
        android:src="@drawable/ic_launcher" />

我正在使用LayoutInflator 获取ImageView,下面的代码在mailActivity.java

@SuppressWarnings("deprecation")
    SimpleCursorAdapter adapter = new SimpleCursorAdapter(
            MainActivity.this, R.layout.user, cursor, from, to)

    {

        @Override
        public View getView(int position, View convertView, ViewGroup parent)
        {
            cursor.moveToPosition(position);
            String s = cursor.getString(cursor.getColumnIndex("USER_SEX"));
            final View row = super.getView(position, convertView, parent);

                LayoutInflater inflater = (LayoutInflater) getApplicationContext()
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

                View sexxx = inflater.inflate(R.layout.user, null);
                sexlogo = (ImageView) sexxx.findViewById(R.id.sex);

            if (s.equalsIgnoreCase("Male"))
                sexlogo.setImageResource(R.drawable.male);

            else 
                sexlogo.setImageResource(R.drawable.female);
            return row;
        }
    };

【问题讨论】:

  • 不要在getView() 中膨胀R.layout.usersuper 电话已经在膨胀它。只需使用row.findViewById() 即可获取 ImageView。
  • @MikeM。确切地说,我将在下面添加答案

标签: android android-listview android-imageview


【解决方案1】:

这是正确答案,只需删除 LayoutInflater,因为 super 已经膨胀了它。

膨胀layout 将返回一个新的root代表xml

{

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            cursor.moveToPosition(position);
            String s = cursor.getString(cursor.getColumnIndex("USER_SEX"));
            final View row = super.getView(position, convertView, parent);

            sexlogo = (ImageView) row.findViewById(R.id.sex);

            if (s.equals("Male"))
                sexlogo.setImageResource(R.drawable.male);

            else
                sexlogo.setImageResource(R.drawable.female);

            return row;

        }
    };

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多