J'essaye de faire une vue de liste dans laquelle chaque article a deux choses, une description dans la vue de texte et un bouton bascule. Les détails dans la vue de texte sont chargés dynamiquement par la tâche asynchrone. À la fin de la liste, j'ai inséré un bouton par programmation. Quand ce bouton est cliqué, j'ai besoin d'obtenir les données dans toutes les textviews et les états de tous les ToggleButtons. Voici le code que je l'ai utilisé:Obtenir l'état du bouton bascule dans Android
fichier XML
<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();
}
});
}
}
}
Mais, le problème est que je suis en mesure d'obtenir correctement la chaîne dans la vues de texte mais pas l'état du bouton bascule. Il apparaît toujours comme l'état par défaut qui est activé dans ce cas. Toute aide avec ce sera très agréable :(
Pourquoi ne pas définir un drapeau séparé qui change chaque fois que l'état du bouton change? – durbnpoisn
En fait je ne sais pas Combien de boutons bascule apparaîtra car cela dépend totalement du nombre d'entrées que je reçois de l'objet JSON –
@durbnpoisn des pointeurs? –