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

拉杰万特·考尔·布旺(Rajwant Kaur Boughan)

我正在从php网址获取一些数据,并在Custom ListView上显示该数据。在“类别”列表视图中,用户将选择一个类别复选框,然后根据所选项目,我从另一个URL发送该项目的ID,以获取“ subCategories”,并且在另一个listView中显示该子类别项目。但是问题是,每当我单击另一个复选框时,子类别项目就会被新获取的项目替换。.我想显示与所选“类别”复选框相对应的所有子类别...请帮助我,我该怎么做?这是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());
                    }
                }); 

这就是我获取价值的方式。私人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上显示项目的方式:private void 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();
        }
    });

}

这是我的适配器类私有类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;

    }

}

这是输出:复选框图像

bar原

试试这个:-

步骤1:在方法“ displaySubCategory”中,删除“ ArrayList CategoryList = new ArrayList();”行。通过此方法,然后在该页面上全局添加同一行。这将解决您的问题。但是为适配器创建多个实例不是正确的方法。因此,也请遵循第二步。

第2步:在方法“ displaySubCategory”中,您将为SubCategoryAdapter创建新的实例“ subCategory = new SubCategoryAdapter(this,R.layout.sub_category_items,CategoryList);”。

您仅应在页面中一次为适配器“ SubCategoryAdapter”创建实例。

删除行“ subCategory = new SubCategoryAdapter(this,R.layout.sub_category_items,CategoryList);” 来自“ displaySubCategory”方法。并在onCreateView / onCreate事件中添加同一行。

添加此行“ subCategory.notifyDataSetChanged();” 在“ displaySubCategory”方法中。

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

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

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用自定义baseadapter从listView中删除项目

在 WPF 中向 ListView 添加项目(自定义“项目”)

如何在WebStorm中添加“自定义”项目模板

如何将项目动态添加到ListView的自定义适配器

Android-在添加ArrayList中的项目后动态更新自定义ListView

如何在ListView中添加/删除项目?

如何在自定义 ListView 上创建 SearchView/过滤器并使用 EditText 在底层 ArrayList 中添加项目?

如何在C ++中从集中删除项目而不删除项目?

如何从Android Studio中的自定义插件的操作中动态添加项目中的依赖项?

如何在Xcode项目中添加和删除现有的自定义键盘扩展?

如何在Flutter中将自定义小部件项目填充到ListView中?

从列表中删除以前选择的项目

从自定义列表中删除项目

如何在android主题声明中添加自定义项目?

如何在Linq查询或Linq查询结果中添加自定义项目

如何在Android中为任何新项目自动添加自定义依赖项?

如何在Eclipse中添加针对每个创建的项目构建的自定义文件?

如何在Qt Creator项目向导中添加自定义构建步骤?

如何在handsontable的上下文菜单中添加具有默认项目的自定义项目

如何在Javascript中添加参数并删除以前的参数

如何在PHP中动态添加自定义方法

如何在表单中动态添加自定义控件

如何在NavigationView中自定义项目背景和项目文本颜色?

如何在代码后面将自定义背景设置为ListView项目颜色?

在自定义模型中为QTableView添加和删除项目

如何在一个Recyclerview中添加两个自定义行而不替换项目?

如何在购物车页面中添加/更新自定义购物车项目数据

如何在使用sql数据源时在下拉列表中添加自定义项目

如何在WPF棱镜项目中添加可在所有ViewModel中访问的自定义服务?