Wednesday, April 24, 2013

[android help] JSONArray in a ListView with Android


no. runOnUiThread() is necessary because you want to call setListAdapter.


again. - move these two lines



ListAdapter adapter = new SimpleAdapter(Utenti.this, mylist , R.layout.utentii,new String[] { "name" },new int[] { 1});
setListAdapter(adapter);


right after the for(){} loop in the onClick() method.


if you are sure that your mylist list is populated with data in your json call.


basically something like this as a sample:


replace your xml file contents with the following:



android:id="@android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />


then the onCreate():



@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout. utentii);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();

StrictMode.setThreadPolicy(policy);
lv = getListView();
lv.setTextFilterEnabled(true);

populateListView(); ///populate list first time


//then onClick() event
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View view, int position, long id) {

populateListView();
}
});

}

private void populateListView(){

Thread t = new Thread(new Runnable (){

public void run() {
//Looper.prepare();
JSONObject json =conn("ketut.php");

try {
//Get the element that holds the earthquakes ( JSONArray )
JSONArray ute = json.getJSONArray("success");
final List> mylist =
new ArrayList>();

for(int i=0;i < ute.length();i++){

JSONObject e = ute.getJSONObject(i);
System.out.println(e.getString("user"));

mylist.put("name",e.getString("user"));
}

runOnUiThread(new Runnable() {


public void run() {

ListAdapter adapter = new SimpleAdapter(Utenti.this, mylist , R.layout.utentii,new String[] { "name" },new int[] { 1});
Utenti.this.setListAdapter(adapter);
}

});

} catch (JSONException e) {
System.out.println("4");
e.printStackTrace();
}

}
});
t.start();

}


hope this helps abit.



.

stackoverflow.comm

No comments:

Post a Comment

Google Voice on T-Mobile? [General]

Google Voice on T-Mobile? So I recently switched from a GNex on Verizon to a Moto X DE on T-Mobile. I had always used Google Voice for my v...