【问题标题】:ImageView not updated in ItemListImageView 未在 ItemList 中更新
【发布时间】:2018-10-30 01:34:43
【问题描述】:

我正在尝试更新 ItemList 中的图像,我正在使用以下方法

这就是我使用以下方法获取 ImageView 'imgView' 对象的方式 -

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);
        }
    }

这段代码没有更新列表项中的取消图像,当我调试时我得到了正确的 id(即 XML 中显示的“imageViewstickId”),但图像没有更新,也没有给出任何错误

imgView.setImageResource(R.drawable.cancel);

我的 XML 代码也是 -

<?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>

我已经尝试了所有修改代码等的方法,但对我没有任何作用 任何帮助将不胜感激,谢谢

编辑 - (添加调试屏幕截图)请检查调试窗口,CardViewId 与 XML ImageView id 相同...仍未更新

【问题讨论】:

标签: android


【解决方案1】:

解决方案:

而不是这个:

imgView.setImageResource(R.drawable.cancel);

写:

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

试试看。让我们希望它有效。

【讨论】:

  • Umang..无法正常工作..listview 未更新 imageview(添加调试屏幕截图)
  • @Aman context 的值是什么你给它分配了什么?
【解决方案2】:

你可以简单地使用这个:
如果您处于活动状态,请使用此

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

或者在片段中

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

这将返回您要访问的drawable的ID...然后您可以通过执行以下操作在imageview中设置图像

 imgView.setImageResource(id);

希望这会奏效。

【讨论】:

    【解决方案3】:

    有两种最佳解决方案:-

    1) 而不是使用这个

       imgView.setImageResource(R.drawable.cancel);
    

    使用此方法:-

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

    2)使用 Glide 库 https://bumptech.github.io/glide/doc/download-setup.html

    【讨论】:

      【解决方案4】:

      你的 id 是正确的,但它没有更新,那么可能是你的资源文件 (R.java) 没有更新的问题尝试清理你的项目,然后检查。

      如果这不起作用,您还可以尝试以下代码:-

      Resources resources = getResources();
      imgView.setImageDrawable(resources.getDrawable(R.drawable.cancel));
      

      【讨论】:

        【解决方案5】:

        可能有助于其他人以这种方式思考 -

        我从这篇文章中得到了线索 - how to know when listview is finished loading data on android

        我的数据可能正在更新,但列表未完全加载,这就是数据过度写入的原因。我使用了以下代码,感谢发布上述答案的实际用户

        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();
                                }
                            }
                        }
                    });
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2023-04-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-07-18
          • 2021-01-31
          • 2012-04-17
          • 2019-03-22
          相关资源
          最近更新 更多