我当前的项目使用了很多对话框。例如,我有一个" Group"列出用户的活动。单击用户将弹出一个显示其详细信息的对话框(在本例中为历史记录)。它默认显示最后访问的5个地方,可以选择查看完整的历史记录,这会显示第三个对话框以及故障开始的位置。我使用volley和JSON来获取listarray,但是我无法获取上下文传递。
public class HistoryQView extends Dialog implements
android.view.View.OnClickListener, OnItemClickListener{
public Activity c;
public Dialog d;
public JSONHistory results;
public ListView members;
public HistoryQView(Activity a, String viewed) {
super(a);
// TODO Auto-generated constructor stub
this.c = a;
this.viewed = viewed;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.fragment_qview);
//blah, blah........
getData();
}
public void getData() {
//blah, blah.......
RequestQueue queue = VolleySingleton.getInstance(getContext()).getRequestQueue();
JsonObjectRequest jsObjRequest = new JsonObjectRequest(
Request.Method.GET, full_url, null,
new Response.Listener<JSONObject>()
@Override
public void onResponse(JSONObject response) {
String example = response.toString();
System.out.println(example);
try {
Gson googleJson = new Gson();
JSONHistory rh = googleJson.fromJson(example,JSONHistory.class);
for (JSONHistory e : rh.getJSONHistory()) {
eventlist.add(e);
}
final ListView listview = (ListView) findViewById(R.id.qviewListView);
final HistoryAdapter adapter = new HistoryAdapter(HistoryQView.this, R.layout.list_history, eventlist);
listview.setAdapter(adapter);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent,final View view, int position, long id) {
final JSONHistory thisevent = (JSONHistory) parent.getItemAtPosition(position);
final String thiseid = thisevent.getEventInfo("eid");
System.out.println(thiseid);
//eventlist.remove(item);
//adapter.notifyDataSetChanged();
}
});
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// TODO Auto-generated method stub
}
});
queue.add(jsObjRequest);
}
我99%确定问题是上下文没有传递给HistoryAdapter(可能是因为我没有正确使用它)。我真的不确定如何解决这个问题。
class HistoryAdapter extends ArrayAdapter<JSONHistory> {
private ArrayList<JSONHistory> items;
public HistoryAdapter(Context context, int textViewResourceId,
ArrayList<JSONHistory> items) {
super(context, textViewResourceId, items);
System.out.println(context);
this.items = items;
}
我已尝试传递HistoryQView.this,HistoryQView.this.getActivity(),HistoryQView.this.getOwnerActivity()(这消除了编译器错误,但传递了NULL)。在我走的时候,我几乎要学习这一点,而且我对如何修复的想法很感兴趣并且希望有人可以提供帮助。我很感激。我试图删除那些不适用的代码,让我知道如果我太过分了,你需要看一些......
答案 0 :(得分:1)
这是初始化适配器的地方:
final HistoryAdapter adapter = new HistoryAdapter(HistoryQView.this, R.layout.list_history, eventlist);
将对话框本身作为第一个参数传递,您需要一个Context。您巧妙地将Activity作为字段存储在构造函数中:
public HistoryQView(Activity a, String viewed) {
super(a);
// TODO Auto-generated constructor stub
this.c = a; // stores the activity!
this.viewed = viewed;
}
所以你需要做的就是改变第一行使用该字段:
final HistoryAdapter adapter = new HistoryAdapter(HistoryQView.this.c, R.layout.list_history, eventlist);