有一些列表适配器及其参数的麻烦?

时间:2014-02-11 07:38:21

标签: android xml android-layout listactivity listadapter

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;
}

list_itemR.id.opcodeR.id.opname中显示错误我应该创建一个新的XML文件并在那里编写代码,否则将动态生成我现在的布局文件如下所示你能帮助我吗

<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>

无法理解。

1 个答案:

答案 0 :(得分:2)

should i create a new XML file and write the code there or else will generate dynamicaly my present layout file

查看构造函数

SimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)

您需要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>

编辑:

我认为你首先没有ListActivity

其次你的json是

[ // is a json array noder
{  // json object node 
    "operatorCode": "AC",
    "operatorName": "Aircel"
},

但你有

 private static final String OPCODE="code"; // should be operatorCOde
 private static final String OPNAME="name"; // operatorName

键与列名称

不匹配

第三,你的json数组顶部没有json对象。

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();
        }

    }

}

对齐

enter image description here