大家好我正在嘗試在listview中顯示json數據列表我的類用SherlockFragment擴展獲取數據,但無法將數據附加到列表中。爲了顯示數據,我創建了一個以baseadapter類擴展的類。我發現,價值在視圖組獲得零在列表視圖中返回null的Json數據在基類適配器類中返回null
可以在推動這一問題由於固定任何一個幫助
public class AboutMeFragment extends SherlockFragment {
String add_users;
View rootView;
ArrayList<HashMap<String, String>> contactList;
static String getQuestionsStr,idStr,getMaxAnsLengthStr,userIdStr,userRankStr,de;
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;
JSONArray jsonArray;
@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 = 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(getSherlockActivity().getBaseContext(),contactList);
// ListViewAdapter la = new ListViewAdapter(getActivity(),contactList);
System.out.println(getActivity()+"\n"+contactList);
list.setAdapter(la);
}
}
}
我的適配器類代碼爲
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
context = baseContext;
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;
TextView textView1_lv_123;
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
itemView = inflater.inflate(R.layout.list_item_view, parent,false);
resultp = data.get(position);
textView1_lv_123 = (TextView)itemView.findViewById(R.id.textView1_lv_123);
System.out.println("The String to be fetched is...."+AboutMeFragment.getQuestionsStr);
textView1_lv_123.setText(resultp.get(AboutMeFragment.getQuestionsStr));
return itemView;
}
}
在OnCreateView中聲明ListView對象。 list =(ListView)rootView.findViewById(R.id.listView1); – Shadow 2015-03-25 07:49:52
嗨陰影,我檢查與沒有改變相同的問題返回 – 2015-03-25 07:59:59
'我發現價值越來越零,這值? – Xcihnegn 2015-03-25 08:55:47