z.al
z.al

Reputation: 279

image icon not showing in Listview Android?

I am having a listview where items are coming from database. I tried to putting image icon for listview item in xml. but, it's not showing the icon.i tried many things but nothing seems to work on this?

Here is my Listview :

public class TypeMenu extends AppCompatActivity {

    private String TAG = TypeMenu.class.getSimpleName();
    String bid;

    private ProgressDialog pDialog;
    private ListView lv;
    private static final String TAG_BID = "bid";

    // URL to get contacts JSON
    private static String url = "http://cloud....com/brtemp/index.php";

    ArrayList<HashMap<String, String>> contactList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_type_menu);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);




        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        contactList = new ArrayList<>();

        lv = (ListView) findViewById(R.id.list);

        new GetContacts().execute();


        // on seleting single product
        // launching Edit Product Screen
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                // getting values from selected ListItem

                HashMap<String, String> selected = contactList.get(position);
                String selectedId= selected.get("id");
                Intent in = new Intent(getApplicationContext(), SubMenu.class);
                //  sending pid to next activity
                in.putExtra("id",selectedId);
                startActivityForResult(in, 100);
                Toast.makeText(getApplicationContext(),"Toast" +selectedId ,Toast.LENGTH_SHORT).show();
            }
        });
    }

    /**
     * Async task class to get json by making HTTP call
     */
    private class GetContacts extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            // Showing progress dialog
            pDialog = new ProgressDialog(TypeMenu.this);
            pDialog.setMessage("Please wait...");
            pDialog.setCancelable(false);
            pDialog.show();
           // Toast.makeText(getApplicationContext(),"Toast",Toast.LENGTH_LONG).show();

        }

        @Override
        protected Void doInBackground(Void... arg0) {
            HttpHandler sh = new HttpHandler();

            // Making a request to url and getting response
         String jsonStr = sh.makeServiceCall(url);

            Log.e(TAG, "Response from url: " + jsonStr);





            if (jsonStr != null) {
                try {
                    JSONArray jsonArry = new JSONArray(jsonStr);

                    for (int i = 0; i < jsonArry.length(); i++)
                    {

                        JSONObject c = jsonArry.getJSONObject(i);
                        String id = c.getString("id");
                        String type = c.getString("type");
                        HashMap<String, String> contact = new HashMap<>();

                       contact.put("id", id);
                        contact.put("type", type);
                        contactList.add(contact);


                    }
                } catch (final JSONException e) {
                    Log.e(TAG, "Json parsing error: " + e.getMessage());
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(getApplicationContext(),
                                    "Json parsing error: " + e.getMessage(),
                                    Toast.LENGTH_LONG)
                                    .show();
                        }
                    });

                }
            } else {
                Log.e(TAG, "Couldn't get json from server.");
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(getApplicationContext(),
                                "Couldn't get json from server. Check LogCat for possible errors!",
                                Toast.LENGTH_LONG)
                                .show();
                    }
                });

            }

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            // Dismiss the progress dialog
            if (pDialog.isShowing())
                pDialog.dismiss();
            /**
             * Updating parsed JSON data into ListView
             * */
            ListAdapter adapter = new SimpleAdapter(
                    TypeMenu.this, contactList,
                    R.layout.list_item, new String[]{ "type","id"},
                    new int[]{
                    R.id.type,R.id.arrow1});

            lv.setAdapter(adapter);
        }

}
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                onBackPressed();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
        }

above code if i put " R.id.Type, R.id.arrow1" then list lis not opening and giving me error.

here is my Listview.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    >

    <!-- Product id (pid) - will be DDEN - used to pass to other activity -->



        <TextView
            android:id="@+id/id"
            android:layout_width="30dp"
            android:layout_height="30dp"/>

        <TextView
            android:id="@+id/type"
            android:textSize="20dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>


    <ImageView
        android:id="@+id/arrow1"
        android:layout_width="match_parent"
        android:layout_height="15dp"
        android:layout_gravity="center"
        android:layout_weight="3"
        android:src="@drawable/dropdown" />

    </LinearLayout>

here is my logcat:

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.example.zeba.broccoli, PID: 1231
                  android.content.res.Resources$NotFoundException: Resource ID #0x1
                      at android.content.res.Resources.getValue(Resources.java:2452)
                      at android.support.v7.widget.AppCompatDrawableManager.loadDrawableFromDelegates(AppCompatDrawableManager.java:330)
                      at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:195)
                      at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:188)
                      at android.support.v7.content.res.AppCompatResources.getDrawable(AppCompatResources.java:100)
                      at android.support.v7.widget.AppCompatImageHelper.setImageResource(AppCompatImageHelper.java:73)
                      at android.support.v7.widget.AppCompatImageView.setImageResource(AppCompatImageView.java:81)
                      at android.widget.SimpleAdapter.setViewImage(SimpleAdapter.java:262)
                      at android.widget.SimpleAdapter.bindView(SimpleAdapter.java:192)
                      at android.widget.SimpleAdapter.createViewFromResource(SimpleAdapter.java:126)
                      at android.widget.SimpleAdapter.getView(SimpleAdapter.java:114)
                      at android.widget.AbsListView.obtainView(AbsListView.java:2828)
                      at android.widget.ListView.measureHeightOfChildren(ListView.java:1292)
                      at android.widget.ListView.onMeasure(ListView.java:1204)
                      at android.view.View.measure(View.java:18870)
                      at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:728)
                      at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:464)
                      at android.view.View.measure(View.java:18870)
                      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5908)
                      at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:703)
                      at android.support.design.widget.HeaderScrollingViewBehavior.onMeasureChild(HeaderScrollingViewBehavior.java:90)
                      at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onMeasureChild(AppBarLayout.java:1367)
                      at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:768)
                      at android.view.View.measure(View.java:18870)
                      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5908)
                      at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
                      at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
                      at android.view.View.measure(View.java:18870)
                      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5908)
                      at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1435)
                      at android.widget.LinearLayout.measureVertical(LinearLayout.java:721)
                      at android.widget.LinearLayout.onMeasure(LinearLayout.java:612)
                      at android.view.View.measure(View.java:18870)
                      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5908)
                      at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
                      at android.view.View.measure(View.java:18870)
                      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5908)
                      at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1435)
                      at android.widget.LinearLayout.measureVertical(LinearLayout.java:721)
                      at android.widget.LinearLayout.onMeasure(LinearLayout.java:612)
                      at android.view.View.measure(View.java:18870)
                      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5908)
                      at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
                      at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:3076)
                      at android.view.View.measure(View.java:18870)
                      at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2392)
                      at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1416)
                      at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1661)
                      at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1301)
                      at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7016)
                      at android.view.Choreographer$CallbackRecord.run(Choreographer.java:777)
                      at android.view.Choreographer.doCallbacks(Choreographer.java:590)
                      at android.view.Choreographer.doFrame(Choreographer.java:560)
                    at android.view.Choreographer$FrameDisplayEventRec
I/Process: Sending signal. PID: 1231 SIG: 9
Application terminated.

Upvotes: 1

Views: 854

Answers (2)

Chaitanya Atkuri
Chaitanya Atkuri

Reputation: 1672

@z.al I understand your situation, but to make you sure, SimpleAdapter just helps in populating string type value to the text view specified by ID.

But in your case, you should implement a custom ArrayAdapter and in getView() method you can set the image icon to the imageView.

Following is simple snippet to make you clear :

public class Person {

public HashMap<String, String> contact;
void Person(HashMap<String, String> contact) {
this.contact = contact;
}
}

public class MyListAdapter extends ArrayAdapter<Person> {

private Context context;
private ArrayList<Person> contactList;

private LayoutInflater mInflater;
private boolean mNotifyOnChange = true;

public MyListAdapter(Context context, ArrayList<Person> contactList) {
    super(context, R.layout.my_row_layout);
    this.context = context;
    this.contactList = new ArrayList<>(contactList);
    this.mInflater = LayoutInflater.from(context);
}

@Override
public int getCount() {
    return contactList.size();
}

@Override
public Person getItem(int position) {
    return contactList.get(position);
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    final ViewHolder holder;
    if (convertView == null) {
        holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.my_row_layout,parent, false);
            holder.name = (TextView) convertView.findViewById(R.id.textview_name);
            holder.description = (TextView) convertView.findViewById(R.id.textview_description);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

Person person = contactList.get(position);
    holder.name.setText(person.get("name"));
    holder.image.setImageResource(person.get("id"));
    holder.pos = position;
    return convertView;
}

//---------------static views for each row-----------//
     static class ViewHolder {

         TextView name;
         TextView description;
         ImageView image;
         int pos; //to store the position of the item within the list
     }
}

Upvotes: 1

Karakuri
Karakuri

Reputation: 38605

The problem is SimpleAdapter is too simple for your use case. The documentation explains that it will eventually call setViewImage(ImageView, String) to set the image; what it does not tell you is it expects that String to be converted to an integer via Integer.parseInt() (unfortunately the documentation is not very good in this regard).

You need to inform your adapter how to bind data to rows in your ListView. You can do this minimally by providing it a ViewBinder by calling setViewBinder(). However, my recommendation is that you write a custom adapter implementation by extending BaseAdapter, because my experience has been that people quickly exceed the very simplistic usage provided by the framework adapters.

As an aside, perhaps you should use this opportunity to switch to RecyclerView since ListView is old and clunky.

Upvotes: 0

Related Questions