Reputation: 3
I have a DisplayStudentName.java file which is my main activity file. There is a model class which is used to get and set data and a MyCustomAdapter.java file which extends ArrayAdapter for listview functioning.
The checkbox is clicked but value is not added in the arraylist(named data; type String).
DisplayStudentName.java
public class DisplayStudentNames extends AppCompatActivity {
String myJSON;
private static final String TAG_RESULTS = "result";
private static final String TAG_ROLL = "RollNo";
private static final String TAG_NAME = "Name";
JSONArray peoples = null;
ListView list;
ArrayList<String> checkedValue;
Button b1;
MyCustomAdapter dataAdapter = null;
ArrayList<Student> personList=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_student_names);
final Spinner sbranch = (Spinner) findViewById(R.id.branch);
final Spinner ssemester = (Spinner) findViewById(R.id.semester);
final Spinner ssubject = (Spinner) findViewById(R.id.edit_subject);
//String branch=sbranch.getSelectedItem().toString();
//String semester=ssemester.getSelectedItem().toString();
//String subject=ssubject.getSelectedItem().toString();
String branch = "cs";
String semester = "7";
String subject = "Soft Computing";
b1 = (Button) findViewById(R.id.submit);
list = (ListView) findViewById(R.id.listView);
new DataFetch().execute(branch, semester, subject);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Student student = (Student) parent.getItemAtPosition(position);
String Name = student.getName();
//String s=(String) ((TextView) view.findViewById(R.id.roll)).getText();
Toast.makeText(getApplicationContext(), "Clicked: " + Name, Toast.LENGTH_SHORT).show();
}
});
//submitAttendance();
}
class DataFetch extends AsyncTask<String, String, String> {
ArrayList<NameValuePair> params;
private ProgressDialog pDialog;
private String url_fetch_data = "http://192.168.1.4/AttendanceApp/fetch_data.php";
InputStream is = null;
String res = "";
protected void onPreExecute() {
super.onPreExecute();
}
protected String doInBackground(String... args) {
String branch = args[0];
String semester = args[1];
String subject1 = args[2];
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url_fetch_data);
params = new ArrayList<NameValuePair>(3);
params.add(new BasicNameValuePair("branch", branch));
params.add(new BasicNameValuePair("semester", semester));
params.add(new BasicNameValuePair("subject", subject1));
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
BufferedReader br = new BufferedReader(new InputStreamReader(is, "utf-8"), 8);
StringBuilder total = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
total.append(line);
}
String result = total.toString();
return result;
} catch (Exception e1) {
res = "error:" + e1.getMessage().toString();
e1.printStackTrace();
}
return res;
}
protected void onPostExecute(String httpResponse) {
myJSON = httpResponse;
showList();
}
}
protected void showList() {
try {
JSONObject jsonObj = new JSONObject(myJSON);
peoples = jsonObj.getJSONArray("result");
personList = new ArrayList<Student>();
for (int i = 0; i < peoples.length(); i++) {
Student student = new Student();
JSONObject c = peoples.getJSONObject(i);
String roll = c.getString(TAG_ROLL);
String name = c.getString(TAG_NAME);
student.setName(name);
student.setRollNo(roll);
personList.add(student);
}
dataAdapter = new MyCustomAdapter(this, R.layout.list_item, personList);
list.setAdapter(dataAdapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
MyCustomAdapter.java
public class MyCustomAdapter extends ArrayAdapter {
public ArrayList<Student> personList=null;
private Context context=null;
private static LayoutInflater inflater=null;
private ArrayList<String> data=null;
private View vi;
ViewHolder holder=null;
public MyCustomAdapter(Context context, int resource, ArrayList<Student> personList) {
super(context, resource, personList);
this.context=context;
this.personList=personList;
}
private class ViewHolder{
TextView roll;
TextView name;
CheckBox check;
}
public View getView(int position, View convertView, ViewGroup parent){
Log.v("ConvertView", String.valueOf(position));
if(convertView==null){
LayoutInflater vi = (LayoutInflater)context.getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.list_item, null);
holder=new ViewHolder();
holder.roll=(TextView)convertView.findViewById(R.id.roll);
holder.name=(TextView)convertView.findViewById(R.id.name);
holder.check=(CheckBox)convertView.findViewById(R.id.checkBox);
convertView.setTag(holder);
}
else{
holder=(ViewHolder)convertView.getTag();
}
Student student=personList.get(position);
holder.roll.setText(student.getRollno());
holder.name.setText(student.getName());
holder.check.setTag(student);
holder.check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
data.add(holder.check.getText().toString());
} else {
data.remove(holder.check.getText().toString());
}
}
});
return convertView;
}
}
Student.java -Model class
public class Student {
String rollno;
String name;
Boolean checkbox;
public Student(){
}
public Student(String rollno, String name, Boolean status){
super();
this.rollno=rollno;
this.name=name;
this.checkbox=status;
}
public String getRollno(){
return rollno;
}
public void setRollNo(String rollno){
this.rollno=rollno;
}
public String getName(){
return name;
}
public void setName(String name){
this.name=name;
}
public Boolean isCheckbox(){
return checkbox;
}
public void setCheckbox(boolean checkbox) {
this.checkbox= checkbox;
}
Please tell me what is wrong with my code? }
Upvotes: 0
Views: 101
Reputation: 6791
Whenever you are making any change in the dataset, call notifyDataSetChanged()
.
Thus it will be like this:
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
data.add(holder.check.getText().toString());
} else {
data.remove(holder.check.getText().toString());
}
notifyDataSetChanged();
}
Upvotes: 1