2015-03-25 103 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