【问题标题】:Having some Trouble with List adapter and its arrguments?Listadapter 及其论点有问题吗?
【发布时间】:2014-02-11 07:38:21
【问题描述】:

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>

无法理解。

【问题讨论】:

  • 如果我想创建一个 XML 文件,你能给我一个想法
  • 如果可能,请发布您的list_item xml 和 logcat。
  • @user3263528 你在 re/layout 文件夹中有一个名为 list_item.xml 的 xml 吗??
  • 不,我没有 list_item.xml
  • 我问如何在上面显示的java的基础上创建一个XML

标签: android xml android-layout listactivity listadapter


【解决方案1】:
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();
        }

    }

}

捕捉

【讨论】:

  • 非常感谢,帮了大忙
  • @AbhisheK6006 问题未解决??你为什么不接受??
  • 不,问题并没有解决它仍然没有显示 JSON 数据
  • @AbhisheK6006 然后发布一些更相关的信息来解决问题。您发布的内容无法解决。并在这里发布
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-26
  • 1970-01-01
  • 1970-01-01
  • 2013-04-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多