如何在不删除以前的项目的情况下在自定义 ListView 中动态添加项目

how to add items dynamically in custom ListView without erasing previous items

我正在从 php url 获取一些数据并在自定义 ListView 上显示该数据。在 'category' listView 中,用户将 select 一个类别复选框并进一步根据 selected 项目我从另一个 url 发送该项目的 id 以获取 'subCategories'我在另一个 listView 中显示该子类别项目。但问题是每当我单击另一个复选框时,子类别项目将被新获取的项目替换。我想显示对应于 selected 'category' 复选框的所有子类别...请帮助我如何才能我这样做吗? 这是 'category' 复选框的 onClickListener。

    holder.name.setOnClickListener( new View.OnClickListener() {
                    public void onClick(View v) {
                        CheckBox cb = (CheckBox) v ;
                        Category Category = (Category) cb.getTag();
                    /*Toast.makeText(getApplicationContext(),"Clicked on Checkbox: " + cb.getText() +" is " + cb.isChecked(),
                            Toast.LENGTH_LONG).show();*/
                        category_id=cb.getText().toString();
                        receiveSubCategory();
                        Category.setSelected(cb.isChecked());
                    }
                }); 

这是我获取值的方式。 private void receiveSubCategory(){

    StringRequest stringRequest = new StringRequest(Request.Method.POST,Sub_Cate_URL,new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            //Log.d("sub response", response);
            receiveSubJSOn(response);
        }
    },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    String er=error.getMessage();
                    Toast.makeText(UpdateUserProfile.this,er,Toast.LENGTH_LONG).show();
                    //Log.d("","error message"+er);
                }
            }){

    @Override
    protected Map<String,String> getParams(){
        Map<String,String> params = new HashMap<String, String>();
        params.put("category_id",category_id);
        return params;
    }

};


RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);
}

private void receiveSubJSOn(String json){
    ParseJSON pj = new ParseJSON(json);
    pj.receiveSubCategory();
    sub_category_id=ParseJSON.sub_id;
    sub_cate_name=ParseJSON.sub_name;
    displaySubCategory();

这是我在 listView 上显示项目的方式: 私人无效 displaySubCategory() {

    //Array list of categories
    ArrayList<SubCategory> CategoryList = new ArrayList<SubCategory>();
    //sub_category_id=temp_id;
    //sub_cate_name=temp_name;
    for(int i=0;i<sub_cate_name.length;i++) {
        SubCategory Category = new SubCategory(sub_cate_name[i], sub_category_id[i], false);
        CategoryList.add(Category);

    }

    //create an ArrayAdaptar from the String Array
    subCategory = new SubCategoryAdapter(this,R.layout.sub_category_items, CategoryList);
    ListView listView = (ListView) findViewById(R.id.sub_category_list);
    // Assign adapter to ListView
    listView.setAdapter(subCategory);


    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {
            // When clicked, show a toast with the TextView text
            Category Category = (Category) parent.getItemAtPosition(position);
            Toast.makeText(getApplicationContext(),
                    "Clicked on Row: " + Category.getName(),
                    Toast.LENGTH_LONG).show();
        }
    });

}

这是我的适配器 Class 私有 class SubCategoryAdapter 扩展 ArrayAdapter {

    private ArrayList<SubCategory> CategoryList;
    private String[] ids;

    public SubCategoryAdapter(Context context, int textViewResourceId,ArrayList<SubCategory> CategoryList) {
        super(context, textViewResourceId, CategoryList);
        this.CategoryList = new ArrayList<SubCategory>();
        this.CategoryList.addAll(CategoryList);
        // ids=id;
    }

    private class ViewHolder {
        TextView code;
        CheckBox name;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder = null;
        //Log.v("ConvertView", String.valueOf(position));

        if (convertView == null) {
            LayoutInflater vi = (LayoutInflater)getSystemService(
                    Context.LAYOUT_INFLATER_SERVICE);
            convertView = vi.inflate(R.layout.sub_category_items, null);

            holder = new ViewHolder();
            holder.code = (TextView) convertView.findViewById(R.id.sub_list_text);
            holder.name = (CheckBox) convertView.findViewById(R.id.sub_category_checkbox);
            convertView.setTag(holder);

            holder.name.setOnClickListener( new View.OnClickListener() {
                public void onClick(View v) {
                    CheckBox cb = (CheckBox) v ;
                    SubCategory Category = (SubCategory) cb.getTag();
                   /* Toast.makeText(getApplicationContext(),
                            "Clicked on Checkbox: " + cb.getText() +
                                    " is " + cb.isChecked()Toast.LENGTH_LONG).show();*/
                    Category.setSelected(cb.isChecked());
                }
            });
        }
        else {
            holder = (ViewHolder) convertView.getTag();
        }

        SubCategory Category = CategoryList.get(position);
        holder.code.setText(""+Category.getCode()+"");

        holder.name.setText(Category.getName());
        holder.name.setChecked(Category.isSelected());
        holder.name.setTag(Category);

        return convertView;

    }

}

这是输出: checkboxes image

试试这个:-

第一步: 在方法 "displaySubCategory" 中,从此方法中删除行 "ArrayList CategoryList = new ArrayList();" 并在该页面上全局添加相同的行。这将解决您的问题。但是为适配器创建多个实例不是正确的方法。所以也跟着第二步走。

第 2 步: 在方法 "displaySubCategory" 中,您正在为 SubCategoryAdapter "subCategory = new SubCategoryAdapter(this,R.layout.sub_category_items, CategoryList);"

创建新的瞬间

您应该只在一个页面中为适配器 "SubCategoryAdapter" 创建一次。

从 "displaySubCategory" 方法中删除行 "subCategory = new SubCategoryAdapter(this,R.layout.sub_category_items, CategoryList);"。并在 onCreateView/onCreate 事件中添加相同的行。

在 "displaySubCategory" 方法中添加这一行 "subCategory.notifyDataSetChanged();"。