2

我想在它之前将我的 JSON 显示到 gridview 中。我将我的 JSON 显示到 ListView 中,它可以工作。但是在 BaseAdapter 中,我不知道如何将我放入 ArrayList 的 JSON 发送到 Base Adapter

所以这是我的源代码:

活动 :

    public class MainActivity extends ListActivity {
    List AgenList = new ArrayList();
        boolean boolStatusKoneksi=true;
        private ProgressDialog Dialog;
        protected Context applicationContext;
        @Override
        public void onCreate(Bundle savedInstanceState){
            super.onCreate(savedInstanceState);     
            setContentView(R.layout.activity_main);
            new AgenAsyncTask().execute();
            GridView gridview = (GridView) findViewById(R.id.gridview);
            gridview.setAdapter(new AgenAdapter(this));
        }


    public class AgenAsyncTask extends AsyncTask<String, String, String>
    {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            Dialog = new ProgressDialog(MainActivity.this);
            Dialog.setMessage("Mohon Tunggu sebentar...");
            Dialog.setIndeterminate(false);
            Dialog.setCancelable(true);
            Dialog.show();
        }


        protected String doInBackground(String... args) {

                 String url = ("http:10.10.2/selectAgent.htm");

                  try{
                      JSONParser j=new JSONParser();
                        JSONArray jsonArray = j.takeJson(url);;

                      for(int i =0; i<jsonArray.length(); i++){
                          JSONObject c  = jsonArray.getJSONObject(i);


                            HashMap<String, String> map = new HashMap<String, String>();
                            if (c.has("atasan")) 
                                map.put("atasan", c.get("atasan").toString());
                            if (c.has("nama_agen")) 
                                map.put("nama_agen", c.get("nama_agen").toString());
                            if (c.has("kode_agen")) 
                                map.put("kode_agen", c.get("kode_agen").toString());
                            if (c.has("no_aaji")) 
                                map.put("no_aaji", c.get("no_aaji").toString());
                            if (c.has("jenis")) 
                                map.put("jenis", c.get("jenis").toString());
                              AgenList.add(map);
                         }
                    } catch (JSONException e) {
                        e.printStackTrace();
                        }
                  return null;
                    }


        @Override
        protected void onPostExecute(String file_url) {

       }
   }

这是我的 BaseAdapter :

    public class AgenAdapter extends BaseAdapter {

        public AgenAdapter(MainActivity mainActivity) {

        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater layout = getLayoutInflater();
            View view= layout.inflate(R.layout.list_item,parent,false);

            TextView ATASAN = (TextView) findViewById(R.id.atasan);
            TextView NAMA_AGEN= (TextView) findViewById(R.id.nama_agen);
            TextView KODE_AGEN= (TextView) findViewById(R.id.kode_agen);
            TextView NO_AAJI= (TextView) findViewById(R.id.no_aaji);
            TextView JENIS= (TextView) findViewById(R.id.jenis);
            return view;
        }
    }
}

如您所见,我将 ArrayList 命名为 = AgenList,但我没有将它放入我的 BaseAdapter。

4

3 回答 3

3

制作一个参数化的构造函数并将您的数组列表传递给它的参数。所以你的课会像

public class AgenAdapter extends BaseAdapter {
List<yourObj> list;
Activity a;
    public AgenAdapter(Activity activity,List<yourObj> list) {
       this.a=activity;
       this.list=list;

    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return list.size();///////return size of list
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;///// dont return null here
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;/////////return position as itemID
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater layout = getLayoutInflater();
        View view= layout.inflate(R.layout.list_item,parent,false);

        TextView ATASAN = (TextView) findViewById(R.id.atasan);
        TextView NAMA_AGEN= (TextView) findViewById(R.id.nama_agen);
        TextView KODE_AGEN= (TextView) findViewById(R.id.kode_agen);
        TextView NO_AAJI= (TextView) findViewById(R.id.no_aaji);
        TextView JENIS= (TextView) findViewById(R.id.jenis);
        return view;
    }
}
于 2013-04-04T04:31:25.200 回答
1

试试这个。

gridview.setAdapter(new AgenAdapter(this), AgenList);

并在你的 onPostExecute() 中使用这一行而不是 onCreate()

像这样:

    @Override
    protected void onPostExecute(String file_url) {
     gridview.setAdapter(new AgenAdapter(this), AgenList);

    }
于 2013-04-04T04:56:39.520 回答
1

看看ArrayAdapter。它包含基于ArrayList.

  1. 使您的适配器成为 的子类ArrayAdapter,并提供参数化类型。在这种情况下,您的Map<String, String>

    public class ResultAdapter extends ArrayAdapter<Map<String,String>> {
    
        private int mResource;
    
        /**
        * @param context
        * @param resource
        */
        public ResultAdapter(Context context, int resource, ArrayList<Map<String,String>> dataList) {
            super(context, resource, dataList);
            this.mResource = resource;
        }
    
        ...
    
    }
    
  2. 覆盖 ArrayAdapter 的getView方法以用于设置提供给视图的数据。

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view;
        // If the view is already inflated, reuse it.
        // Else inflate the view
        if (convertView != null) {
            view = convertView;
        } else {
            //inflate view
            final LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(mResource, parent, false);
        }
    
        TextView ATASAN = (TextView) findViewById(R.id.atasan);
        ...
    
    
        //Get data here
        Map<String, String> item = getItem(position);
    
        //Set data to the View's e.g.
        ATASAN.setText(item.get("ATASAN"));
        ...
    
        return view;
    }
    
于 2013-10-07T09:22:36.387 回答