2

我是 android 编程的新手,我正在开发一个应用程序来总结客户选择的价格。我的问题是检测用户选择了哪些复选框。

我的适配器

package com.example.system2.tranxav.adapters;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;

import com.example.system2.tranxav.R;
import com.example.system2.tranxav.model.Problems;

import java.util.List;
import java.util.Locale;

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>{

Context context;
private List<Problems> mDataset;
private static final int TYPE_HEADER = 0;
private static final int TYPE_FOOTER = 1;
private static final int TYPE_ITEM = 2;
private ProblemListener setProblemListener;

public void setProblemListener(ProblemListener problemListener){
    this.setProblemListener = problemListener;
}
public class ViewHolder extends RecyclerView.ViewHolder{
    public TextView tvProblems, tvPrice;
    public Button btnProblem;
    Button btnSubmitProblem;
    CheckBox cbProblems;
    public ViewHolder(View itemView) {
        super(itemView);
        tvProblems = (TextView) itemView.findViewById(R.id.tvProblems);
        tvPrice = (TextView) itemView.findViewById(R.id.tvPrice);
        btnSubmitProblem = (Button) itemView.findViewById(R.id.btnProblem);
        cbProblems = (CheckBox) itemView.findViewById(R.id.cbProblems);
    }
}

public void add(List<Problems> itemList){
    mDataset = itemList;
    notifyDataSetChanged();
}

public void remove(Problems item){
    int position = mDataset.indexOf(item);
    mDataset.remove(position);
    notifyItemRemoved(position);
}

public MyAdapter(Context con, List<Problems> myDataset){
    mDataset = myDataset;
    this.context = con;
}
@Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {


    View itemView;
    if (viewType == TYPE_ITEM) {
        //Inflating recycle view item layout
        itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.problem_content, parent, false);
        return new ItemViewHolder(itemView);

    } else if (viewType == TYPE_HEADER) {
        //Inflating header view
        itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.problem_recycler_header, parent, false);
        return new HeaderViewHolder(itemView);
    } else if (viewType == TYPE_FOOTER) {
        //Inflating footer view
        itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.problem_recycler_footer, parent, false);
        return new FooterViewHolder(itemView);
    }
    throw new RuntimeException("there is no type that matches the type " + viewType + " + make sure your using types correctly");
}

@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {

    try {
    if (holder instanceof HeaderViewHolder) {

        HeaderViewHolder headerHolder = (HeaderViewHolder) holder;
        headerHolder.tvHeader.setText("Please kindly ask the mechanic what the problem is and check them to continue");
        headerHolder.tvHeader.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(context, "You clicked at Header View!", Toast.LENGTH_SHORT).show();
            }
        });
    } else {
        if (holder instanceof FooterViewHolder) {

            final FooterViewHolder footerHolder = (FooterViewHolder) holder;
            footerHolder.btnSubmitProblem.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                  //  footerHolder.cbProblems.setOnClickListener(null);
                   setProblemListener.onProblemSelected(v, mDataset, position);
                    Log.i("info","You are welcome");

                }
            });

        } else if (holder instanceof ItemViewHolder) {
            ItemViewHolder itemView = (ItemViewHolder) holder;
            itemView.tvPrice.setText(mDataset.get(position - 1).getPrice());
            itemView.tvProblems.setText(mDataset.get(position - 1).getProblems());

        }
    }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
@Override
public int getItemCount() {
    return mDataset.size()+2;
}

public int getItemViewType(int position) {

    if (position == 0) {
        return TYPE_HEADER;
    } else if (position == mDataset.size() + 1) {
        return TYPE_FOOTER;
    }
    return TYPE_ITEM;
}

private class HeaderViewHolder extends MyAdapter.ViewHolder {
    TextView tvHeader;

    public HeaderViewHolder(View view) {
        super(view);
        tvHeader = (TextView) view.findViewById(R.id.tvHeader);
    }
}

private class FooterViewHolder extends MyAdapter.ViewHolder {
    Button btnSubmitProblem;
    CheckBox cbProblems;


    public FooterViewHolder(View view) {
        super(view);
        btnSubmitProblem = (Button) view.findViewById(R.id.btnProblem);
        cbProblems = (CheckBox) itemView.findViewById(R.id.cbProblems);
    }
}

private class ItemViewHolder extends MyAdapter.ViewHolder {
    TextView tvProblems, tvPrice;


    public ItemViewHolder(View itemView) {
        super(itemView);
        tvProblems = (TextView) itemView.findViewById(R.id.tvProblems);
        tvPrice = (TextView) itemView.findViewById(R.id.tvPrice);

    }
}

private class CheckboxViewHolder extends MyAdapter.ViewHolder {
    public CheckboxViewHolder(View itemView) {
        super(itemView);
        cbProblems = (CheckBox) itemView.findViewById(R.id.cbProblems);

    }
}

public interface ProblemListener{
    void onProblemSelected(View view, List<Problems> problem, int position);
}
}

我的模特

package com.example.system2.tranxav.model;


public class Problems {
private String problems;
private String price;
private boolean selected;


public boolean isSelected() {
    return selected;
}
public void setSelected(boolean selected) {
    this.selected = selected;
}

public String getPrice() {
    return price;
}

public void setPrice(String price) {
    this.price = price;
}

public void setProblems(String problems) {
    this.problems = problems;
}

public String getProblems() {
    return problems;
}
}

我的活动

public class ProblemsActivity extends AppCompatActivity {

private String PROBLEM_URL = "http://192.168.1.35:85/tranxavApi/public/api/get-problems";

CardView cardView;
CheckBox breakCheckBox;
private ListView getListValue, listView;
SharedPreferences sharedPreferences;
Button btnSubmitProblems;
String problems;
List<Problems> listProblems = new ArrayList<>();



protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_problems);

    mRecyclerView = (RecyclerView) findViewById(R.id.problem_recycle_view);
    mRecyclerView.setHasFixedSize(true);

    mLayoutManager = new LinearLayoutManager(ProblemsActivity.this, LinearLayoutManager.VERTICAL, false);
    mRecyclerView.setLayoutManager(mLayoutManager);

    mAdapter = new MyAdapter(ProblemsActivity.this, null);

    RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
    StringRequest stringRequest = new StringRequest(Request.Method.GET, PROBLEM_URL, new com.android.volley.Response.Listener<String>() {

        @Override
        public void onResponse(String response)
        {
            mRecyclerView.setAdapter(mAdapter);
            JSONParse jsonParse = new JSONParse(response);
            jsonParse.parseJSON();
            listProblems = jsonParse.getProblems();
            mAdapter.add(listProblems);
        }

    }, new com.android.volley.Response.ErrorListener() {

        @Override

        public void onErrorResponse(VolleyError error) {
            Log.i("Info", "Failed");
            error.printStackTrace();

        }

    });
    int socketTimeout = 30000;
    RetryPolicy policy = new DefaultRetryPolicy(socketTimeout, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
    stringRequest.setRetryPolicy(policy);
    requestQueue.add(stringRequest);

    mRecyclerView.addItemDecoration(new DividerItemDecoration(ProblemsActivity.this, LinearLayoutManager.HORIZONTAL));
    mAdapter.setProblemListener(new MyAdapter.ProblemListener() {
        @Override
        public void onProblemSelected(View view, List<Problems> problem, int position) {

            Toast.makeText(getApplicationContext(),String.valueOf(problem.size()), Toast.LENGTH_LONG).show();

            double price = 0;
            for (int i = 0; i < problem.size(); i++) {
                Problems prob = problem.get(i);
                if (prob.isSelected()) {
                    price += Double.parseDouble(prob.getPrice());
                    Log.i("info", String.valueOf(price));
                    Toast.makeText(getApplicationContext(), "Selected", Toast.LENGTH_LONG).show();
                }
            }
        }
    });
}

public void paymentFN(View view) {
    Intent intent = new Intent(ProblemsActivity.this, TotalAmountActivity.class); // Create an Intent
    startActivity(intent); // Move to Verification Activity
}

public void getProblems() {



}

}

当用户选择不同的复选框时,应用程序应该添加从复选框中获取的所有值。

4

0 回答 0