与适配器复选框关联的toogle侦听器

时间:2017-05-25 15:31:05

标签: java android android-recyclerview

我使用recycleView / CardLayout建立一个简单的应用程序,我跟着this教程。

我看到许多问题已经解决了卡片点击问题,但我需要的是当用户点击标题或用户点击每张卡片中的图片时,处理具有不同操作的听众。

我的问题是,当用户点击一个单选按钮时,我需要在监听器上获取无线电,这样我就可以处理检查和取消选中,因为我需要将其传递给构建器

这就是我现在所拥有的:

public class SimiliarPlantsAdapter extends RecyclerView.Adapter<SimiliarPlantsAdapter.PlantViewHolder>{

    ArrayList<Plant> plants = new ArrayList<Plant>();
    Context context;

    public static class PlantViewHolder extends RecyclerView.ViewHolder {
        CardView cv;
        TextView plantName;
        CheckBox plantCheck;
        ImageView plantPhoto;

        PlantViewHolder(View itemView) {
            super(itemView);
            cv = (CardView)itemView.findViewById(R.id.cv);
            plantName = (TextView)itemView.findViewById(R.id.plantName);
            plantCheck = (CheckBox)itemView.findViewById(R.id.plantCheck);
            plantPhoto = (ImageView)itemView.findViewById(R.id.plantPhoto);
        }

    }

    @Override
    public PlantViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.similiar_photo_row, viewGroup, false);
        PlantViewHolder pvh = new PlantViewHolder(v);
        return pvh;
    }

    @Override
    public void onBindViewHolder(PlantViewHolder holder, int position) {
        holder.plantName.setText(plants.get(position).getSpecie());
        holder.plantCheck.setText("Are you sure this is the plant?");


        Log.d("foto",String.valueOf(holder.plantName));


        String urlFoto = "http://10.0.2.2:3000/images/" + holder.plantName.getText().toString() + "/Thumbnail.jpg";
        Picasso.with(context)
                .load(urlFoto)
                .resize(250, 250)
                .into(holder.plantPhoto);

    }

    @Override
    public int getItemCount() {
        return plants.size();
    }

    public SimiliarPlantsAdapter(ArrayList<Plant> plants,Context context) {
        this.plants = plants;
        this.context = context;
    }

    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
    }

}

我的活动

public class SimiliarPhotos extends AppCompatActivity implements IResult,SimiliarPlantsAdapter.OnItemClickListener {

    RecyclerView rv;
    LinearLayoutManager llm;
    ArrayList<Plant> plants = new ArrayList<Plant>();
    SimiliarPlantsAdapter adapter;
    CheckBox checkBox;

    VolleyService mVolleyService;
    IResult mResultCallback = null;
    final String GETREQUEST = "GETCALL";

    //login url connection
    final String URL = "http://10.0.2.2:3000/plants";


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

        rv = (RecyclerView)findViewById(R.id.rv);
        llm = new LinearLayoutManager(this);
        llm.setAutoMeasureEnabled(true);
        rv.setLayoutManager(llm);


        initializeAdapter();

        initVolleyCallback();

        mVolleyService = new VolleyService(mResultCallback,this);

        mVolleyService.getDataVolley(GETREQUEST,URL);
    }

    @Override
    public void notifySuccess(String requestType, JSONObject response) {
        Log.d("resposta",response.toString());
    }

    @Override
    public void notifySuccess(String requestType, JSONArray response) {
        Log.d("resposta",response.toString());
    }

    @Override
    public void notifyError(String requestType, VolleyError error) {
        Log.d("resposta",error.toString());
    }

    void initVolleyCallback(){
        mResultCallback = new IResult() {
            @Override
            public void notifySuccess(String requestType, JSONObject response) {
        }

        @Override
        public void notifySuccess(String requestType, JSONArray response) {
            Plant plant;
            Log.d("ENTERED","ENTEREDHERE1");
            // iterate over the JSONArray response
            for (int i=0; i < response.length(); i++) {
                try {
                    JSONObject object = response.getJSONObject(i); // get the individual object from JSONArray
                    int id = Integer.parseInt(object.getString("id")); // get the unique identifier from the object
                    String specie = object.getString("specie"); // get the name of the specie from the object
                    String description = object.getString("description"); // get the description of the object
                    plant = new Plant(id,specie,description); // construct the object
                    Log.d("plant",String.valueOf(plant));
                    plants.add(plant); // add the object to the arraylist so it can be used on the cardLayout

                } catch (JSONException e) {
                    Log.d("ENTERED",e.toString());
                    e.printStackTrace();
                }
            }
            adapter.notifyDataSetChanged();
        }

        @Override
        public void notifyError(String requestType, VolleyError error) {
            Log.d("resposta",error.toString());
        }
    };
}

public void initializeAdapter(){
    Log.d("plants",String.valueOf(plants.size()));
    adapter = new SimiliarPlantsAdapter(plants,SimiliarPhotos.this,SimiliarPhotos.this);
    rv.setAdapter(adapter);
}


@Override
public void onTitleClicked(int position, int id, View clickedview) {

}

@Override
public void onImageClicked(int position, View clickedview) {

}

@Override
public void onCheckClicked(int position, String specie, View clickedview) {
    adapter.getItemId(position);
    CreateDialog(specie);
}

public void CreateDialog(String specie){
    AlertDialog.Builder builder;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        builder = new AlertDialog.Builder(SimiliarPhotos.this, android.R.style.Theme_Material_Dialog_Alert);
    } else {
        builder = new AlertDialog.Builder(SimiliarPhotos.this);
    }
    builder.setTitle("Esta é a sua escolha?")
            .setMessage("Confirma " + specie + " como a planta que fotografou?")
            .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    //TODO associa nome da planta a fotografia na base de dados
                }
            })
            .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                   dialog.dismiss();
                }
            })
            .setIcon(android.R.drawable.ic_dialog_alert)
            .show();
    }
}

volley init是请求,对问题不是那么重要,因为我正确地获取了数据

1 个答案:

答案 0 :(得分:0)

您需要为CheckBox实现CompoundButton.OnCheckedChangeListener。

Your_CHECK_BOX.setOnCheckedChangeListener(checkedChangeListener);
CompoundButton.OnCheckedChangeListener checkedChangeListener = new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
if(compoundButton == YOUR_CHECK_BOX) {
    if(isChecked) {
        // when check box is checked, call your action listener just like you did for title click/image click etc
    } else {
        // when check box is unchecked, call your action listener just like you did for title click/image click etc
    }
}