gpt4 book ai didi

android - 如何获取用户选择的值

转载 作者:行者123 更新时间:2023-11-29 23:49:00 25 4
gpt4 key购买 nike

我正在尝试获取我的应用程序中选中复选框的值。我想在应用程序中获取用户检查的项目并将项目发送到服务器。我已尝试这样做,但我不知道该怎么做?

适配器

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;
CardView cv;

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.btnProblems);
cbProblems = (CheckBox) itemView.findViewById(R.id.cbProblems);

overrideFonts(context, tvProblems);
}
}
private void overrideFonts(final Context context, final View v) {
try {
if (v instanceof ViewGroup) {
ViewGroup vg = (ViewGroup) v;
for (int i = 0; i < vg.getChildCount(); i++) {
View child = vg.getChildAt(i);
overrideFonts(context, child);
}
} else if (v instanceof TextView ) {
((TextView) v).setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/RobotoCondensedBold.ttf"));
}
} catch (Exception e) {
}
}

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");

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

}
});

} else if (holder instanceof ItemViewHolder) {
ItemViewHolder itemView = (ItemViewHolder) holder;

//in some cases, it will prevent unwanted situations
itemView.cbProblems.setOnCheckedChangeListener(null);

//if true, your checkbox will be selected, else unselected
itemView.cbProblems.setChecked(mDataset.get(position-1).isSelected());

itemView.tvPrice.setText(mDataset.get(position - 1).getPrice());
itemView.tvProblems.setText(mDataset.get(position - 1).getProblems());


itemView.cbProblems.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

if (buttonView.isPressed()){
mDataset.get(position - 1).setSelected(isChecked);
Toast.makeText(context, "postion " + position + "Boolean value " + Boolean.valueOf(isChecked), Toast.LENGTH_SHORT).show();;
}

}
});

}
}
} 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);
overrideFonts(context, tvHeader);
}
}

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


public FooterViewHolder(View view) {
super(view);
btnSubmitProblem = (Button) view.findViewById(R.id.btnProblems);
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);
cv = (CardView)itemView.findViewById(R.id.cards);

}
}

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

主要 Activity

public class ProblemsActivity extends AppCompatActivity {

private String PROBLEM_URL = MYURL.url + "get-problems";

CardView cardView;
CheckBox breakCheckBox;
private ListView getListValue, listView;
SharedPreferences sharedPreferences;
Button btnSubmitProblems;
String problems;
List<Problems> listProblems = new ArrayList<>();
DividerItemDecoration mDividerItemDecoration;
Toolbar toolbar;
private double price = 0.0;
private RecyclerView mRecyclerView;
MyAdapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/


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

toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setTitleTextColor(Color.parseColor("#FFFFFF"));
if (getSupportActionBar() != null){
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
}
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, listProblems);

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

}, 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) {

}
});
}

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

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

if (id == R.id.logout){

SharedPreferences preferences = getSharedPreferences("Mypref", 0);
preferences.edit().clear().commit();
Intent intent = new Intent(ProblemsActivity.this, HomeActivity.class); // Create an Intent
startActivity(intent); // Move to Verification Activity
Toast.LENGTH_SHORT).show();
}else if (id == android.R.id.home){
finish();
}
return super.onOptionsItemSelected(item);
}

}

几天来我一直在考虑如何做到这一点,但我不知道该怎么做。任何人都可以帮助我。

最佳答案

您正在将选中的状态保存在列表中,因此在提交点击时您可以迭代列表并获取选定的状态,例如:

mAdapter.setProblemListener(new MyAdapter.ProblemListener() {
@Override
public void onProblemSelected(View view, List<Problems> problem, int position) {
List<Problems> selectedProblems=new ArrayList();
for(Problems item: problem)
{
if(item.isSelected())
{
selectedProblems.add(item);
}
}
sendToServer(selectedProblems);
}
});

这样您将获得选定的项目并可以实现您拥有的发送到服务器逻辑。

回收站 View 适配器中代码的一个问题是您仅在选择项目时设置选定状态,但项目也可以取消选择,因为它是一个复选框,因此您还必须在项目时更改选定状态未选中,您可以简单地删除 if 条件并执行此操作,

itemView.cbProblems.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

mDataset.get(position - 1).setSelected(isChecked);
Toast.makeText(context, "postion " + position + "Boolean value " + Boolean.valueOf(isChecked), Toast.LENGTH_SHORT).show();;


}
});

关于android - 如何获取用户选择的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51114496/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com