I have an activity which has the following variable
static CharSequence[] categories;
I’m receiving data using JSON
try
{
Thread cThread = new Thread(new getStringContent("http://192.168.0.101/curltest/getcategories.php"));
cThread.start();
}
catch(Exception ex)
{
Log.i("YGo", ex.toString());
}
public class getStringContent implements Runnable {
String suri;
public getStringContent(String uri)
{
suri = uri;
}
@Override
public void run() {
try {
JSONArray jsonArray = new JSONArray(getPageContent(suri));
for (int i = 0; i < jsonArray.length(); i++) {
JSONArray jsonSArray = jsonArray.getJSONArray(i);
categories = new CharSequence[jsonArray.length()];
categories[i] = jsonSArray.getString(0);
Log.i("YGo", categories[i].toString());
}
} catch (JSONException e) {
// TODO Auto-generated catch block
Log.e("YGo", e.toString());
}
dialog.dismiss();
}
}
In the GUI thread if I click on the button, I would like to display the received data:
for(int i=0; i<categories.length; i++)
{
Log.i("Ygo", String.valueOf(categories[i]));
Log.i("Ygo", String.valueOf(selectedData[i]));
}
AlertDialog.Builder builder = new AlertDialog.Builder(PreferencesClass.this);
builder.setTitle("Pick categories");
builder.setMultiChoiceItems(categories, selectedData, onClickCatChooser);
builder.setPositiveButton("Ok", clickedOnClickCatOk);
AlertDialog alert = builder.create();
alert.show();
but in the categories variable I have null,null,Data3, instead of Data1 and Data2 I have only null. Why?
The line
should be outside the
forloop. You reset the data with each iteration in the loop, so in the end you only have data left from the last operation.