Android 将图像设置为listView错误
应用程序将用户添加到SQLite数据库,然后将在数据库中找到的所有用户检索到ListView 我想检查用户的性别,然后给他设置一个标志 使用logcat可以实现以下功能:Android 将图像设置为listView错误,android,android-listview,android-imageview,Android,Android Listview,Android Imageview,应用程序将用户添加到SQLite数据库,然后将在数据库中找到的所有用户检索到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:2
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" />
这是正确的答案,只需将其拆下,因为super已经使其膨胀 膨胀布局将返回表示xml的新根
不要在getView中为R.layout.user充气。超级电话已经让它膨胀了。只需使用row.findviewbyd即可获得图像视图。@MikeM。没错,我会在下面补充答案
@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;
}
};
{
@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;
}
};