samedi 21 juillet 2018

How can I get the selected check item in the listView and pass the value to next activity? Sorry for my bad english. Android

public class FavouriteLodging extends AppCompatActivity{

List<Lodging> ll = new ArrayList<>();
List<String> compareList = new ArrayList<>();

-- This usually is to get the selected checkbox item

public void prepareSelection(View view, int Position){
    if(((CheckBox)view).isChecked()){
        compareList.add(ll.get(Position).getLodgingId());
    }else{
        compareList.remove(ll.get(Position).getLodgingId());
    }
}

--This to pass the selected item and send to next activity

public void SelectedItem(){
    Intent intent = new Intent(FavouriteLodging.this, ViewCompareDetails.class);
    intent.putExtra("UserID", getIntent().getStringExtra("UserID"));
    intent.putExtra("selected_list", (Parcelable) compareList);
    startActivity(intent);
}

-- When a user clicks on the menu item, if there is no checkbox is click should display "At least Select Two Item to Compare", otherwise should be pass to next activity which performs SelectedItem.

public boolean onOptionsItemSelected(MenuItem item) {
    if(item.getItemId() == android.R.id.home){
        onBackPressed();
    }
    if(item.getItemId() == R.id.item_compare){
        if(compareList.isEmpty() && compareList.size() == 1){
            Toast.makeText(FavouriteLodging.this, "At least Select Two Item to Compare", Toast.LENGTH_SHORT).show();
        }else
        //SelectedItem();
            Toast.makeText(FavouriteLodging.this, "Got Two Item", Toast.LENGTH_SHORT).show();

    }

   /*if(item.getItemId() == R.id.item_compare){
       Toast.makeText(FavouriteLodging.this, "At least Select Two Item to Compare", Toast.LENGTH_SHORT).show();
   }*/
    return super.onOptionsItemSelected(item);
}

}

--This is my Adapter --How can I get the selected item from checkbox and pass to next activity

public class LodgingAdapter extends RecyclerView.Adapter{

private List<Lodging> ll;
public List<Integer> mylist = new ArrayList<>();
private static ClickListener clickListener;
private static int CHECKED_BOX_MAXIMUM = 0;

public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener,CompoundButton.OnCheckedChangeListener{

    public CheckBox cbBox;


    public ViewHolder(View v) {
        super(v);
        v.setOnClickListener(this);
        v.setOnLongClickListener(this);
        cbBox = v.findViewById(R.id.cbCompare);
        cbBox.setOnCheckedChangeListener(this);

    }

    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

        switch (buttonView.getId()){
            case R.id.cbCompare :
                if(isChecked == true){
                    CHECKED_BOX_MAXIMUM++;
                }else{
                    CHECKED_BOX_MAXIMUM--;
                }if (isChecked && CHECKED_BOX_MAXIMUM > 2) {
                     cbBox.setChecked(false);
                     CHECKED_BOX_MAXIMUM--;
            }
        }
    }
}

public void onBindViewHolder(LodgingAdapter.ViewHolder holder, int position) {
    Lodging l = ll.get(position);
    holder.title.setText(l.getTitle());
    holder.price.setText("RM" +String.format("%.2f",l.getPrice()));
    holder.type.setText(l.getLodgingType());
    Glide.with(holder.itemView.getContext())
            .load(l.getImage())
            .diskCacheStrategy(DiskCacheStrategy.NONE)
            .skipMemoryCache(true)
            .centerCrop()
            .into(holder.picture);
}
}




Aucun commentaire:

Enregistrer un commentaire