mardi 1 mars 2016

Start Activity with a Checkbox in RecyclerView

I would start an Activity on check of my Checkbox in a RecyclerView. I've already set the Checkbox and the OnClickListener in my RecyclerView Adapter, now i have to start the activity in my main. I know i should override and declare as follow:

@Override
public void mClick(View v, int position) {
Intent i = new Intent(FrammentoUno.this, SQL_Main.class);
startActivity(i);
}

but i don't know where, in my Activity. I'll probably have to set my setListener as follow

 MyAdapter.setListner(this);

but i don't understand where. Here my Main and my Adapter:

FrammentoUno.java (main)

public class FrammentoUno extends AppCompatActivity {


private RecyclerView GetAllAllarmiListView;
/**
 * ATTENTION: This was auto-generated to implement the App Indexing API.
 * See http://ift.tt/1Shh2Dk for more information.
 */
private GoogleApiClient mClient;
private Uri mUrl;
private String mTitle;
private String mDescription;

private Button frammentoUno, frammentoDue;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fragment_one);

    addListenerOnButton();


    this.GetAllAllarmiListView = (RecyclerView) this.findViewById(R.id.GetAllAllarmiListView);

    new GetAllAllarmiTask().execute(new ApiConnector());

    mClient = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
    mUrl = Uri.parse("android-app://com.example.andrea/http/host_path");
    mTitle = "Standard Poodle";
    mDescription = "The Standard Poodle stands at least 18 inches at the withers";



    LayoutInflater factory = LayoutInflater.from(this);
    final View view = factory.inflate(R.layout.get_all_allarmi_list_view_cell, null);

    setListAdapter(new JSONArray());
}


public void addListenerOnButton() {

    frammentoUno = (Button) findViewById(R.id.frammentoUno);
    frammentoDue = (Button) findViewById(R.id.frammentoDue);

    frammentoUno.setOnClickListener(new View.OnClickListener() {

        //Run when button is clicked
        @Override
        public void onClick(View v) {
            Intent i = new Intent(FrammentoUno.this, FrammentoUno.class);

            startActivity(i);
            Toast.makeText(FrammentoUno.this, "Allarmi Generali",
                    Toast.LENGTH_LONG).show();

        }
    });

    frammentoDue.setOnClickListener(new View.OnClickListener() {
        //Run when button is clicked
        @Override
        public void onClick(View v) {
            Intent i = new Intent(FrammentoUno.this, FrammentoDue.class);

            startActivity(i);
            Toast.makeText(FrammentoUno.this, "Controllo Ultimi Allarmi",
                    Toast.LENGTH_LONG).show();

        }
    });

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu_main, menu);
    return true;
}

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

    switch (id) {
        case R.id.MENU_1:
        /*
            Codice di gestione della voce MENU_1
         */
            startActivity(new Intent(this, ControlloSbarre.class));
            return true;

        case R.id.MENU_2:
             /*
            Codice di gestione della voce MENU_2
         */
            startActivity(new Intent(this, LoginActivity.class));
            return true;
    }
    return false;
}


public void setListAdapter(JSONArray jsonArray) {
    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
    this.GetAllAllarmiListView.setLayoutManager(layoutManager);
    this.GetAllAllarmiListView.setAdapter(new GetAllAllarmiListViewAdapter(jsonArray, this));
}




private class GetAllAllarmiTask extends AsyncTask<ApiConnector, Long, JSONArray> {
    @Override
    protected JSONArray doInBackground(ApiConnector... params) {
        return params[0].GetAllAllarmi();
    }

    @Override
    protected void onPostExecute(JSONArray jsonArray) {
        setListAdapter(jsonArray);
    }
}
}

GetAllAllarmiListViewAdapter.java (adapter)

public class GetAllAllarmiListViewAdapter extends RecyclerView.Adapter<GetAllAllarmiListViewAdapter.MyViewHolder> {

private JSONArray dataArray;
private Activity activity;

private static LayoutInflater inflater = null;

// Provide a suitable constructor (depends on the kind of dataset)
public GetAllAllarmiListViewAdapter(JSONArray jsonArray, Activity a) {
    this.dataArray = jsonArray;
    this.activity = a;

    inflater = (LayoutInflater) this.activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public GetAllAllarmiListViewAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    // create a new view
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.get_all_allarmi_list_view_cell, parent, false);
    // set the view's size, margins, paddings and layout parameters
    MyViewHolder vh = new MyViewHolder(v);
    return vh;
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    try {

        JSONObject jsonObject = this.dataArray.getJSONObject(position);
        holder.nomeParcheggio.setText(jsonObject.getString("nome"));
        holder.data.setText(jsonObject.getString("data_al"));
        holder.stato.setText(jsonObject.getString("stato"));
        holder.descrizione.setText(jsonObject.getString("descrizione"));
        holder.targa.setText(jsonObject.getString("targa"));
        holder.azione.setText(jsonObject.getString("azione"));
        jsonObject.getString("azione");

        if (jsonObject.getString("azione") == "1") {
            holder.dispositivo.setText(jsonObject.getString("varco"));
        } else if (jsonObject.getString("azione") == "2") {
            holder.dispositivo.setText(jsonObject.getString("cassa"));
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }


    MyViewHolder.chkSelected.setOnClickListener (new View.OnClickListener() {
        public void onClick(View v) {

            CheckBox cb = (CheckBox) v;
            Toast.makeText(
                    v.getContext(),
                    "Visto", Toast.LENGTH_LONG).show();
            }
    });
}

@Override
public int getItemCount() {
    return dataArray.length();
}

public interface mClickListener {
    public void mClick(View v, int position);
}

private static mClickListener listener;

public void setListner(mClickListener listner){
    this.listener = listner;
}

public static class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{


    // each data item is just a string in this case
    private TextView nomeParcheggio;
    private TextView data;
    private TextView stato;
    private TextView descrizione;
    private TextView targa;
    private TextView azione;
    private TextView dispositivo;


    public static CheckBox chkSelected;



    public MyViewHolder(View v) {
        super(v);
        nomeParcheggio = (TextView) v.findViewById(R.id.id_park);
        data = (TextView) v.findViewById((R.id.data));
        stato = (TextView) v.findViewById((R.id.stato));
        descrizione = (TextView) v.findViewById((R.id.id_descrizione));
        targa = (TextView) v.findViewById((R.id.targa));
        azione = (TextView) v.findViewById((R.id.azione));
        dispositivo = (TextView) v.findViewById((R.id.dispositivo));

        chkSelected = (CheckBox) v.findViewById(R.id.visto);
        v.setOnClickListener(this);


    }

    @Override
    public void onClick(View v) {
        int position = getAdapterPosition();
        listener.mClick(v, this.getLayoutPosition());
        // It will be called if you click on a list item. variable position contains the position of the item in the adapter. You can do whatever you want with the position..
    }
}
}




Aucun commentaire:

Enregistrer un commentaire