2016-10-28 11 views
0

私はphp urlからデータを取り出して、そのデータをカスタムListViewに表示しています。 'category' listViewでは、ユーザーはcategoryチェックボックスを選択し、選択したアイテムに応じて、別のurlからそのアイテムのIDを送信して 'subCategories'を取得し、そのサブcategoryアイテムを別のlistViewに表示します。しかし、問題は、別のチェックボックスをクリックするたびに、新しく取得されたアイテムによって置き換えられたサブカテゴリの項目です。選択した 'カテゴリ'のチェックボックスに対応するすべてのサブカテゴリを表示したいのです...どうすればいいですか? ここには 'カテゴリ'チェックボックスのonClickListenerがあります。前の項目を消去せずにカスタムListViewで項目を動的に追加する方法

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

は私がリストビューの項目を示しています方法ですここ

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; 

    } 

} 

が出力されます。 "ArrayListのCategoryList =新しいArrayListを();" 行を削除し、 "displaySubCategory" 方法で :

工程1 - :

答えて

0

はこれを試してみてくださいこのメソッドから同じ行をそのページにグローバルに追加します。これで問題は解決します。しかし、アダプターの複数のインスタンスを作成することは正しい方法ではありません。それで、2番目のステップにも従ってください。

手順2: メソッド "displaySubCategory"では、SubCategoryAdapter "subCategory = new SubCategoryAdapter(this、R.layout.sub_category_items、CategoryList);"の新しいインスタントを作成しています。

アダプター "SubCategoryAdapter"のインスタントを1ページに1度だけ作成する必要があります。

"subCategory = new SubCategoryAdapter(this、R.layout.sub_category_items、CategoryList);"という行を削除します。 "displaySubCategory"メソッドから取得します。また、onCreateView/onCreateイベントに同じ行を追加します。

この行を追加 "subCategory.notifyDataSetChanged();" "displaySubCategory"メソッドの中で。

+0

あなたの応答のために非常に@サバリ..私は最初の方法を使用して出力を得て、それは完璧に動作します。しかし、私は2番目のメソッドを試すたびに、 "arrayIndexOutOfBound"例外が発生します。しかし、私はまだ私のコードで間違ったものについていくつか考えを持っている... :) –

+0

@RBoughan、それは第二の方法ではありません。それは第二段階です。最初のステップを実装すると、問題が解決されます。しかし、同じページ内のアダプターの複数のインスタンスを作成することは正しい方法ではありません。そこで、私はあなたに両方のステップを実装するよう提案しました。私の悪い英語を申し訳ありません。 – Sabari

+0

kkそれから私はそれを把握します。ありがとう@サバリ –

関連する問題