我正在尝试从服务器接收到的 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;
}
我相信这是由于来自异步任务“后执行”方法时传递的上下文不正确。我尝试通过上下文但仍然是同样的问题。请指教。谢谢。