将图像设置为listView错误

时间:2014-09-06 02:44:32

标签: android android-listview android-imageview

应用将用户添加到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;
        }
    };

1 个答案:

答案 0 :(得分:0)

这是正确的答案,只需删除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;

        }
    };