0

I am trying to update the image inside ItemList, I am using the below method

This is how I am getting ImageView 'imgView' Object using the below methods -

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(JSONParsingMainActivity.this, contactList,
                R.layout.list_item, new String[]{"name", "email", "mobile"}, new int[]{R.id.name,
                R.id.email, R.id.mobile});


        lv.setAdapter(adapter);

        if(markedQuestion != null){
            try {
                JSONObject jsonObj = new JSONObject(markedQuestion);
                JSONArray contacts = jsonObj.getJSONArray("data");
                for (int i = 0; i < contacts.length(); i++) {
                    JSONObject c = contacts.getJSONObject(i);

                    String questionid = c.getString("questionid");
                    System.out.println("Question No :" + i + "Question Id is :" + questionid);


                    Long itemId = lv.getItemIdAtPosition(Integer.parseInt(questionid));
                    System.out.println("Item Id xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxasdasd" + itemId);


                    changedView = (CardView) getViewByPosition(Integer.parseInt(questionid),lv);
                    imgView = (ImageView) changedView.findViewById(R.id.imageViewstickId);
                    imgView.setImageResource(R.drawable.cancel);

                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }


        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                mp.start();
                position++;
                Intent intent = new Intent(context, DetailsActivity.class);
                intent.putExtra("position", Integer.toString(position));
                context.startActivity(intent);
            }
        });


    }

    public View getViewByPosition(int pos, ListView listView) {
        final int firstListItemPosition = listView.getFirstVisiblePosition();
        final int lastListItemPosition = firstListItemPosition + listView.getChildCount() - 1;

        if (pos < firstListItemPosition || pos > lastListItemPosition ) {
            return listView.getAdapter().getView(pos, null, listView);
        } else {
            final int childIndex = pos - firstListItemPosition;
            return listView.getChildAt(childIndex);
        }
    }

This code is not updating the cancel image in the list item, when I debug I am getting correct id (i.e. 'imageViewstickId' as shown in XML), but the image is not updating, nor giving any error

imgView.setImageResource(R.drawable.cancel);

My XML code also -

<?xml version="1.0" encoding="utf-8"?>

<android.support.v7.widget.CardView
    android:id="@+id/cardViewQuestionId"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:card_view="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="8dp"
    app:cardCornerRadius="15dp"
    card_view:cardElevation="2dp"
    card_view:cardCornerRadius="5dp">

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="10dp">

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

     <TextView
         android:id="@+id/name"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:paddingBottom="2dp"
         android:paddingTop="6dp"
         android:text="test"
         android:textColor="@color/gradientStart"
         android:textSize="16sp"
         android:textStyle="bold"
         />


     <ImageView
         android:id="@+id/imageViewstickId"
         android:layout_width="30dp"
         android:layout_height="30dp"
         android:layout_gravity="center"
         />

 </LinearLayout>

    <TextView
        android:id="@+id/email"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="2dip"
        android:textColor="@color/gradientStop" />

    <TextView
        android:id="@+id/mobile"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#5d5d5d"
        android:textStyle="bold" />

</LinearLayout>
</android.support.v7.widget.CardView>

I have tried everything modifying the code, etc. but nothing works for me Any help shall be appreciated, thanks

Edits - (Added Debug screen Shot) Please check in debug window, CardViewId is same as XML ImageView id... Still not updating

enter image description here

Faysal Ahmed
  • 7,501
  • 5
  • 28
  • 50
Aman
  • 137
  • 1
  • 2
  • 9

5 Answers5

0

Solution:

Instead of this:

imgView.setImageResource(R.drawable.cancel);

write:

imgView.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.cancel));

Try it. Let's hope it works.

Ümañg ßürmån
  • 9,695
  • 4
  • 24
  • 41
0

You can simply use this:
If you are in Activity then use this

int id = getResources().getIdentifier("cancel","drawable", getPackageName());

Or in Fragment

int id = context.getResources().getIdentifier("cancel","drawable", context.getPackageName());
 // "cancel" drawable resource name
 // "drawable" drawable directory

This will return the id of the drawable you want to access... then you can set the image in the imageview by doing the following

 imgView.setImageResource(id);

hope this will work.

Faysal Ahmed
  • 7,501
  • 5
  • 28
  • 50
0

There are two best solutions for that:-

1) Instead of using this

   imgView.setImageResource(R.drawable.cancel);

Use This Method:-

   imgView.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.cancel));

2)Using Glide Library https://bumptech.github.io/glide/doc/download-setup.html

Faysal Ahmed
  • 7,501
  • 5
  • 28
  • 50
Rahul Kushwaha
  • 5,473
  • 3
  • 26
  • 30
0

Your id is correct still it is not updating then it might be a problem that your Resource file (R.java) is not updating try cleaning your project and then check.

If this not works you can also try following code:-

Resources resources = getResources();
imgView.setImageDrawable(resources.getDrawable(R.drawable.cancel));
Chirag Sharma
  • 888
  • 6
  • 18
0

May be helpful for others to think this way -

I got Clue from this post - how to know when listview is finished loading data on android

My data may be getting updated, but the list was not fully loaded and thats why the data is over writing. I used the following code , thanks for the actual user who posted the above answer

lv.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
                @Override
                public void onLayoutChange(View view, int i, int i1, int i2, int i3, int i4, int i5, int i6, int i7) {

                    Log.d(TAG, "list got updated, do what ever u want");


                    if(markedQuestion != null){
                        try {
                            JSONObject jsonObj = new JSONObject(markedQuestion);
                            JSONArray contacts = jsonObj.getJSONArray("data");
                            for (int x = 0; x < contacts.length(); x++) {
                                JSONObject c = contacts.getJSONObject(x);

                                String questionid = c.getString("questionid");
                                System.out.println("Question No :" + x + "Question Id is :" + questionid);


                                System.out.println(getResources());
                                changedView = (CardView) getViewByPosition(Integer.parseInt(questionid),lv);
                                imgView = (ImageView) changedView.findViewById(R.id.imageViewstickId);
                                //  imgView.setImageResource(R.drawable.cancel);
                                imgView.setImageDrawable(ContextCompat.getDrawable(context,R.drawable.cancel));

                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }
            });
Aman
  • 137
  • 1
  • 2
  • 9