JSON Android ListView
I build this webservice on netbeans, package in.figures.on.mobile; import db.koneksi.dbKoneksi; import java.sql.Statement; import java.sql.ResultSet; import java.util.ArrayList; i
Solution 1:
Ok. Try this bellow code. It is full functional to me. You should implement the HttpRequest in the commented line. Pay atention to that the JSON array is hard-coded.
// the Adapter
public class ListViewAdapter extends BaseAdapter {
private Context context = null;
private List<String> fields = null;
public ListViewAdapter(Context context, JSONArray arr) {
this.context = context;
this.fields = new ArrayList<String>();
for (int i=0; i<arr.length(); ++i) {
try {
fields.add(arr.getJSONObject(i).toString());
} catch (JSONException e) {
e.printStackTrace();
}
}
}
@Override
public int getCount() {
return fields.size();
}
@Override
public Object getItem(int position) {
return fields.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup viewGroup) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.itemlist, null);
TextView txt = (TextView) convertView.findViewById(R.id.ItemList_txt);
txt.setText(fields.get(position));
return convertView;
}
}
// the activity
public class ListViewActivity extends Activity {
public final String result = "[{\"idPrimary_key\":\"21ye21\",\"kategori\":\"FirstCategory\"},{\"idPrimary_key\":\"89oy89\",\"kategori\":\"SecondCategory\"},{\"idPrimary_key\":\"34ew34\",\"kategori\":\"ThirdCategory\"}]";
public final String obj = "{\"kat\":"+result+"}";
private ListViewAdapter adapter = null;
private ListView myList = null;
private JSONArray items = new JSONArray();
final Handler handler = new Handler() {
@Override
public void handleMessage(android.os.Message msg) {
if (msg.what == 0) { // server returned null, try again
loadFields();
} else if(msg.what == 1) { // error in json
// do something to treat it
} else if (msg.what == 2) { // ready to roll the list
adapter = new ListViewAdapter(ListViewActivity.this, items);
myList.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myList = (ListView) findViewById(R.id.Lists_notificationsListview);
loadFields();
}
private void loadFields() {
new Thread() {
@Override
public void run() {
Looper.prepare();
StringBuilder builder = new StringBuilder(obj);
if (builder != null) {
try {
// HERE, you should implement the HTTP request...
items = new JSONObject(obj).getJSONArray("kat");
handler.sendEmptyMessage(2);
} catch (JSONException e) {
handler.sendEmptyMessage(1);
}
} else {
handler.sendEmptyMessage(0);
}
Looper.loop();
}
}.start();
}
And the xml files:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:orientation="vertical"
android:isScrollContainer="true">
<ListView
android:id="@+id/Lists_notificationsListview"
android:layout_width="fill_parent" android:layout_height="match_parent">
</ListView>
</RelativeLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<TextView
android:id="@+id/ItemList_txt"
android:layout_width="fill_parent" android:layout_height="wrap_content"/>
</LinearLayout>
As result, it generates the following view:
Of course, you can customize it to create lists that you want, just parsing the jsons! Hope that I've helped in some way...
Post a Comment for "JSON Android ListView"