it is list of student selected by me it is list of student already selected when i scroll list当我检查上方单选按钮时,已经检查了底部的单选按钮。我可以设置单选按钮以使其状态永不更改滚动列表。
这是我的studentlist.java代码
public class studentlist extends AppCompatActivity {
private static final String TAG_RESULTS="result";
private static final String TAG_ID = "id";
private static final String TAG_ClassDet = "classdetails";
private static final String TAG_YEAR = "yearof";
private static final String TAG_BRANCH ="branchof";
private static final String TAG_SECTION ="sectionof";
private static String KEY_SUCCESS = "success";
private static String KEY_ERROR = "error";
private ArrayList<HashMap<String, String>> studentattendence,studentstatus;
ListView listView;
ArrayAdapter<Model> adapter;
List<Model> list = new ArrayList<Model>();
public ArrayList<HashMap<String, String>> personList;
private static final String KEY_STUDENT = "student";
private RadioGroup radioGroup;
private RadioButton radiobtn;
public Button ok;
private RadioButton radioButton;
HashMap<String, String> contact,contactstatus;
private String convertjson;
private String statusofattendence;
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.liststudent);
listView = (ListView) findViewById(R.id.my_list);
studentattendence=new ArrayList<HashMap<String, String>>();
studentstatus=new ArrayList<HashMap<String, String>>();
personList = new ArrayList<HashMap<String, String>>();
Intent getstud = getIntent();
personList = (ArrayList<HashMap<String, String>>) getstud.getSerializableExtra(KEY_STUDENT);
ok= (Button) findViewById(R.id.ok);
Toast.makeText(getApplicationContext(), pioGroup.ersonList.get(0).get("enrolment") + "" + personList.get(1).get("enrolment") + "" + personList.get(2).get("enrolment"), Toast.LENGTH_LONG).show();
adapter = new MyAdapter(this, getModel(personList));
listView.setAdapter(adapter);
radioGroup = (RadioGroup) findViewById(R.id.radio);
listView.childDrawableStateChanged(radioGroup);
////ok button click get status of each radio button and store in hashmap
ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int child=listView.getChildCount();
ArrayList<String> radio=new ArrayList<>();
View[] vy = new View[1];
Log.d("child", String.valueOf(child));
for(int i=0;i<child;i++) {
// RadioGroup rd = (RadioGroup) listView.getChildAt(i);
View rgg=listView.getChildAt(i);
Log.d("radioview",String.valueOf(rgg));
radioGroup = (RadioGroup) rgg.findViewById(R.id.radio);
Log.d("radiogroup", String.valueOf(radioGroup));
int selectedId=radioGroup.getCheckedRadioButtonId();
radioButton = (RadioButton) rgg.findViewById(selectedId);
TextView tv= (TextView) rgg.findViewById(R.id.label);
Log.d("radiobutton and id", selectedId + "" + radioButton);
String status= radioButton.getText().toString();
Log.d("hello",status);
String enrol=tv.getText().toString();
contact = new HashMap<String, String>();
contact.put("enrollmentnum",enrol);
Log.d("enum",enrol);
studentattendence.add(contact);
contactstatus = new HashMap<String, String>();
contactstatus.put("status",status);
Log.d("ststuss",status);
studentstatus.add(contactstatus);
}
///here converting arraylist in to json string
convertjson = new Gson().toJson(studentattendence);
statusofattendence=new Gson().toJson(studentstatus);
Toast.makeText(getApplicationContext(),""+convertjson+""+statusofattendence,Toast.LENGTH_LONG).show();
NetAsync(v, convertjson,statusofattendence);
}
});
}
private List<Model> getModel(ArrayList<HashMap<String, String>> personList) {
for (int i = 0; i < personList.size(); i++) {
String enrolment = personList.get(i).get("enrolment");
list.add(new Model(enrolment));
}
return list;
}
////now we will send taked attendence to webservice
private class NetCheck extends AsyncTask<String,String,Boolean>
{
public final String convertjson,statusofattendence;
private ProgressDialog nDialog;
public NetCheck(String convertjson,String statusofattendence) {
this.convertjson=convertjson;
this.statusofattendence=statusofattendence;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
nDialog = new ProgressDialog(studentlist.this);
nDialog.setMessage("Loading..");
nDialog.setTitle("Checking Network");
nDialog.setIndeterminate(false);
nDialog.setCancelable(true);
nDialog.show();
}
@Override
protected Boolean doInBackground(String... args) {
/**
* Gets current device state and checks for working internet connection by trying Google.
**/
/*
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnected()) {
try {
URL url = new URL("http://www.google.com");
HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
urlc.setConnectTimeout(3000);
urlc.connect();
if (urlc.getResponseCode() == 200) {
return true;
}
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}*/
// return false;
return true;
}
@Override
protected void onPostExecute(Boolean th) {
if (th == true) {
nDialog.dismiss();
new ProcessRegister().execute();
} else {
nDialog.dismiss();
}
}
}
private class ProcessRegister extends AsyncTask<String, String, JSONObject> {
private ProgressDialog pDialog;
String forgotpassword;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(studentlist.this);
pDialog.setTitle("Contacting Servers");
pDialog.setMessage("Getting Data ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected JSONObject doInBackground(String... args) {
TeacherFunctions userFunction = new TeacherFunctions();
Log.d("convertjson",convertjson);
Log.d("convertjson",statusofattendence);
JSONObject json = userFunction.addAttendence(convertjson,statusofattendence);
return json;
}
@Override
protected void onPostExecute(JSONObject json) {
/**
* Checks if the Password Change Process is sucesss
**/
try {
if (json.getString(KEY_SUCCESS) != null) {
String res = json.getString(KEY_SUCCESS);
String red = json.getString(KEY_ERROR);
if(Integer.parseInt(res) == 1){
pDialog.dismiss();
Toast.makeText(getApplicationContext(),"students Attendence Uploaded successfully"+json.getString("user"),Toast.LENGTH_LONG).show();
}
else if (Integer.parseInt(red) == 2)
{ pDialog.dismiss();
}
else {
pDialog.dismiss();
}
}}
catch (JSONException e) {
e.printStackTrace();
pDialog.dismiss();
Toast.makeText(getApplicationContext()," not Uploaded successfully"+e,Toast.LENGTH_LONG).show();
}
}}
public void NetAsync(View view, String convertjson,String statusofattendence){
new NetCheck(convertjson,statusofattendence).execute();
}
}
model.java的代码
public class Model {
private String name;
private boolean selected;
public Model(String name) {
this.name = name;
}
public String getName() {
return name;
}
public boolean isSelected() {
return selected;
}
public void setSelected(boolean selected) {
this.selected = selected;
}
}
MyAdapter.java的代码
public class MyAdapter extends ArrayAdapter<Model> {
private final List<Model> list;
private final Activity context;
boolean checkAll_flag = false;
boolean checkItem_flag = false;
public MyAdapter(Activity context, List<Model> list) {
super(context, R.layout.row, list);
this.context = context;
this.list = list;
}
static class ViewHolder {
protected TextView text;
protected RadioButton radiobox;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if (convertView == null) {
LayoutInflater inflator = context.getLayoutInflater();
convertView = inflator.inflate(R.layout.row, null);
viewHolder = new ViewHolder();
viewHolder.text = (TextView) convertView.findViewById(R.id.label);
viewHolder.radiobox = (RadioButton) convertView.findViewById(R.id.radio1);
viewHolder.radiobox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int getPosition = (Integer) buttonView.getTag(); // Here we get the position that we have set for the checkbox using setTag.
list.get(getPosition).setSelected(buttonView.isChecked()); // Set the value of checkbox to maintain its state.
}
});
convertView.setTag(viewHolder);
convertView.setTag(R.id.label, viewHolder.text);
convertView.setTag(R.id.radio1, viewHolder.radiobox);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.radiobox.setTag(position); // This line is important.
viewHolder.text.setText(list.get(position).getName());
viewHolder.radiobox.setChecked(list.get(position).isSelected());
return convertView;
}
}
答案 0 :(得分:1)
您在setOnCheckedChangeListener()
上使用viewHolder.radiobox
。这是非常糟糕的代码,因为每次convertView
获得null
到view
,然后setOnCheckedChangeListener()
也会被调用。它将您的模态数据设置为下一个位置。如果您想检查它,只需将Log
放入onCheckedChange
或尝试将断点放入其中。
试试这个:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if (convertView == null) {
LayoutInflater inflator = context.getLayoutInflater();
convertView = inflator.inflate(R.layout.row, null);
viewHolder = new ViewHolder();
viewHolder.text = (TextView) convertView.findViewById(R.id.label);
viewHolder.radiobox = (RadioButton) convertView.findViewById(R.id.radio1);
convertView.setTag(viewHolder);
convertView.setTag(R.id.label, viewHolder.text);
convertView.setTag(R.id.radio1, viewHolder.radiobox);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.radiobox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
if(viewHolder.radiobox.isChecked())
{
viewHolder.radiobox.setChecked(false);
}
else
{
viewHolder.radiobox.setChecked(true);
}
list.get(getPosition).setChecked( viewHolder.radiobox.isChecked()); // Set the value of checkbox to maintain its state.
}
});
viewHolder.text.setText(list.get(position).getName());
viewHolder.radiobox.setChecked(list.get(position).isChecked());
return convertView;
}
希望它会对你有所帮助。
答案 1 :(得分:0)
enter code here
getListView().setOnScrollListener(this);***
enter code here
public void onScroll(AbsListView view, int firstCell, int cellCount, int itemCount) {
RadioButton rb1 = (RadioButton) findViewById(R.id.option1);
rb1.setChecked(flase);
}
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
答案 2 :(得分:-1)
实际上一切都工作不正常,然后我用4个按钮代替单选按钮移动到listview。