繁体   English   中英

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

[英]toogle listener associated with checkbox with adapter

我用recycleView / CardLayout构建了一个简单的应用程序,我遵循了教程。

我看到许多有关卡单击的问题,但是我需要的是在用户单击标题或用户单击每张卡中的图像时以不同的方式处理侦听器。

我的问题是,当用户单击单选按钮时,我需要将广播调到侦听器上,以便我可以处理检查和取消检查,因为我需要将其传递给生成器

这是我目前所拥有的:

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();
    }
}

齐射init是请求,对这个问题并不重要,因为我正确地获取了数据

您需要为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
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM