列表视图中的Json数据在基本适配器类中返回null

时间:2015-03-25 07:42:54

标签: java android json android-listview actionbarsherlock

大家好我试图在listview中显示json数据列表我的类已经扩展,SherlockFragment成功获取数据但无法将数据附加到列表中。为了显示数据,我创建了一个用baseadapter类扩展的类。我发现View组的值为零

任何人都可以提前帮助解决问题

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);
    }

}
}

我的Adapter类代码是

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;
}
}

3 个答案:

答案 0 :(得分: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);
}

希望它有所帮助!!

答案 1 :(得分: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));

在create method list =(ListView)rootView.findViewById(R.id.listView1)中添加这两行;         ListViewAdapter la = new ListViewAdapter(getSherlockActivity()。getBaseContext(),contactList);

答案 2 :(得分:0)

在你的getView()方法中,而不是这个 -

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

写下这个 -

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

因为您的ArrayList提供了Hashmap对象,并且您将从该Hashmap对象获取数据。