如何将Picasso中的ImageView设置为android中的简单适配器列表?

时间:2017-03-08 07:14:29

标签: android listview picasso

我想通过使用Picasso将Image设置为我的简单适配器ListView,我尝试了下面的代码,但它显示错误为“java.lang.IllegalArgumentException:Target不能为null”。在日志中,我该怎么做?

// MainActivity.java

StringRequest stringRequest = new StringRequest(Request.Method.POST, REGISTER_URL,
                new Response.Listener<String>() {

                    @Override
                    public void onResponse(String response) {

                        try {

                            JSONObject jsonObj = new JSONObject(response);
                            //Log.e(response,"sdsa");
                            // Getting JSON Array node

                            int iserror = jsonObj.getInt("error");
//                            Toast.makeText(MainActivity.this,iserror,Toast.LENGTH_LONG).show();

                            if (iserror == 1) {
                                String error_msg = jsonObj.getString("result");
                                Toast.makeText(Earn.this,error_msg,Toast.LENGTH_LONG).show();
                            }

                            JSONArray result = jsonObj.getJSONArray("result");
    for (int j = 0; j < result.length(); j++) {

                                        JSONObject apps_applist = result.getJSONObject(j);

                                         iddstr = apps_applist.getString("id");
                                         namestr = apps_applist.getString("name");
                                         logostr=apps_applist.getString("logo");


                                        HashMap<String, String> contact = new HashMap<>();

                                        // adding each child node to HashMap key => value
                                        contact.put("id", iddstr);
                                        contact.put("name", namestr);
                                        contact.put("logo", logostr);

                                        Picasso.with(Earn.this)
                                                .load(logostr)
                                                // .placeholder(R.drawable.placeholder)   // optional
                                                //.error(R.drawable.error)      // optional
                                                .resize(400,400)                        // optional
                                                .into((ImageView) findViewById(R.id.Img_Logo));
                                        // adding contact to contact list
                                        contactList.add(contact);

                                    }
                         ListAdapter adapter = new SimpleAdapter(Earn.this, contactList,
                                        R.layout.list_earn, new String[]{ "name","id","logo"},
                                        new int[]{R.id.textView24, R.id.textView23, R.id.Img_Logo});
                                lv.setAdapter(adapter);

// list_earn.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="1">
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.15">


        <ImageView
            android:layout_width="113dp"
            android:layout_height="107dp"
            app:srcCompat="@mipmap/ic_launcher"
            android:id="@+id/Img_Logo" />

    <TextView
        android:text="TextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView23"
        android:layout_weight="1"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="18dp" />

    <TextView
        android:text="TextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:id="@+id/textView24"
        android:layout_below="@+id/textView23"
        android:layout_alignLeft="@+id/textView23"
        android:layout_alignStart="@+id/textView23"
        android:layout_marginTop="23dp" />

    <Button
        android:text="Button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/button4"
        android:background="@drawable/button_format"
        android:layout_below="@+id/Img_Logo"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="31dp" />


</RelativeLayout>

</LinearLayout>

// activity_main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin">


    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

0 个答案:

没有答案