【问题标题】:List view not refreshing when item is deleted [duplicate]删除项目时列表视图不刷新[重复]
【发布时间】:2017-08-03 06:32:37
【问题描述】:

我有一个列表视图,我在其中加载了 json 数组数据。我有减号按钮从列表中删除行。这里的问题是我能够删除该项目,但列表视图没有得到刷新。一旦应用程序关闭或当我返回并再次导航到该活动时,列表视图就会刷新。

下面是自定义适配器类代码:

 public class AddPassengerAdapater extends BaseAdapter implements ListAdapter {

    private final JSONArray jsonArray;
   ArrayList<String> data;
    Context context;
    JSONObject json_data;
    LayoutInflater inflater;

    public AddPassengerAdapater(Context context, JSONArray jsonArray,ArrayList<String> data) {

        this.context = context;
        this.jsonArray = jsonArray;
        this.data = data;

        String details = SessionManager.getPreferences(context,"splitfare_consumer");
        data = new ArrayList<String>();
        JSONArray jsonarray = null;
        try {
            jsonarray = new JSONArray(details);
            for (int i=0; i < jsonarray.length() ; i++){
                json_data = jsonarray.getJSONObject(i);
                data.add(String.valueOf(json_data));

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

        inflater = LayoutInflater.from(this.context);
    }

    @Override public int getCount() {
        if(null==jsonArray)
            return 0;
        else
            return jsonArray.length();
    }


    @Override public JSONObject getItem(int position) {
        if(null==jsonArray) return null;
        else
            return jsonArray.optJSONObject(position);
    }

    @Override public long getItemId(int position) {
        JSONObject jsonObject = getItem(position);

        return jsonObject.optLong("id");
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final MyViewHolder mViewHolder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.addpassenger_row_layout, parent, false);
            mViewHolder = new MyViewHolder(convertView);
            convertView.setTag(mViewHolder);
        }
        else {
            mViewHolder = (MyViewHolder) convertView.getTag();
        }


         json_data = getItem(position);
        try {
            String n =  json_data.getString("name");
            mViewHolder.name.setText(n);
            String nu =  json_data.getString("number");
            mViewHolder.number.setText(nu);
        } catch (JSONException e) {
            e.printStackTrace();
        }


        mViewHolder.deletelist.setOnClickListener(new View.OnClickListener() {

            JSONArray delete_jsonarray;
            JSONObject jsonObject;
            @Override
            public void onClick(View v) {

                String phonenumber = mViewHolder.number.getText().toString();
                String jsonsetpreference_data = SessionManager.getPreferences(context,"splitfare_consumer");
                Log.d("jsonsetpreference_data:::::::","" +jsonsetpreference_data);
                try {
                    delete_jsonarray= new JSONArray(jsonsetpreference_data);
                    for (int i =0; i<= delete_jsonarray.length()-1 ; i++){
                        jsonObject = delete_jsonarray.getJSONObject(i);
                        String array_phonenumber = jsonObject.getString("number");
                        if (phonenumber.equals(array_phonenumber)){
                            delete_jsonarray.remove(i);
                        }
                    }
                    SessionManager.setPreferences(context,"splitfare_consumer",delete_jsonarray.toString());


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

            }

        });

        return convertView;
    }

    private class MyViewHolder {


        TextView name, number;
        ImageView deletelist;

        public MyViewHolder(View item) {

            name = (TextView) item.findViewById(R.id.consumername);
            number = (TextView) item.findViewById(R.id.consumer_number);
            deletelist = (ImageView) item.findViewById(R.id.dltconsumer);

        }
    }


}

Listview activity code:

        public class AddPassengerList extends AppCompatActivity {

        @BindView(R.id.listview)
        ListView listview;

        @BindView(R.id.btn_addmore)
        CustomFontButton btn_addmore;

        @BindView(R.id.donebtn)
        CustomFontButton donebtn;

        public static final String TAG = "AddPassengerList";
        Activity activity=this;
        Context context = this;
        JSONArray js;
        AddPassengerAdapater adapter;
        LayoutInflater inflater;
        String name ;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_add_passenger_list);
            ButterKnife.bind(this);

             /*Status bar color*/
            Window window = activity.getWindow();
            // clear FLAG_TRANSLUCENT_STATUS flag:
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            // add FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS flag to the window
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            // finally change the color
            window.setStatusBarColor(ContextCompat.getColor(context,R.color.colorAccent));

 String data = SessionManager.getPreferences(context,"splitfare_consumer");

            Log.d(TAG,""+data);

            ArrayList<String> details= new  ArrayList<String>();
            try {
                js = new JSONArray(data);
            } catch (JSONException e) {
                e.printStackTrace();
            }

           adapter = new AddPassengerAdapater(context,js,details);
            listview.setAdapter(adapter);

            btn_addmore.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    switch (event.getAction()){
                        case MotionEvent.ACTION_DOWN:

            btn_addmore.setBackgroundResource(R.drawable.addmorepassengerpressed);
                            break;
                        case MotionEvent.ACTION_UP:

            btn_addmore.setBackgroundResource(R.drawable.addmorepassenger);
                            Intent i = new Intent(AddPassengerList.this,AddPassenger.class);
                            startActivity(i);
                            break;
                    }
                    return false;
                }
            });

           }

       }

【问题讨论】:

  • 删除item后,调用notifyDataSetchanged()方法
  • 我这样做了,但对我没有用
  • 你的适配器类在你的活动类之外吗?
  • 是的,我有单独的课程
  • 现在你需要解释你的代码。您将一个 json 数组和空数据数组传递给您的构造函数,然后用它填充数据数组。然后你实际上是用 jsonArray 填充你的列表视图,而不是在任何地方使用数据数组,然后当你删除一个对象时,你正在从另一个数组 delete_array 中删除。这是如何工作的,请您解释一下这些数组是什么。

标签: android listview


【解决方案1】:

删除项目后再次调用您的适配器...

 mViewHolder.deletelist.setOnClickListener(new View.OnClickListener() {

        JSONArray delete_jsonarray;
        JSONObject jsonObject;
        @Override
        public void onClick(View v) {

            String phonenumber = mViewHolder.number.getText().toString();
            String jsonsetpreference_data = SessionManager.getPreferences(context,"splitfare_consumer");
            Log.d("jsonsetpreference_data:::::::","" +jsonsetpreference_data);
            try {
                delete_jsonarray= new JSONArray(jsonsetpreference_data);
                for (int i =0; i<= delete_jsonarray.length()-1 ; i++){
                    jsonObject = delete_jsonarray.getJSONObject(i);
                    String array_phonenumber = jsonObject.getString("number");
                    if (phonenumber.equals(array_phonenumber)){
                        delete_jsonarray.remove(i);
                    }
                }
                SessionManager.setPreferences(context,"splitfare_consumer",delete_jsonarray.toString());


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


    });

【讨论】:

  • 你能建议我怎么做吗?
  • 只在项目删除代码后写下这一行
  • 全局制作适配器对象
  • @NaveenKumar mViewHolder.deletelist.setOnClickListener 在此方法中删除项目后只需调用notifyDataSetChanged();
  • 感谢 Nilesh 改进我的答案..
猜你喜欢
  • 2013-10-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-25
  • 1970-01-01
  • 1970-01-01
  • 2019-08-16
  • 1970-01-01
相关资源
最近更新 更多