我想制作一个简单的listview,其中包含2个textview和一个imageView。 Textviews显示正确的文本,但imageview显示xml文件中设置的背景。
.java文件:-
String laptops[] = {"DELL Inspiron 15", "Mosiso smooth Matte Finish", "HP
455 ", "MicroMax 1160", "Asus Eeebook"};
String keys[]={"name","price","image"}; //from array
int price[]={20000,30000,23980,40000,28000};
int images[]=
{R.drawable.lap,R.drawable.lapt,R.drawable.lapt,R.drawable.lapto,R.drawable.laptop};
int x=R.drawable.lapt;
int[] to={R.id.textView3,R.id.textView4,R.id.imageView2};
ArrayList<HashMap<String,String>> al=new ArrayList<HashMap<String, String>>();
for(int i=0;i<5;i++)
{
HashMap<String,String> h=new HashMap<String, String>();
h.put("name",laptops[i]);
h.put("price",price[i]+" ");
h.put("image",images[i]+" ");
al.add(h);
}
ListView lv = (ListView) findViewById(R.id.list);
SimpleAdapter sa=new SimpleAdapter(this,al,R.layout.new_manual,keys,to);
lv.setAdapter(sa);
}
new_manual是用于列表视图的资源文件。
new_manual.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="33dp"
android:layout_marginTop="92dp"
android:text="TextView"
android:layout_toLeftOf="@id/textView4"/>
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignTop="@+id/textView3"
android:layout_marginEnd="137dp"
android:text="TextView" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_marginEnd="17dp"
android:layout_marginTop="75dp"
app:srcCompat="@mipmap/ic_launcher"
android:background="@drawable/ic_launcher_background"/>
</RelativeLayout>
activity_main.xml:-
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="16dp"
android:divider="@color/Grey"
android:dividerHeight="2dp"
android:footerDividersEnabled="true"
android:headerDividersEnabled="true"
/>
</RelativeLayout>
输出显示以下内容:-{screen of app
我希望在图像数组中指定图像,而不是列表视图中的ic_launcher_background。 在此先感谢:)