doufen3838 2016-10-28 06:24
浏览 57
已采纳

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

i am fetching some data from php url and showing that data on Custom ListView. In 'category' listView, user will select a category checkbox and further according to selected item i am sending id of that item from another url to fetch 'subCategories' and i am showing that sub category items in another listView. But the problem is whenever i click on another checkbox the subCategory items replaced by the newly fetched items.. i want to show all subcategories corresponding to selected 'category' checkboxes... Please Please help me how can i do that? here is onClickListener for 'category' checkbox.

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

Here is how i am getting values. 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();

here is how i am showing items on 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();
        }
    });

}

Here is my Adapter Class private class SubCategoryAdapter extends 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;

    }

}

here is Output: checkboxes image

  • 写回答

1条回答 默认 最新

  • doulu7921 2016-10-28 07:52
    关注

    Try this:-

    Step 1: In method "displaySubCategory", Remove the line "ArrayList CategoryList = new ArrayList();" from this method and add the same line globally on that page. This will fix your problem. But creating multiple instance for an adaptor is not a correct way. So follow the second step also.

    Step 2: In method "displaySubCategory", you are creating new instants for SubCategoryAdapter "subCategory = new SubCategoryAdapter(this,R.layout.sub_category_items, CategoryList);"

    You should create the instants for the adaptor "SubCategoryAdapter" once in a page only.

    Remove the line "subCategory = new SubCategoryAdapter(this,R.layout.sub_category_items, CategoryList);" from "displaySubCategory" method. And add the same line in onCreateView/onCreate event.

    Add this line "subCategory.notifyDataSetChanged();" in "displaySubCategory" method.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP