Question

ProgressTask.java

 public class ProgressTask extends AsyncTask<String, Void, Boolean>{
 private ProgressDialog dialog;
 private ListActivity activity;
 private Context context;

 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="code";
 private static final String OPNAME="name";

 public ProgressTask(ListActivity activity) {
     this.activity = activity;
     context = activity;
     dialog = new ProgressDialog(context);
 }

 protected void onPreExecute() {
     this.dialog.setMessage("Progress start");
     this.dialog.show();
 }

@Override
protected void onPostExecute(final Boolean success) {
    if (dialog.isShowing()) {
        dialog.dismiss();
    }
    ListAdapter adapter = new SimpleAdapter(context, jsonlist,
            R.layout.list_item, new String[] { OPCODE, OPNAME}, new int[] {
                    R.id.opcode, R.id.opname});

    activity.setListAdapter(adapter);
     lv = activity.getListView();

}

protected Boolean doInBackground(final String... args) {
    JSONParser jParser = new JSONParser();
    JSONArray json = jParser.getJSONFromUrl(url);

    for (int i = 0; i < json.length(); i++)
    {
        try {
                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(JSONException e)
        {
            e.printStackTrace();
        }
    }
    return null;
}

its showing error in the list_item, R.id.opcode , R.id.opname should i create a new XML file and write the code there or else will generate dynamically my present layout file is as show below can you guys help me out

<RelativeLayout>
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

  <ListView
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"/>
<RealativeLayout>

Not Able to Understand.

Was it helpful?

Solution

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

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top