2017-07-17 64 views
0

您好,我的列表视图中有chechbox的问题。我尝试了一整天,但我不能这样做:/我想:当有人点击复选框我想更改菜单项的标题从取消保存(1),当选择下一个保存(2)等。我想选择产品的编号和数量。 我得到身份证,但我认为它是错误的方式,因为现在当有人点击复选框时我会抽取产品数量。如果有人点击并且下一次更改数量值,该怎么办......任何人都可以帮助我? Plsss再次使用CheckBox的ListView Android

它我的代码:

片段

package befit.example.com.befit.Fragments; 


import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.SearchView; 
import android.view.LayoutInflater; 
import android.view.Menu; 
import android.view.MenuInflater; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.AdapterView; 
import android.widget.ListView; 
import android.widget.RelativeLayout; 

import com.ami.fundapter.BindDictionary; 
import com.ami.fundapter.extractors.StringExtractor; 
import com.kosalgeek.android.json.JsonConverter; 
import com.kosalgeek.asynctask.AsyncResponse; 
import com.kosalgeek.asynctask.PostResponseAsyncTask; 

import org.json.JSONArray; 
import org.json.JSONException; 

import java.util.ArrayList; 
import java.util.HashMap; 

import befit.example.com.befit.Adapter.MyFunDapter; 
import befit.example.com.befit.R; 
import befit.example.com.befit.Serialized.Products; 

public class SearchFragment extends Fragment { 

    private ListView lvSearch; 
    private SearchView searchView; 
    private HashMap postData; 
    private PostResponseAsyncTask loginTask; 
    private ArrayList<Products> productsList; 
    private Bundle bundle; 
    private MenuItem save; 
    private RelativeLayout layoutInvisible; 
    private MyFunDapter<Products> adapter; 
    private ArrayList products, sizes; 
    private String resultAll; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) { 

     bundle = this.getArguments(); 

     if (bundle != null) { 
      String type = bundle.getString("type"); 
      ((AppCompatActivity) getActivity()).getSupportActionBar().setTitle(type); 
     } 
     return inflater.inflate(R.layout.fragment_search, parent, false); 

    } 

    public void onViewCreated(View view, Bundle savedInstanceState) { 

     setHasOptionsMenu(true); 

     lvSearch(view); 

     search(view); 

     getCounterFromAdapter(); 

    } 

    private void getCounterFromAdapter() { 

     int counter = adapter.getCounter(); 

     if (counter != 0) 
      save.setTitle("Save (" + counter + ")"); 
     else 
      save.setTitle("Cancel"); 

    } 
    @Override 
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { 

     inflater.inflate(R.menu.fragment_search_menu, menu); 

     save = menu.findItem(R.id.save); 

     super.onCreateOptionsMenu(menu, inflater); 
    } 

    private void lvSearch(View view) { 

     lvSearch = (ListView) view.findViewById(R.id.lvSearch); 

     postData = new HashMap(); 
     postData.put("mobile", "android"); 

     loginTask = new PostResponseAsyncTask(getActivity(), postData, new AsyncResponse() { 

      @Override 
      public void processFinish(String result) { 

       resultAll = result; 

       productsList = new JsonConverter<Products>().toArrayList(result, Products.class); 

       BindDictionary<Products> dict = new BindDictionary<Products>(); 

       dict.addStringField(R.id.tvProductsName, new StringExtractor<Products>() { 

        @Override 
        public String getStringValue(Products products, int position) { 
         return products.name; 
        } 
       }); 

       dict.addStringField(R.id.tvProductsWeight, new StringExtractor<Products>() { 

        @Override 
        public String getStringValue(Products products, int position) { 
         return products.quantity + " gram - " + products.calories + " kcal"; 
        } 
       }); 

       dict.addStringField(R.id.tvSearchSize, new StringExtractor<Products>() { 

        @Override 
        public String getStringValue(Products products, int position) { 
         return products.quantity + ""; 
        } 
       }); 

       dict.addStringField(R.id.tvSearchCalories, new StringExtractor<Products>() { 

        @Override 
        public String getStringValue(Products products, int position) { 
         return "Calories: " + products.calories + " kcal"; 
        } 
       }); 

       dict.addStringField(R.id.tvSearchFat, new StringExtractor<Products>() { 

        @Override 
        public String getStringValue(Products products, int position) { 
         return "Fat: " + products.fat + " gram"; 
        } 
       }); 

       dict.addStringField(R.id.tvSearchCarbs, new StringExtractor<Products>() { 

        @Override 
        public String getStringValue(Products products, int position) { 
         return "Carbs: " + products.carbs + " gram"; 
        } 
       }); 

       dict.addStringField(R.id.tvSearchProtein, new StringExtractor<Products>() { 

        @Override 
        public String getStringValue(Products products, int position) { 
         return "Protein: " + products.protein + " gram"; 
        } 
       }); 

       adapter = new MyFunDapter<Products>(getActivity(), productsList, R.layout.list_view_search, dict); 

       lvSearch.setAdapter(adapter); 
      } 
     }); 

     loginTask.execute("http://10.0.3.2/befit/products.php"); 

     lvSearch.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> parent, final View adapterView, int position, long id) { 

       layoutInvisible = (RelativeLayout) adapterView.findViewById(R.id.layoutInvisible); 

       if (layoutInvisible.getVisibility() == View.VISIBLE) 
        layoutInvisible.setVisibility(View.GONE); 
       else 
        layoutInvisible.setVisibility(View.VISIBLE); 

       getCounterFromAdapter(); 
      } 
     }); 
    } 

    private void search(View view) { 

     searchView = (SearchView) view.findViewById(R.id.searchView); 
     searchView.setQueryHint("Search..."); 
     searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { 

      @Override 
      public boolean onQueryTextSubmit(String query) { 
       return false; 
      } 

      @Override 
      public boolean onQueryTextChange(String newText) { 

       //adapter.getFilter().filter(newText); 

       return false; 
      } 
     }); 

    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     int id = item.getItemId(); 
     if (id == R.id.save) { 

      int sizeOfProducts = adapter.getClickedProducts().size(); 

      if (sizeOfProducts > 0) { 
       products = adapter.getClickedProducts(); 
       sizes = adapter.getClickedSize(); 

       postData = new HashMap(); 
       postData.put("mobile", "android"); 

       try { 
        JSONArray jArray = new JSONArray(resultAll); 

        postData.put("p1", jArray.getJSONObject(Integer.parseInt(products.get(0).toString())).getInt("id_produktu")); 
        postData.put("r1", Integer.parseInt(sizes.get(0).toString())); 

        if (sizeOfProducts > 1) { 
         postData.put("p2", jArray.getJSONObject(Integer.parseInt(products.get(1).toString())).getInt("id_produktu")); 
         postData.put("r2", Integer.parseInt(sizes.get(1).toString())); 
        } 

        if (sizeOfProducts > 2) { 
         postData.put("p3", jArray.getJSONObject(Integer.parseInt(products.get(2).toString())).getInt("id_produktu")); 
         postData.put("r3", Integer.parseInt(sizes.get(2).toString())); 
        } 

        if (sizeOfProducts > 3) { 
         postData.put("p4", jArray.getJSONObject(Integer.parseInt(products.get(3).toString())).getInt("id_produktu")); 
         postData.put("r4", Integer.parseInt(sizes.get(3).toString())); 
        } 


       } catch (JSONException e) { 
        e.printStackTrace(); 
       } 
/* 
       loginTask = new PostResponseAsyncTask(getActivity(), postData, new AsyncResponse() { 


        @Override 
        public void processFinish(String s) { 

        } 
       }); 

       loginTask.execute("10.0.3.2/products.php"); 
*/ 

       return true; 
      } 
     } 
     return super.onOptionsItemSelected(item); 
    } 
} 

适配器

package befit.example.com.befit.Adapter; 


import android.content.Context; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.CheckBox; 
import android.widget.Filter; 
import android.widget.Filterable; 
import android.widget.RelativeLayout; 
import android.widget.TextView; 

import com.ami.fundapter.BindDictionary; 
import com.ami.fundapter.FunDapterUtils; 
import com.ami.fundapter.interfaces.FunDapterFilter; 

import java.util.ArrayList; 
import java.util.List; 

import befit.example.com.befit.R; 
import interfaces.LongExtractor; 

/** 
* A generic adapter that takes a BindDictionary and data and shows them. Does 
* basic validation for you for all fields and also handles the ViewHolder 
* pattern. 
* 
* @param <T> 
* @author Ami G 
*/ 
public class MyFunDapter<T> extends BaseAdapter implements Filterable { 

    protected List<T> mDataItems; 
    protected List<T> mOrigDataItems; 
    protected LongExtractor<T> idExtractor; 
    protected final Context mContext; 
    private final int mLayoutResource; 
    private final BindDictionary<T> mBindDictionary; 
    private int oddColorRes; 
    private int evenColorRes; 
    private FunDapterFilter<T> funDapterFilter; 
    private Filter mFilter; 
    public int counter = 0; 
    private ArrayList<String> products, size; 

    /** 
    * A generic adapter that takes a BindDictionary and data and shows them. 
    * Does basic validation for you for all fields and also handles the 
    * ViewHolder pattern. 
    * 
    * @param context 
    * @param dataItems  - An arraylist of model items 
    * @param layoutResource - resource ID of a layout to inflate for each item. (Example: 
    *      R.layout.list_item) 
    * @param dictionary  - The dictionary that will match between fields and data. 
    */ 
    public MyFunDapter(Context context, List<T> dataItems, int layoutResource, 
         BindDictionary<T> dictionary) { 
     this(context, dataItems, layoutResource, null, dictionary); 
    } 

    /** 
    * A generic adapter that takes a BindDictionary and data and shows them. 
    * Does basic validation for you for all fields and also handles the 
    * ViewHolder pattern. 
    * 
    * @param context 
    * @param dataItems  - An arraylist of model items 
    * @param layoutResource - resource ID of a layout to inflate for each item. (Example: 
    *      R.layout.list_item) 
    * @param idExtractor - The extractor that will get stable ids from the data item 
    * @param dictionary  - The dictionary that will match between fields and data. 
    */ 


    public MyFunDapter(Context context, List<T> dataItems, int layoutResource, 
         LongExtractor<T> idExtractor, BindDictionary<T> dictionary) { 
     this.mContext = context; 
     this.mDataItems = dataItems; 
     this.mOrigDataItems = dataItems; 
     this.mLayoutResource = layoutResource; 
     this.idExtractor = idExtractor; 
     this.mBindDictionary = dictionary; 
    } 

    /** 
    * Replace the current dataset with a new one and refresh the views. This 
    * will call notifyDataSetChanged() for you. 
    * 
    * @param dataItems 
    */ 
    public void updateData(List<T> dataItems) { 
     this.mDataItems = dataItems; 
     this.mOrigDataItems = dataItems; 
     notifyDataSetChanged(); 
    } 

    @Override 
    public int getCount() { 
     if (mDataItems == null || mBindDictionary == null) return 0; 

     return mDataItems.size(); 
    } 

    @Override 
    public T getItem(int position) { 
     return mDataItems.get(position); 
    } 

    @Override 
    public boolean hasStableIds() { 
     if (idExtractor == null) return super.hasStableIds(); 
     else return true; 
    } 

    @Override 
    public long getItemId(int position) { 
     if (idExtractor == null) return position; 
     else return idExtractor.getLongValue(getItem(position), position); 
    } 

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

     // Inflate a new view or use a recycled view. 
     View v = convertView; 
     final MyGenericViewHolder holder; 
     if (null == v) { 
      LayoutInflater vi = 
        (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      v = vi.inflate(mLayoutResource, null); 
      holder = new MyGenericViewHolder(); 
      holder.root = v; 

      holder.tvSearchFat = (TextView) v.findViewById(R.id.tvSearchFat); 
      holder.tvSearchSize = (TextView) v.findViewById(R.id.tvSearchSize); 
      holder.layoutInvisible = (RelativeLayout) v.findViewById(R.id.layoutInvisible); 
      holder.checkBox = (CheckBox) v.findViewById(R.id.checkBox); 

      // init the sub views and put them in a holder instance 
      FunDapterUtils.initViews(v, holder, mBindDictionary); 

      v.setTag(holder); 
     } else { 
      holder = (MyGenericViewHolder) v.getTag(); 
     } 

     // Show the data 
     final T item = getItem(position); 
     showData(item, holder, position); 

     holder.layoutInvisible.setVisibility(View.GONE); 

     products = new ArrayList<String>(); 

     size = new ArrayList<String>(); 

     holder.checkBox.setTag(R.id.ID_TAG, position); 
     holder.checkBox.setTag(R.id.SIZE_TAG, holder.tvSearchSize.getText().toString()); 
     holder.checkBox.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       String id = String.valueOf(v.getTag(R.id.ID_TAG)); 
       String sizeOfProduct = String.valueOf(v.getTag(R.id.SIZE_TAG)); 

       if(holder.checkBox.isChecked()) { 
        counter++; 
        products.add(id); 
        size.add(sizeOfProduct); 
       } 
       else{ 
        counter--; 
        products.remove(id); 
        size.remove(sizeOfProduct); 
       } 
      } 
     }); 

     return v; 
    } 

    public ArrayList getClickedProducts(){ 

     return products; 
    } 

    public ArrayList getClickedSize(){ 

     return size; 
    } 

    public int getCounter(){ 

     return counter; 
    } 

    private void showData(T item, MyGenericViewHolder holder, int position) { 

     // handles alternating background colors if selected 
     if (oddColorRes > 0 && evenColorRes > 0) { 
      if (position % 2 == 0) { 
       holder.root.setBackgroundColor(mContext.getResources().getColor(evenColorRes)); 
      } else { 
       holder.root.setBackgroundColor(mContext.getResources().getColor(oddColorRes)); 
      } 
     } 

     FunDapterUtils.showData(item, holder, position, mBindDictionary); 
    } 

    public MyFunDapter<T> setAlternatingBackground(int oddColorRes, int evenColorRes) { 

     if (oddColorRes <= 0 || evenColorRes <= 0) { 
      throw new IllegalArgumentException("Color parameters are illegal"); 
     } 

     this.oddColorRes = oddColorRes; 
     this.evenColorRes = evenColorRes; 

     return this; 
    } 

    /** 
    * @param idExtractor - used to extract a stable id from the underlying data item (like a database id or something) 
    */ 
    public void setIdExtractor(LongExtractor<T> idExtractor) { 
     this.idExtractor = idExtractor; 
    } 

    @Override 
    public Filter getFilter() { 
     return mFilter; 
    } 

    /** 
    * Use this method to enable filtering in the adapter. 
    * 
    * @param filter - a filter implementation for your adapter. 
    */ 
    public void initFilter(FunDapterFilter<T> filter) { 

     if (filter == null) 
      throw new IllegalArgumentException("Cannot pass a null filter to FunDapter"); 

     this.funDapterFilter = filter; 

     mFilter = new Filter() { 

      @Override 
      protected void publishResults(CharSequence constraint, FilterResults results) { 

       @SuppressWarnings("unchecked") List<T> list = (List<T>) results.values; 

       if (results.count == 0) { 
        resetData(); 
       } else { 
        mDataItems = list; 
       } 

       notifyDataSetChanged(); 
      } 

      @Override 
      protected FilterResults performFiltering(CharSequence constraint) { 

       FilterResults results = new FilterResults(); 
       if (constraint == null || constraint.length() == 0) { 

        // No constraint - no point in filtering. 
        results.values = mOrigDataItems; 
        results.count = mOrigDataItems.size(); 
       } else { 
        // Perform the filtering operation 

        List<T> filter = 
          funDapterFilter.filter(constraint.toString(), mOrigDataItems); 

        results.count = filter.size(); 
        results.values = filter; 

       } 

       return results; 
      } 
     }; 
    } 



    public void resetData() { 
     mDataItems = mOrigDataItems; 
    } 
} 

Pllssss帮我:))

回答

0

制作使用的接口来获得物品托运的数量在您的适配器中, 然后inavlidateMenu()的值为

接口

public interface MyAdapterClickInterface{ 
    public void itemChecked(int position); 
} 
现在

在您的片段将其更改为

public class SearchFragment extends Fragment implements MyAdapterClickInterface{ 
int itemsChecked = 0; //this will keep a note of items to be shown in menu 
// if you want to save items also then add an attribute to your Products class to get checked item 

实现方法

public void itemChecked(int position){ 
    itemsChecked++; 
    invalidateOptionsMenu(); 
} 

当你初始化你的片段适配器,改变通话到:

adapter = new MyFunDapter<Products>(getActivity(), productsList, R.layout.list_view_search, dict, this);//this means fragments implementations 
现在

在适配器构造

MyAdapterClickInterface checkedListener; 
public MyFunDapter(Context context, List<T> dataItems, int layoutResource, 
         LongExtractor<T> idExtractor, BindDictionary<T> dictionary, MyAdapterClickInterface checkedListener) { 
     this.mContext = context; 
     this.checkedListener = checkedListener; //making use of interface instance 
... 
} 

现在终于当你的项目正在检查

holder.checkBox.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
      if(holder.checkBox.isChecked()){ 
      checkedListener.itemChecked(position); 
      //or some other method to save your checks.. 
      } 

这将做你想做的事.. :)

+0

谢谢很多,但我有问题,适配器=新MyFunDapter (getActivity(),productsList,R.layout.list_view_search,字典,这); [链接](https://ibb.co/ijme0F)对不起,但我真的是新手:) –

+0

更改你的适配器的构造函数,我也包括在回答中。 – Ashwani

+0

并接受答案,如果它帮助 – Ashwani

相关问题