我正在尝试从服务器收到的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);
Tablelist在WriteJSONArray中正确填充。然后控制从gridview.setAdapter(new OpenTableAdapter(this));
转到适配器类。但它没有执行任何东西。我已经设置断点来查看实际的流量,但它只是来到下面的方法,之后没有执行任何操作。跟踪中没有错误,它没有执行任何操作。
public OpenTableAdapter(Context c) {
// TODO Auto-generated constructor stub
mContext = c;
}
我认为这是因为来自异步任务'Post Execute'方法时传递的上下文不正确。我试过传递上下文,但仍然是同样的问题。请指教。感谢。
答案 0 :(得分:0)
您尚未初始化GridView
GridView gridView = (GridView) findViewById(R.id.gridview);
您已编辑过您的帖子,并说您已初始化gridview,这不是问题。
在BaseAdapter类的构造函数中初始化mInflater。同时将hashmap传递给construcotr并在getview中使用它。
你在getView中做错了什么openInstance = new OpenTableActivity()
是错误的。您可以致电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;
}
答案 1 :(得分:0)
替换
gridview.setAdapter(new OpenTableAdapter(this));
使用
gridview.setAdapter(new OpenTableAdapter(OpenTableActivity.this));