ImageView 未在 ItemList 中更新

安全的

我正在尝试更新 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 上加载数据

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

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章