2015-10-23 4 views
8

में विस्तारणीय सूचीदृश्य में चेकबॉक्स स्थिति कैसे प्राप्त करें मैं अपने आवेदन डेटा में विस्तारणीय सूचीदृश्य में चेकबॉक्स स्थिति को सर्वर से आना चाहता हूं।एंड्रॉइड

मेरे ProductsCategory.java: -

public class ProductsCategory extends AppCompatActivity { 

    private ListView listView; 
    private GridView gridView; 
    ProductsAdapter adapter; 
    ProductsAdapterGridView adapterGridView; 
    ArrayList<Products> productsList = new ArrayList<>(); 
    String menu_id; 
    TextView textView,textViewfilter; 

    // Filter Class 
    ExpandableListAdapter listAdapter; 
    ExpandableListView expListView; 
    List<String> listDataHeader = new ArrayList<>(); 
    HashMap<String, List<String>> listDataChild = new HashMap<>(); 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.produts_category); 

     Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(mToolbar); 
     getSupportActionBar().setTitle("Shopping Mazza"); 
     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
     getSupportActionBar().setDisplayShowHomeEnabled(true); 

     Intent i = getIntent(); 
     menu_id = i.getStringExtra("category"); 
     adapter = new ProductsAdapter(ProductsCategory.this,R.layout.product_page_list_view,productsList); 
     adapterGridView = new ProductsAdapterGridView(ProductsCategory.this,R.layout.products_page_grid_view,productsList); 
     expListView = (ExpandableListView) findViewById(R.id.filter_expandable_list_view); 
     listView = (ListView)findViewById(R.id.list_product); 
     gridView = (GridView)findViewById(R.id.list_product_grid); 
     textView = (TextView)findViewById(R.id.change_view); 
     textViewfilter = (TextView)findViewById(R.id.textView); 

     textView.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       if(listView.getVisibility()==View.VISIBLE){ 
        listView.setVisibility(View.GONE); 
        gridView.setVisibility(View.VISIBLE); 
       } 
       else { 
        listView.setVisibility(View.VISIBLE); 
        gridView.setVisibility(View.GONE); 
       } 
      } 
     }); 

     textViewfilter.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       if (expListView.getVisibility() == View.VISIBLE) { 

        listDataHeader.clear(); 
        listDataChild.clear(); 

        listAdapter = new ExpandableListAdapter(ProductsCategory.this, listDataHeader, listDataChild); 
        expListView.setAdapter(listAdapter); 

        listAdapter.notifyDataSetChanged(); 
        expListView.setVisibility(View.GONE); 
       } 
       else { 
         expListView.setVisibility(View.VISIBLE); 

        // preparing list data 
        // prepareListData(); 
        new PrepareListData().execute("http://opencart.codeniques.com/shopping/?route=feed/web_api/filters&key=test123$"); 

        // Listview Group click listener 
        expListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() { 

         @Override 
         public boolean onGroupClick(ExpandableListView parent, View v, 
                int groupPosition, long id) { 
          // Toast.makeText(getApplicationContext(), 
          // "Group Clicked " + listDataHeader.get(groupPosition), 
          // Toast.LENGTH_SHORT).show(); 
          return false; 

         } 
        }); 

        // Listview Group expanded listener 
        expListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() { 

         @Override 
         public void onGroupExpand(int groupPosition) { 
          Toast.makeText(getApplicationContext(), 
            listDataHeader.get(groupPosition) + " Expanded", 
            Toast.LENGTH_SHORT).show(); 
         } 
        }); 

        // Listview Group collasped listener 
        expListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() { 

         @Override 
         public void onGroupCollapse(int groupPosition) { 
          Toast.makeText(getApplicationContext(), 
            listDataHeader.get(groupPosition) + " Collapsed", 
            Toast.LENGTH_SHORT).show(); 

         } 
        }); 

        // Listview on child click listener 
        expListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { 

         @Override 
         public boolean onChildClick(ExpandableListView parent, View v, 
                int groupPosition, int childPosition, long id) { 

          Toast.makeText(
            getApplicationContext(), 
            listDataHeader.get(groupPosition) 
              + " : " 
              + listDataChild.get(
              listDataHeader.get(groupPosition)).get(
              childPosition), Toast.LENGTH_SHORT) 
            .show(); 
          return false; 
         } 
        }); 

       /* Intent i = new Intent(ProductsCategory.this,FilterClass.class); 
        i.putExtra("category_id",menu_id); 
        startActivity(i);*/ 
       } 
      } 
     }); 

     new Product().execute("http://opencart.codeniques.com/shopping/?route=feed/web_api/products&key=test123$"); 
    } 
    public class PrepareListData extends AsyncTask<String,Void,Void>{ 
     ProgressDialog dialog; 
     @Override 
     protected synchronized void onPreExecute() { 
      super.onPreExecute(); 
      dialog = new ProgressDialog(ProductsCategory.this); 
      dialog.setCancelable(false); 
      dialog.show(); 
     } 

     @Override 
     protected synchronized Void doInBackground(String... params) { 
      try { 
       HttpClient client = new DefaultHttpClient(); 
       HttpPost post = new HttpPost(params[0]); 
       List<NameValuePair> nameValuePairs = new ArrayList<>(1); 

       nameValuePairs.add(new BasicNameValuePair("category",menu_id)); 
       post.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

       HttpResponse response = client.execute(post); 

       if(response.getStatusLine().getStatusCode()==200){ 

        HttpEntity entity = response.getEntity(); 
        String data = EntityUtils.toString(entity); 

        JSONObject jsonObject = new JSONObject(data); 
        JSONArray jsonArray = jsonObject.getJSONArray("filters"); 

        //  Log.d("jsonarray",jsonArray.length()+""); 

        Log.d("filter",jsonArray+""); 

        for(int i=0;i<jsonArray.length();i++){ 

         JSONObject jsonObject1 = jsonArray.getJSONObject(i); 

         String s = jsonObject1.getString("filter_group_id"); 
         listDataHeader.add(jsonObject1.getString("name")); 

         JSONArray jsonArray1 = jsonObject1.getJSONArray("filter"); 

         List<String> firstlevel = new ArrayList<>(); 
         for(int j=0;j<jsonArray1.length();j++){ 

          JSONObject jsonObject11 = jsonArray1.optJSONObject(j); 

          String s1 = jsonObject11.getString("filter_id"); 
          String colorcode = jsonObject11.getString("colorcode"); 
          firstlevel.add(jsonObject11.getString("name")); 
         } 
         listDataChild.put(listDataHeader.get(i), firstlevel); 
         Log.d("listDataHeader", listDataHeader.get(i)+""); 
         Log.d("value of", firstlevel + ""); 
         Log.d("hash map", listDataChild.size() + ""); 
         // firstlevel.clear(); 
        } 
       } 
      }catch (IOException |JSONException e){ 
       Log.e("Error :", e.getMessage()); 
      } 
      return null; 
     } 
     @Override 
     protected synchronized void onPostExecute(Void aVoid) { 
      // super.onPostExecute(aVoid); 
      dialog.dismiss(); 
      listAdapter = new ExpandableListAdapter(ProductsCategory.this, listDataHeader, listDataChild); 
      // setting list adapter 
      expListView.setAdapter(listAdapter); 

     } 
    } 

मेरे ExpandableListAdapter: -

public class ExpandableListAdapter extends BaseExpandableListAdapter { 

    private Context _context; 
    private List<String> _listDataHeader; // header titles 
    // child data in format of header title, child title 
    private HashMap<String, List<String>> _listDataChild; 

    public ExpandableListAdapter(Context context, List<String> listDataHeader, 
           HashMap<String, List<String>> listChildData) { 
     this._context = context; 
     this._listDataHeader = listDataHeader; 
     this._listDataChild = listChildData; 
    } 

    @Override 
    public Object getChild(int groupPosition, int childPosititon) { 
     return this._listDataChild.get(this._listDataHeader.get(groupPosition)) 
       .get(childPosititon); 
    } 

    @Override 
    public long getChildId(int groupPosition, int childPosition) { 
     return childPosition; 
    } 

    @Override 
    public View getChildView(int groupPosition, final int childPosition, 
          boolean isLastChild, View convertView, ViewGroup parent) { 

     final String childText = (String) getChild(groupPosition, childPosition); 

     if (convertView == null) { 
      LayoutInflater infalInflater = (LayoutInflater) this._context 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = infalInflater.inflate(R.layout.list_item_filter, null); 


     } 

     TextView txtListChild = (TextView) convertView 
       .findViewById(R.id.lblListItem); 

     txtListChild.setText(childText); 
     return convertView; 
    } 

    @Override 
    public int getChildrenCount(int groupPosition) { 
     return this._listDataChild.get(this._listDataHeader.get(groupPosition)) 
       .size(); 
    } 

    @Override 
    public Object getGroup(int groupPosition) { 
     return this._listDataHeader.get(groupPosition); 
    } 

    @Override 
    public int getGroupCount() { 
     return this._listDataHeader.size(); 
    } 

    @Override 
    public long getGroupId(int groupPosition) { 
     return groupPosition; 
    } 

    @Override 
    public View getGroupView(int groupPosition, boolean isExpanded, 
          View convertView, ViewGroup parent) { 
     String headerTitle = (String) getGroup(groupPosition); 
     if (convertView == null) { 
      LayoutInflater infalInflater = (LayoutInflater) this._context 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = infalInflater.inflate(R.layout.list_group_filter, null); 
     } 

     TextView lblListHeader = (TextView) convertView 
       .findViewById(R.id.lblListHeader); 
     lblListHeader.setTypeface(null, Typeface.BOLD); 
     lblListHeader.setText(headerTitle); 

     return convertView; 
    } 

    @Override 
    public boolean hasStableIds() { 
     return false; 
    } 

    @Override 
    public boolean isChildSelectable(int groupPosition, int childPosition) { 
     return true; 
    } 
} 

मुझे पता है कि कौन सा बटन है चेकबॉक्स चेक किया जाता है और एक userdefine methods.One अधिक कॉल करना चाहते हैं चीज जब मैं एक चेकबॉक्स और कोलास्ड समूह की जांच करता हूं और फिर समूह को विस्तारित करता हूं तो चेकबॉक्स की स्थिति बदल जाती है।

मैं दोनों स्थिति का स्नैपशॉट दे: -

enter image description here

जब मैं collasped और फिर विस्तार चेक बॉक्स स्थिति परिवर्तन है।

enter image description here

+0

क्या आपके लिए काम उपलब्ध उत्तर दिया गया है? यदि नहीं है तो क्या गुम/अस्पष्ट है? –

उत्तर

15

यह वास्तव में चेक बॉक्स के साथ सूचीदृश्य के समान है। आपको आंतरिक रूप से राज्य को याद रखना होगा (उदा। बूलियन सरणी या सेट के साथ) और राज्य पर गेटटर प्रदान करें।

  1. अपने बच्चे लेआउट से चेकबॉक्स प्राप्त करें और जोड़ने के एक श्रोता

    public class ExpandableListAdapter extends BaseExpandableListAdapter { 
        ... 
        private final Set<Pair<Long, Long>> mCheckedItems = new HashSet<Pair<Long, Long>>(); 
        ... 
        public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { 
    
         ... 
    
         final CheckBox cb = (CheckBox) convertView.findViewById(R.id.your_checkbox_id); 
         // add tag to remember groupId/childId 
         final Pair<Long, Long> tag = new Pair<Long, Long>(
          getGroupId(groupPosition), 
          getChildId(groupPosition, childPosition)); 
         cb.setTag(tag); 
         // set checked if groupId/childId in checked items 
         cb.setChecked(mCheckedItems.contains(tag)); 
         // set OnClickListener to handle checked switches 
         cb.setOnClickListener(new OnClickListener() { 
    
          public void onClick(View v) { 
           final CheckBox cb = (CheckBox) v; 
           final Pair<Long, Long> tag = (Pair<Long, Long>) v.getTag(); 
           if (cb.isChecked()) { 
            mCheckedItems.add(tag); 
           } else { 
            mCheckedItems.remove(tag); 
           } 
          } 
         }); 
    
         ... 
    
        } 
        ... 
    } 
    
  2. अपने जाँच की राज्य के लिए एक गेटर जोड़े

    public class ExpandableListAdapter extends BaseExpandableListAdapter { 
        ... 
        public Set<Pair<Long, Long>> getCheckedItems() { 
         return mCheckedItems; 
        } 
        ... 
    } 
    

कम भरा सेट सभी की जाँच में शामिल है समूह/बाल वस्तुओं। आप विधि को कॉल कर सकते हैं उदा। आपकी गतिविधि में

अपने दूसरे प्रश्न के बारे में: मुझे लगता है कि समूह को ध्वस्त/विस्तारित करते समय चेक किया गया बच्चा किसी आंतरिक स्थिति में कुछ आंतरिक रूप से पुन: उपयोग किए गए बच्चे के विचारों के कारण गलत है।

बच्चे आइटम के लिए घटनाओं को ढीला क्लिक करने के लिए चेकबॉक्स आइटम पर निम्न लेआउट विशेषताओं को भी न भूलें।

android:focusable="false" 
android:focusableInTouchMode="false" 
+0

Thnx मदद करने के लिए .......... मैंने इस काम के लिए 1 दिन बर्बाद कर दिया है ........ – Sunil