删除项目时列表视图不刷新

时间:2017-08-03 06:32:38

标签: android listview

我有一个列表视图,我用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;
                }
            });

           }

       }

1 个答案:

答案 0 :(得分:0)

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

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


    });