大家好我试图在listview中显示json数据列表我的类已经扩展,SherlockFragment成功获取数据但无法将数据保存在列表中。为了显示数据,我创建了一个用baseadapter类扩展的类。它显示在这里是我的以下代码,任何人都可以对此有所了解。
提前致谢
public class AboutMeFragment extends SherlockFragment {
String add_users;
View rootView;
ArrayList<HashMap<String, String>> contactList;
String getQuestionsStr,idStr,getMaxAnsLengthStr,userIdStr,userRankStr;
ListView list;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_aboutme,
container, false);
contactList = new ArrayList<HashMap<String, String>>();
new CheckLoginAboutmen().execute();
return rootView;
}
private class CheckLoginAboutmen extends AsyncTask<Void,Void,Void>{
String json;
String outputData = null;
@Override
protected Void doInBackground(Void... args) {
// TODO Auto-generated method stub
List<NameValuePair> params = new ArrayList<NameValuePair>(1);
// params.add(new BasicNameValuePair("LoginName", loginStr));
params.add(new BasicNameValuePair("UserID", "195"));
ServiceHandler sh = new ServiceHandler();
add_users = getString(R.string.about_me);
json = sh.makeServiceCall(add_users, ServiceHandler.POST,params);
Log.d("Create Prediction Request: ", "> " + json);
System.out.println("The returned JSON Value is..."+json);
try{
JSONArray jsonArray = new JSONArray(json);
if(json != null){
HashMap<String, String> contact = null;
for(int i =0;i<jsonArray.length();i++){
contact = new HashMap<String, String>();
JSONObject c = jsonArray.getJSONObject(i);
getQuestionsStr = c.getString("GetQuestion");
idStr = c.getString("_id");
getMaxAnsLengthStr = c.getString("MaxAnswerLength");
userIdStr = c.getString("UserId");
userRankStr = c.getString("UserRank");
System.out.println("The Fetched Id is...."+idStr+"\n"+getQuestionsStr+"\n"+getMaxAnsLengthStr+"\n"+userIdStr+"\n"+userRankStr);
contact.put("getQuestionsStr", getQuestionsStr);
}
contactList.add(contact);
}
}catch(Exception e){
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result)
{
super.onPostExecute(result);
list = (ListView)rootView.findViewById(R.id.listView1);
// ListViewAdapter la = new ListViewAdapter(getActivity().getBaseContext(),contactList);
ListViewAdapter la = new ListViewAdapter(getActivity(),contactList);
System.out.println(getActivity()+"\n"+contactList);
list.setAdapter(la);
}
}
}
这是我的ListViewAdapter类代码
public class ListViewAdapter extends BaseAdapter{
Context context;
ArrayList<HashMap<String, String>> data;
String question;
HashMap<String, String> resultp = new HashMap<String, String>();
/* public ListViewAdapter(Context baseContext,
ArrayList<HashMap<String, String>> contactList) {
// TODO Auto-generated constructor stub
this.context = context;
data = contactList;
}*/
SherlockFragmentActivity sfa;
FragmentActivity fa;
/*public ListViewAdapter(SherlockFragmentActivity sherlockActivity,
ArrayList<HashMap<String, String>> contactList) {
// TODO Auto-generated constructor stub
this.sfa = sherlockActivity;
data = contactList;
}*/
public ListViewAdapter(FragmentActivity activity,
ArrayList<HashMap<String, String>> contactList) {
// TODO Auto-generated constructor stub
this.fa = activity;
data = contactList;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return data.size();
}
@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) {
// TODO Auto-generated method stub
View itemView = null;
AboutMeFragment amf = new AboutMeFragment();
LayoutInflater inflater = (LayoutInflater)fa.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// LayoutInflater inflater = (LayoutInflater)sfa.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
itemView = inflater.inflate(R.layout.list_item_view, parent,false);
resultp.get(position);
TextView textView1_lv_123 = (TextView)itemView.findViewById(R.id.textView1_lv_123);
textView1_lv_123.setText(resultp.get(amf.getQuestionsStr));
return itemView;
}
}