I am trying to make a list view in which every item has two things, a description in textview and a toggle button. The details in the textview are dynamically loaded by the AsyncTask. At the end of the listview, I have inserted a button programatically. When that button is clicked, I need to get the data in all the textviews and the states of all the ToggleButtons. This is the Code that I have used:
XML FILE
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#dfdede"
android:focusable="true"
android:focusableInTouchMode="false">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_marginTop="15dp"
android:textColor="#000000"
android:textSize="20dp" />
<ToggleButton
android:id="@+id/toggle1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:checked="true"
android:text="ToggleButton" />
<View
android:layout_width="fill_parent"
android:layout_height="2dp"
android:layout_alignBottom="@id/textView1"
android:background="#49656565" />
public class MarkAttendance extends ListActivity {
String url = "http://192.168.173.1:8080/WebApplication1/androidrollsheet.jsp";
String pid = "";
ArrayList<String> array = new ArrayList<String>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
pid = getIntent().getStringExtra("pid");
new LoadRollSheet().execute();
final ListView lv = getListView();
Button submitButton = new Button(this);
submitButton.setText("Submit");
lv.addFooterView(submitButton);
submitButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ListAdapter adapter = lv.getAdapter();
int total = adapter.getCount() - 1;
for (int i = 0; i < total; i++) {
View rowview = adapter.getView(i, null, lv);
TextView textView = (TextView) rowview.findViewById(R.id.textView1);
ToggleButton toggle1 = (ToggleButton) rowview.findViewById(R.id.toggle1);
String studentDetails = textView.getText().toString();
String value = String.valueOf(toggle1.getText());
Log.i("VALUE ???? ", value);
}
}
});
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
String item = (String) getListAdapter().getItem(position);
Toast.makeText(this, item + " selected", Toast.LENGTH_LONG).show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_mark_attendance, menu);
return true;
}
private ProgressDialog pDialog;
class LoadRollSheet extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
*/
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MarkAttendance.this);
pDialog.setMessage("Loading products. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("pid", pid));
// getting JSON string from URL
JSONParser jParser = new JSONParser();
JSONObject json = jParser.makeHttpRequest(url, "GET", params);
// Check your log cat for JSON reponse
Log.d("All Entries: ", json.toString());
//getting Sections array from JSON object
JSONArray rollSheet = null;
try {
rollSheet = json.getJSONArray("RollSheet");
//looping through all sections
for (int i = 0; i < rollSheet.length(); i++) {
JSONObject entry = rollSheet.getJSONObject(i);
int rollNo = entry.getInt("rollNo");
int userId = entry.getInt("userId");
String studentName = entry.getString("studentName");
String value = rollNo + " " + studentName;
array.add(value);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
**/
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all products
pDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.activity_mark_attendance, R.id.textView1, array);
setListAdapter(adapter);
((BaseAdapter) getListAdapter()).notifyDataSetChanged();
}
});
}
}
}
But, the problem is that I am able to correctly get the String in the text views but not the state of the toggle button. It always comes out to be the default state that is ON in this case. Any help with this will be very nice :(