My goal is to put the fetched JSON
in my ArrayList<Object>
to be able to display my ListView
I'm searching for syntax how to do it but I'm confuse on how to do it correctly.
here's my code
public class MyListActivity extends ActivityWithTabBar implements
OnGestureListener, OnTouchListener {
private String searchText = null;
private ArrayList<ListItems> itemsArray = null;
private MyArrayAdapter myArrayAdapter;
private ListView lv = null;
public void onCreate(Bundle savedInstanceState) {
searchText = intent.getStringExtra(Constants.SEARCH).trim();
performSearch();
}
private void performSearch() {
searchText = editTextSearch.getText().toString();
loadDataViaWS();
}
private void loadDataViaWS(){
itemsArray = new ArrayList<ListItems>();
lv = (ListView) findViewById(R.id.listViewSearch);
this.myArrayAdapter = new MyArrayAdapter(this,
R.layout.listview_row_2rows_subtitle, itemsArray);
lv.setAdapter(this.myArrayAdapter);
new GetValues().execute();
}
Here is my AsyncTask
class GetValues extends AsyncTask<Void, Void, ArrayList<ListItems>> {
private final String URL_STRING = url;
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(URL_STRING);
String jsonData = null;
@Override
protected ArrayList<ListItems> doInBackground(Void... params){
try {
HttpResponse response = httpclient.execute(httpget);
jsonData = EntityUtils.toString(response.getEntity());
Log.d(TAG, String.format("JSON:", jsonData));
} catch (IOException e) {
e.printStackTrace();
}
itemsArray = new ArrayList<ListItems>();
JSONObject object = null;
try {
object = new JSONObject(jsonData);
JSONArray jArray = object.getJSONArray("DATA");
for(int i = 0; i < jArray.length(); i++){
JSONObject jObj = jArray.getJSONObject(i);
String Id = jObj.getString("id");
String Name = jObj.getString("name");
String Description = jObj.getString("description");
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
As you can see in my doInBackground
inside the for
loop how will I put the JSONObject
I fetched in my itemsArray
where itemsArray
is equal to ArrayList<ListItems>
?
Edit
Here is my ListItems
public class ListItems implements Serializable {
private int id;
private String name;
private String description;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}