should i create a new XML file and write the code there or else will generate dynamicaly my present layout file
Look at the constructor
SimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)
You need to have list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/opcode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="49dp"
android:layout_marginTop="51dp"
android:text="TextView" />
<TextView
android:id="@+id/opname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textView1"
android:layout_alignParentRight="true"
android:layout_marginRight="72dp"
android:text="TextView" />
</RelativeLayout>
Edit:
I think you do not have a ListActivity
in the first place.
Secondly your json is
[ // is a json array noder
{ // json object node
"operatorCode": "AC",
"operatorName": "Aircel"
},
But you have
private static final String OPCODE="code"; // should be operatorCOde
private static final String OPNAME="name"; // operatorName
Keys are not matching the column names
Thirdly you do not have a json object at the top its a json array.
public class MainActivity extends ListActivity
{
private ProgressDialog dialog;
ArrayList<HashMap<String, String>> jsonlist = new ArrayList<HashMap<String, String>>();
ListView lv ;
private static String url = "http://api.cartperk.com/v1/supportedoperator";
private static final String OPCODE="operatorCode";
private static final String OPNAME="operatorName";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
dialog = new ProgressDialog(this);
dialog.setMessage("Loading...");
new TheTask().execute();
}
class TheTask extends AsyncTask<Void,Void,Void>
{
@Override
protected Void doInBackground(Void... params) {
try
{
HttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
HttpGet request = new HttpGet(url);
HttpResponse response = httpclient.execute(request);
HttpEntity resEntity = response.getEntity();
String res = EntityUtils.toString(resEntity);
JSONArray json = new JSONArray(res);
for(int i=0;i<json.length();i++)
{
JSONObject c = json.getJSONObject(i);
String opcode = c.getString(OPCODE);
String opname = c.getString(OPNAME);
HashMap<String, String> map = new HashMap<String, String>();
map.put(OPCODE,opcode);
map.put(OPNAME,opname);
jsonlist.add(map);
}
}catch(Exception e)
{
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
dialog.dismiss();
ListAdapter adapter = new SimpleAdapter(MainActivity.this, jsonlist,
R.layout.list_item, new String[] { OPCODE, OPNAME}, new int[] {
R.id.opcode, R.id.opname});
setListAdapter(adapter);
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
dialog.show();
}
}
}
Snap