2015-03-25 79 views
0

大家好我正在嘗試在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; 
} 
} 
+0

在OnCreateView中聲明ListView對象。 list =(ListView)rootView.findViewById(R.id.listView1); – Shadow 2015-03-25 07:49:52

+0

嗨陰影,我檢查與沒有改變相同的問題返回 – 2015-03-25 07:59:59

+0

'我發現價值越來越零,這值? – Xcihnegn 2015-03-25 08:55:47

回答

0

這裏是你的問題在適配器 ..

@Override 
public Object getItem(int position) { 
// TODO Auto-generated method stub 
return null; //this line 
} 

從你的arrayList中返回對象就像下面這樣的位置!

@Override 
public Object getItem(int position) { 
// TODO Auto-generated method stub 
return data.get(position); 
} 

希望它有幫助!

+0

你能說清楚我該如何聲明通過代碼 – 2015-03-25 07:49:57

+0

看到更新的答案,並接受和投票,如果它是有益的! @AdityaHariKishan – Sjd 2015-03-25 07:54:13

+0

感謝您的支持仍然返回空值 – 2015-03-25 07:58:48

0
First thing is that you have just declared your ListView not initialized. 

ListView list; 
So you need to initialize it on main() method. Like, 

list = (ListView)findViewById(R.id.yourlistviewname); 
and also call your main() method before your AsyncTask been executed. 

Another thing is that just set your adapter class out of your for loop. 

list.setAdapter(new ApplistAdapter(getApplicationContext(), 
        mylist)); 

添加這兩條線在上創建方法列表=(ListView中)rootView.findViewById(R.id.listView1); ListViewAdapter la = new ListViewAdapter(getSherlockActivity()。getBaseContext(),contactList);

+0

添加這兩行在創建方法列表= (ListView的)rootView.findViewById(R.id.listView1); ListViewAdapter la = new ListViewAdapter(getSherlockActivity()。getBaseContext(),contactList); – 2015-03-25 08:25:05

+0

嗨Alka,我已經在main()方法中聲明瞭listview,並在asynctask中修改了代碼,但沒有更改 – 2015-03-25 08:28:46

+0

查看我的更新Ans @AdityaHariKishan – 2015-03-25 08:30:05

0

,而不是這個 -

textView1_lv_123.setText(resultp.get(AboutMeFragment.getQuestionsStr)); 

在你getView()方法,寫這個 -

textView1_lv_123.setText(resultp.get(position).get("getQuestionsStr")); 

,因爲你的ArrayList是給HashMap對象,並從HashMap對象,你會得到你的數據。

+0

嗨Amit,我做了上面的修改,但結果爲空 – 2015-03-25 09:39:40

+0

在評論中,你寫了你的值「resultp.get(AboutMeFragment .getQuestionsStr)「爲空。你仍然得到那個「空」的價值? – 2015-03-25 09:59:27

+0

Amit,我嘗試了多種方式來執行數據能夠傳遞是正確的,但是,當我將它追加到textview它變爲空 – 2015-03-25 10:05:53