vendredi 29 janvier 2016

Hold checked items in list view. Searched and searched but no luck or I don't understand

I am trying to have a checked list retain values when scrolling in a listview and then also until the user has chosen to clear the list. I know that this has been asked many times but every answers I've read over the last two days havn't worked for me or I don't understand enough about what I'm doing to get it right (first thing I've developed). I am wondering if anyone can point me in the right direction using my actual code please? Or something that will means something to the code I have. Thank you in advance!

Below is my list code:

public class ShopList {

    public String RecName;
    public String RecIngredient;

    public ShopList(String recName, String recIng) {
        this.RecName = recName;
        this.RecIngredient = recIng;

    }
}

Below is my Custom Adapter:

public class ShoppingListAdapter extends ArrayAdapter<ShopList> {
public ShoppingListAdapter(Context context, ArrayList<ShopList> shoppingLists) {
    super(context, 0, shoppingLists);
}

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

    ViewHolder holder;

    ShopList shopList = getItem(position);
    if (convertView == null) {
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.shoppinglist, parent, false);
        holder = new ViewHolder();
        holder.tvRecName = (TextView) convertView.findViewById(R.id.tvRecipeShop);
        holder.tvIngredient = (CheckedTextView) convertView.findViewById(R.id.tvIngredShop);
        convertView.setTag(holder);
    }
    else {

        holder = (ViewHolder) convertView.getTag();
    }

    holder.tvRecName.setText(shopList.RecName);
    holder.tvIngredient.setText(shopList.RecIngredient);
    holder.tvIngredient.isChecked();

    return convertView;

}
    public static class ViewHolder {
    public TextView tvRecName;
    public CheckedTextView tvIngredient;

}


}

And this is the relevant code from my Activity:

public void getListData(){

    lv = (ListView)findViewById(R.id.lvShoppingList);

        ArrayList<ShopList> shopListArray = new ArrayList<ShopList>();

        ShoppingListAdapter myAdapter = new ShoppingListAdapter(this, shopListArray);
        ListView lv = (ListView) findViewById(R.id.lvShoppingList);

        lv.setAdapter(myAdapter);

        Cursor c = myDB.rawQuery("SELECT * FROM ShoppingList", null);

        c.moveToFirst();



        while (!c.isAfterLast()){

            String RecName = c.getString(c.getColumnIndex("RecipeName"));
            String RecIngredient = c.getString(c.getColumnIndex("IngredientName"));

            ShopList sL = new ShopList(RecName, RecIngredient);

            myAdapter.add(sL);

            c.moveToNext();
        }
        c.close();
}

public void toggle(View v)
{
    CheckedTextView cView = (CheckedTextView) v.findViewById(R.id.tvIngredShop);
    if (cView.isSelected())
    {
        cView.setSelected(false);
        cView.setChecked(false);

    }
    else {
        cView.setSelected(true);
        cView.setChecked(true);


    }
}

}

I still have a checkbox in my layout that I have just made invisible for now while I tried to get a solution trying to mimic what the simple_list_item_multiple_choice does. Can always put it back if need be.

I also understand that my SQL query is not the "correct" way of doing things from what I can gather but as a newbie I could recognise what it was doing and it's worked for me so far.

Thank you for your assistance, this has been bothering me for too long now. Also, sorry that I know it's a repeatedly asked question but I am at a loss. Thanks




Aucun commentaire:

Enregistrer un commentaire