samedi 28 mai 2016

Android Check box in Simple Adapter with ArrayList

I have a custom adapter Simple which the xml contains a CheckBox , corresponding to the id select ( R.id.selecionar ) .

However in another Activity where I create an ArrayList < HashMap > () called mcommentList (receiving various data from the database via Jason , however works perfectly )

After I click on a button that has onclick argument irpedidos . Clicking this button want to sweep all my arrayList looking for all the elements that are selected ( Checked box true ) and then put on a String all products who are selected .

However the idea that I have to do is wrong, to access the value of the CheckBox

what would be the solution to do what I want ? ERROR LINE: final CheckBox selec = mCommentList.get(i).get(R.id.selecionar);

package melo.gustavo.Deliveryapp;

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

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class VisualizarCardapio extends ListActivity {

    private ProgressDialog pDialog;


    private static final String READ_COMMENTS_URL =
            "http://-------------------/webserviceDelivery/cardapio.php";

    private static final String TAG_NOMEPRODUTO = "title";
    private static final String TAG_POSTS = "posts";
    private static final String TAG_PRECO = "username";
    private static final String TAG_DESCRICAO= "message";

    private JSONArray mComments = null;
    private ArrayList<HashMap<String, String>> mCommentList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.visualizar_cardapio);
    }

    @Override
    protected void onResume() {
        super.onResume();
        new LoadComments().execute();
    }

    public void irpedidos (View v) {

        String texto = "";
        double totalpedido = 0.0;

        for (int i = 0; i < mCommentList.size(); i++) {
          final String nometeste = mCommentList.get(i).get(TAG_NOMEPRODUTO);
          final String preco = mCommentList.get(i).get(TAG_PRECO);

            final CheckBox selec = mCommentList.get(i).get(R.id.selecionar);

                    if (selec.isChecked()) {
                        texto+= nometeste+" :"+preco+"\n";
                    }

                }

   Toast.makeText(VisualizarCardapio.this,texto , Toast.LENGTH_LONG).show();
    }

    public void updateJSONdata() {

        mCommentList = new ArrayList<HashMap<String, String>>();

        JSONParser jParser = new JSONParser();
        JSONObject json = jParser.getJSONFromUrl(READ_COMMENTS_URL);


        try {

            mComments = json.getJSONArray(TAG_POSTS);

    // looping through all posts according to the json object returned
            for (int i = 0; i < mComments.length(); i++) {
                JSONObject c = mComments.getJSONObject(i);

                String nomep = c.getString(TAG_NOMEPRODUTO);
                String descp = c.getString(TAG_DESCRICAO);
                String precop = c.getString(TAG_PRECO);

                HashMap<String, String> map = new HashMap<String, String>();

                map.put(TAG_NOMEPRODUTO, nomep);
                map.put(TAG_DESCRICAO, descp);
                map.put(TAG_PRECO, precop);

                // adding HashList to ArrayList
                mCommentList.add(map);

            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    private void updateList() {
    ListAdapter adapter = new MySimpleAdapterCardapio(this, mCommentList,
        R.layout.single_post_cardapio, new String[] {  TAG_NOMEPRODUTO, 
        TAG_DESCRICAO,TAG_PRECO }, new int[] { R.id.title, R.id.message,
                        R.id.username});

        // I shouldn't have to comment on this one:
        setListAdapter(adapter);


        ListView listView = getListView();
        listView.setOnItemClickListener(new OnItemClickListener() {

            @Override
    public void onItemClick(AdapterView<?> adapter, View arg1, int posicao,
                                    long arg3) {

                //implementar detalhes (adicional, sem salada)

            }
        });
    }

    public class LoadComments extends AsyncTask<Void, Void, Boolean> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(VisualizarCardapio.this);
            pDialog.setMessage("Loading Comments...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }

        @Override
        protected Boolean doInBackground(Void... arg0) {
            updateJSONdata();
            return null;

        }

        @Override
        protected void onPostExecute(Boolean result) {
            super.onPostExecute(result);
            pDialog.dismiss();
            updateList();
        }
    }
}

Follow the codes of activitys and xml the same

MySimpleAdapterCardapio .java
    package melo.gustavo.Deliveryapp;

    import android.content.Context;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.CheckBox;
    import android.widget.CompoundButton;
    import android.widget.ImageView;
    import android.widget.SimpleAdapter;
    import android.widget.TextView;
    import android.widget.Toast;

    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;


    public class MySimpleAdapterCardapio extends SimpleAdapter {


        private static final String TAG_NOMEPRODUTO = "title";
        private static final String TAG_POSTS = "posts";
        private static final String TAG_PRECO = "username";
        private static final String TAG_DESCRICAO= "message";

        private ArrayList<HashMap<String, Object>> results;
        private Context mContext;
        public LayoutInflater inflater = null;

        public MySimpleAdapterCardapio(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to) {
            super(context, data, resource, from, to);
            mContext = context;
            results = (ArrayList<HashMap<String, Object>>) data;
            inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            View vi = convertView;
            if (convertView == null)
                vi = inflater.inflate(R.layout.single_post_cardapio, null);

            final HashMap<String, Object> data = (HashMap<String, Object>) getItem(position);

            final TextView tt = (TextView) vi.findViewById(R.id.title);
            tt.setText((CharSequence) data.get(TAG_NOMEPRODUTO));

            TextView tt2 = (TextView) vi.findViewById(R.id.username);
            tt2.setText((CharSequence) data.get(TAG_PRECO));

            TextView tt3 = (TextView) vi.findViewById(R.id.message);
            tt3.setText((CharSequence) data.get(TAG_DESCRICAO));

            final CheckBox selec = (CheckBox) vi.findViewById(R.id.selecionar);

            selec.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    selec.setChecked(isChecked);
                    if (selec.isChecked()) {
                        selec.setText("Selecionado");
                        Toast.makeText(mContext, tt.getText() + " " + isChecked, Toast.LENGTH_LONG).show();
                    }

                    else {
                        selec.setText("Selecionar");
                    }

                }
            });

            //new DownloadTask((ImageView) vi.findViewById(R.id.fotolanchonete)).execute((String) data.get(TAG_FOTOLANC));

            return vi;
        }
    }

single_post_cardapio.xml

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://ift.tt/nIICcg"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#f0f0f0"
        android:orientation="vertical"
        android:weightSum="1">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:background="@drawable/post_border_style"
            android:orientation="vertical"
            android:layout_weight="6">

            <LinearLayout
                android:id="@+id/box"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="2dp"
                android:background="@drawable/post_background_style"
                android:orientation="horizontal" >

                <CheckBox
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:id="@+id/selecionar"
                    android:layout_weight="3.08"
                    android:text="Selecionar"
                    />

                <LinearLayout
                    android:id="@+id/box2"
                    android:layout_width="206dp"
                    android:layout_height="wrap_content"
                    android:layout_margin="2dp"
                    android:orientation="vertical"
                    android:padding="5dp"
                    android:weightSum="1"
                    android:layout_weight="3.08">

                    <TextView
                        android:id="@+id/title"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="bottom"
                        android:paddingBottom="2dip"
                        android:paddingLeft="5dp"
                        android:paddingTop="6dip"
                        android:textColor="#333"
                        android:textSize="16sp"
                        android:textStyle="bold"

                        android:text="Produto: "/>

                    <TextView
                        android:id="@+id/message"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:paddingBottom="2dip"
                        android:paddingLeft="8dp"
                        android:textColor="#888"
                        android:text="Descrição "
                        >
                    </TextView>

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal"
                        android:paddingBottom="5dp" >

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:gravity="left"
                            android:paddingLeft="5dp"
                            android:text="Preço: ">
                        </TextView>

                        <TextView
                            android:id="@+id/username"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:gravity="left"
                            android:textColor="#acacac"
                            android:textStyle="bold" >
                        </TextView>
                    </LinearLayout>
                </LinearLayout>
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>

visualizar_cardapio . xml

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://ift.tt/nIICcg"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#fff" >

        <LinearLayout
            android:id="@+id/top_layover"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:background="@drawable/blue_gradient"
            android:orientation="horizontal" >

            <TextView
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:gravity="center"
                android:text="Cardápio"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:layout_width="wrap_content" />
        </LinearLayout>

        <ListView android:id="@android:id/list"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_above="@+id/bottom_layover"
            android:layout_below="@+id/top_layover"
            android:background="#fff"
            android:divider="@android:color/transparent"
            android:scrollbars="none" />

        <LinearLayout
            android:id="@+id/bottom_layover"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:background="@drawable/blue_gradient"
            android:orientation="horizontal"
            android:weightSum="2" >

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:orientation="vertical" >
            </LinearLayout>

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:orientation="vertical" >

                <Button
                    android:id="@+id/btnValorLista"
                    android:onClick="irpedidos"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="right"
                    android:background="@drawable/black_button"
                    android:text="Selecionar Produtos" />
            </LinearLayout>
        </LinearLayout>

    </RelativeLayout>




Aucun commentaire:

Enregistrer un commentaire