ImageView can not display image using SimpleAdapter

85 Views Asked by At

MainActivity.java

public class MainActivity extends AppCompatActivity {
    private String[] names = new String[]{"name1", "name2", "name3"};
    private String[] says = new String[]{"desc1", "desc2", "desc3"};
    private int[] imgIds = new int[]{R.mipmap.head_icon1, R.mipmap.head_icon2, R.mipmap.head_icon3};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        List<Map<String, Object>> listitem = new ArrayList<Map<String, Object>>();
        for (int i = 0; i < names.length; i++) {
            Map<String, Object> showitem = new HashMap<String, Object>();
            showitem.put("touxiang", imgIds[i]);
            showitem.put("name", names[i]);
            showitem.put("says", says[i]);
            listitem.add(showitem);
        }

        //create one simpleAdapter
        SimpleAdapter myAdapter = new SimpleAdapter(getApplicationContext(), listitem, R.layout.list_item, new String[]{"touxiang", "name", "says"}, new int[]{R.id.imgtou, R.id.name, R.id.says});
        ListView listView = (ListView) findViewById(R.id.list_test);
        listView.setAdapter(myAdapter);

    }
}

list_item.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">
    <ImageView
        android:id="@+id/imgtou"
        android:layout_width="64dp"
        android:layout_height="64dp"
        android:baselineAlignBottom="true"
        android:paddingLeft="8dp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="8dp"
            android:textColor="#1D1D1C"
            android:textSize="20sp" />

        <TextView
            android:id="@+id/says"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="8px"
            android:textColor="#B4B4B9"
            android:textSize="14sp" />
    </LinearLayout>
</LinearLayout>

but these images can be recognized by android studio.
enter image description here enter image description here

anyone can help to find the problem?

2

There are 2 best solutions below

0
Daniel.Wang On

You've used SimpleAdapter in your code. But usage was wrong. SimpleAdapter haven't image id as parameter.

Please look following URL.
https://developer.android.com/reference/android/widget/SimpleAdapter

public SimpleAdapter (Context context, 
            List<? extends Map<String, ?>> data, 
            int resource, 
            String[] from, 
            int[] to)
  • context(Context): The context where the View associated with this SimpleAdapter is running data List: A List of Maps. Each entry in the List corresponds to one row in the list. The Maps contain the data for each row, and should include all the entries specified in "from"
  • resource(int): Resource identifier of a view layout that defines the views for this list item. The layout file should include at least those named views defined in "to"
  • from(String): A list of column names that will be added to the Map associated with each item.
  • to(int): The views that should display column in the "from" parameter. These should all be TextViews. The first N views in this list are given the values of the first N columns in the from parameter.

But you used image ids array to "to" fields.

//create one simpleAdapter
SimpleAdapter myAdapter = new SimpleAdapter(
   getApplicationContext(),
   listitem,
   R.layout.list_item,
   new String[]{"touxiang", "name", "says"},
   new int[]{R.id.imgtou, R.id.name, R.id.says} //You used wrong values. This should be textview ids.
);

So you cannot use SimpleAdapter with your purpose.

You should customize Adapter class to your purpose.

0
UncagedMist On
SimpleAdapter myAdapter = new SimpleAdapter(
getApplicationContext(), 
listitem, 
R.layout.list_item,
new String[]{"touxiang", "name", "says"}, 
new int[]{R.mipmap.head_icon1, R.mipmap.head_icon2, R.mipmap.head_icon3}
);

Or you can use Picasso Library for setting and Loading Images.