【发布时间】: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.user。super电话已经在膨胀它。只需使用row.findViewById()即可获取 ImageView。 -
@MikeM。确切地说,我将在下面添加答案
标签: android android-listview android-imageview