0

我正在尝试从服务器接收到的 JSON 填充我的网格视图。然后我需要从获得的结果中填充 gridview。下面是填充网格视图的活动文件

public class OpenTableActivity extends Activity {

    String serUri, method;
    OpenTableAdapter op;

    ArrayList<HashMap<String, String>> tableList = new ArrayList<HashMap<String, String>>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.open_table);

        LinearLayout layout = (LinearLayout) findViewById(R.id.opentableLinear);
        layout.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent ev) {
                hideKeyboard(view);
                return false;
            }
        });
        getTables();
            GridView gridview = (GridView) findViewById(R.id.grid);
        gridview.setAdapter(new OpenTableAdapter(this));
        Button btn = (Button) findViewById(R.id.table_home_btn);
        btn.setOnClickListener(homeBtn);
    }

    public boolean getTables() {

        serUri = "tables.json";

        method = "get";
        WebServiceAsyncTask webServiceTask = new WebServiceAsyncTask(OpenTableActivity.this);
        webServiceTask.execute(serUri, method, this);

        return true;

    }

    public void WriteJsonArray(final JSONArray result, Context contextInstance) {


        try {

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


                String tabLabel = c.getString("tablabel");
                String tabStatus = c.getString("tabstatus");

                HashMap<String, String> map = new HashMap<String, String>();


                map.put("table_name", tabLabel);
                map.put("table_status", tabStatus);

                tableList.add(map);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

下面是网格视图的适配器类

public class OpenTableAdapter extends BaseAdapter {
    private Context mContext;
    String orderNumber;
    OpenTableActivity openInstance;
    String tableLabel;
    String tableStatus;
    //ArrayList<HashMap<String, String>> tablist = null;

    public OpenTableAdapter(Context c) {
        // TODO Auto-generated constructor stub
        mContext = c;
        //tablist = tablelist;
    }


    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        openInstance = new OpenTableActivity();
        return openInstance.tableList.size();
    }

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = null;
        if (convertView == null) {
            LayoutInflater li = (LayoutInflater) mContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = li.inflate(R.layout.button, null);
        } else {
            view = convertView;
        }
        Button btn = (Button) view.findViewById(R.id.button);
        openInstance = new OpenTableActivity();

        for (HashMap<String, String> map : openInstance.tableList)
            for (java.util.Map.Entry<String, String> mapEntry : map.entrySet()) {
                String key = mapEntry.getKey();
                String value = mapEntry.getValue();
                btn.setText(key);

表格列表在 WriteJSONArray 中正确填充。然后控制从 转到适配器类 gridview.setAdapter(new OpenTableAdapter(this));。但它在那里什么也不执行。我已经设置了断点来查看实际流程,但它只涉及到下面的方法,此后没有执行任何操作。跟踪没有错误,也没有执行任何操作。

public OpenTableAdapter(Context c) {
    // TODO Auto-generated constructor stub
    mContext = c;

}

我相信这是由于来自异步任务“后执行”方法时传递的上下文不正确。我尝试通过上下文但仍然是同样的问题。请指教。谢谢。

4

2 回答 2

0

你还没有初始化GridView

GridView gridView = (GridView) findViewById(R.id.gridview);

您已经编辑了您的帖子并说您已经初始化了 gridview,所以这不是问题。

在 BaseAdapter 类的构造函数中初始化 mInflater。还将哈希图传递给构造函数并在 getview 中使用相同的值。

您在 getView 中所做的错误openInstance = new OpenTableActivity()是错误的。您可以通过调用来启动 Activity startActivity

   LayoutInflater mInflater;  
   public OpenTableAdapter(Context c) {
    mContext = c;
    mInflater = LayoutInflater.from(c); 
    tablist = tablelist;
    }

在你的getView

         ViewHolder holder; 

             if (convertView == null) { 
                 convertView = mInflater.inflate(R.layout.button, 
                         parent, false);
                 holder = new ViewHolder();  
                 holder.btn = (Button) convertView.findViewById(R.id.button);   
                convertView.setTag(holder); 
            } else { 
                holder = (ViewHolder) convertView.getTag(); 
            } 

// use tablelist data to set the text to button using the position as index      

                 HashMap<String, String>> map = tablist.get(position)
                 // use the map to set the text to the button now. 
                    holder.btn.setText(key);
            return convertView; 
    }

使用 ViewHolder。http://developer.android.com/training/improving-layouts/smooth-scrolling.html

   static class ViewHolder
   {
     Button btn;
   } 
于 2013-08-16T04:41:20.067 回答
0

代替

gridview.setAdapter(new OpenTableAdapter(this));

 gridview.setAdapter(new OpenTableAdapter(OpenTableActivity.this));
于 2013-08-16T04:53:13.557 回答