gpt4 book ai didi

android - 如何在方向更改时保留 android ListView 中单选按钮的值

转载 作者:太空狗 更新时间:2023-10-29 13:54:05 24 4
gpt4 key购买 nike

我有不同的横向和纵向模式布局。当我从单选组中的单选按钮中选择值并更改方向时,将显示新布局并且所有值都消失。请帮忙。

package com.hp.opiniondesknew;

import android.content.Context;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.os.ParcelableCompat;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
* Created by HP on 15-12-2016.
*/

public class FragmentFeedback extends Fragment {



private ArrayList<Questions> questionsArrayList;
HashMap<Questions, String> mapAns=new HashMap<Questions, String>();
private MyAdapter myAdapter;
private ListView listFeedbackQuestions;
private Button btSubmitFeedback;
private String selectedAns;
ArrayList<String> rbAnswersArray = new ArrayList<>();

int yesCount=0,noCount=0;
Questions questionPojo;

public FragmentFeedback() {

// setRetainInstance(true);
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

View view=inflater.inflate(R.layout.feedback_layout,container,false);

listFeedbackQuestions= (ListView)view.findViewById(R.id.opinion_desk_feedback_lv_questions);
btSubmitFeedback=(Button)view.findViewById(R.id.opinion_desk_feedback_bt_submit);

questionsArrayList= new ArrayList<Questions>();


addlistview();
initializeBtnSubmit(btSubmitFeedback);
return view;
}

private void initializeBtnSubmit( Button btSubmitFeedback) {



btSubmitFeedback.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

//---------

try
{

for(Map.Entry m:mapAns.entrySet()){
// System.out.println(m.getKey"()+" "+m.getValue());
Log.e("key value",m.getKey()+""+m.getValue());
}
if(mapAns.size()==0 || mapAns.size()< questionsArrayList.size())
{
Toast.makeText(getContext(),"Ans all q",Toast.LENGTH_SHORT).show();
}
else
{
int countYes = Collections.frequency(mapAns.values(), "yes");
{
Log.e("yescount",countYes+"");
}

int countNo = Collections.frequency(mapAns.values(), "no");
{
Log.e("noCount",countNo+"");
Log.e("mapsize",mapAns.size()+"");
}

if(countYes>countNo)
{

Fragment someFragment = new FragmentTestimonial();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.content_frame, someFragment ); // give your fragment container id in first parameter
// transaction.addToBackStack(null); // if written, this transaction will be added to backstack
transaction.commit();
Log.e("noCount",countNo+"");
Log.e("yescount",countYes+"");
// Log.e("yes is greater",rbAnswersArray.size()+"");

}
else
{

Fragment someFragment = new FragmentSuggestions();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.content_frame, someFragment ); // give your fragment container id in first parameter
// transaction.addToBackStack(null); // if written, this transaction will be added to backstack
transaction.commit();
Log.e("noCount",countNo+"");
Log.e("yescount",countYes+"");

}
mapAns.clear();
}


}catch (Exception e){e.printStackTrace();}

//-----

}
});

}




@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);

getActivity().setTitle("Feedback");
}

public void addlistview()
{
try
{
for (int i=0;i<5;i++) {
Questions questions = new Questions();

// questions.setQuestion(i+"");
questions.setQuestion("Did you like the performance of the service?");
questions.setQuestionNo(i);
questionsArrayList.add(questions);

myAdapter = new MyAdapter(getContext(), questionsArrayList);
listFeedbackQuestions.setAdapter(myAdapter);

if(selectedState != null) {
listFeedbackQuestions.onRestoreInstanceState(selectedState);
}

// Log.e("pojo",questions.toString());
}
}
catch (Exception e){e.printStackTrace();}



}

public class MyAdapter extends BaseAdapter {
Context context;
ArrayList<Questions> questionsArrayList;
LayoutInflater inflator;

public MyAdapter(Context context, ArrayList<Questions> questionsArrayList) {
this.context = context;
this.questionsArrayList = questionsArrayList;
inflator= LayoutInflater.from(context);
}

@Override
public int getCount() {

return questionsArrayList.size();
}


@Override
public Object getItem(int position) {
return questionsArrayList.get(position);
}


@Override
public long getItemId(int position) {
return 0;
}


@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// Questions questionsPojo= new Questions();
Holder holder= new Holder();
if(convertView==null)
{
convertView=inflator.inflate(R.layout.feedback_question_row,parent,false);
holder.tvQuestion=(TextView) convertView.findViewById(R.id.opinion_desk_feedback_tv_question);
holder.rGroupButtons=(RadioGroup)convertView.findViewById(R.id.opinion_desk_rg_yes_no);
holder.rbYes=(RadioButton) convertView.findViewById(R.id.opinion_desk_feedback_rb_yes);
holder.rbNo=(RadioButton)convertView.findViewById(R.id.opinion_desk_feedback_rb_no);

convertView.setTag(holder);

}
else
{
holder=(Holder)convertView.getTag();
}

questionPojo=questionsArrayList.get(position);

holder.tvQuestion.setText(questionsArrayList.get(position).getQuestion());
final Holder finalHolder = holder;
holder.rGroupButtons.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {

if (finalHolder.rbYes.isChecked())
{
Log.e("no",position+"");

finalHolder.selectedAnswer="yes";
questionsArrayList.get(position).setAnswer(finalHolder.selectedAnswer);

selectedAns= questionsArrayList.get(position).getAnswer();

rbAnswersArray.add(selectedAns);

yesCount++;

}
else

{
Log.e("no",position+"");

finalHolder.selectedAnswer="no";
questionsArrayList.get(position).setAnswer(finalHolder.selectedAnswer);
selectedAns=questionsArrayList.get(position).getAnswer();


noCount++;

}
mapAns.put(questionsArrayList.get(position),selectedAns);
rbAnswersArray.add(selectedAns);
//
}
});



return convertView;
}
}


public class Holder
{
TextView tvQuestion;
RadioGroup rGroupButtons;
RadioButton rbYes,rbNo, rbSelectedAnswer;
String selectedAnswer, ansNo;

}


@Override
public void onDestroyOptionsMenu() {
super.onDestroyOptionsMenu();


}

// @Override
// public void onSaveInstanceState(Bundle outState) {
// super.onSaveInstanceState(outState);
// }
}

我正在为 ListView 使用 ListView 和自定义适配器。我正在启动来自导航 Activity 的 fragment 反馈。 OnSavedInstanceState 不工作,应用程序在 onRestore 方法中崩溃。我可能做错了什么。

最佳答案

您必须使用 Bundle savedInstanceState 来检索您的值。您可以将它们保存在 onSaveInstanceState 方法中。

这里是文档的链接 https://developer.android.com/guide/components/activities/activity-lifecycle.html

关于android - 如何在方向更改时保留 android ListView 中单选按钮的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41543726/

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