-2

大家好,我是 android 新手,正在尝试在 listview 中显示 json 数据列表,我的类扩展了 SherlockFragment 成功获取数据但无法将数据附加到列表中。为了显示数据,我创建了一个使用 baseadapter 类扩展的类。我发现视图组中的值返回 null

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

public class AboutMeFragment extends Fragment { 

ListViewAdapter la;
String add_users;
View rootView;
ArrayList<HashMap<String, String>> contactList;
static String idStr,getMaxAnsLengthStr,userIdStr,userRankStr,de;

static String getQuestionsStr = "GetQuestion";

ListView list;



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    rootView = inflater.inflate(R.layout.fragment_aboutme,
            container, false);

    la = new ListViewAdapter(getActivity(), contactList);

    list = (ListView)rootView.findViewById(R.id.listView1);
    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;
                System.out.println("The Length of JSON Array is..."+jsonArray.length());
                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);

        //  ListViewAdapter la = new ListViewAdapter(getSherlockActivity().getBaseContext(),contactList);
        //  ListViewAdapter la = new ListViewAdapter(getActivity().getBaseContext(),contactList);

        //list.setAdapter(la);


        list.setAdapter(new ListViewAdapter(getActivity().getBaseContext(),contactList));
    }

}}

我的基础适配器类如下......

public class ListViewAdapter extends BaseAdapter{

int tempI =0;
int tempII = 7;

Context context;
ArrayList<HashMap<String, String>> data;
LayoutInflater inflater;
HashMap<String, String> resultp = new HashMap<String, String>();

public ListViewAdapter(Context baseContext,
        ArrayList<HashMap<String, String>> contactList) {
    // TODO Auto-generated constructor stub

    this.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
    System.out.println("The Item Value is..."+data.get(position));
    return 0;
}

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


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

    // facing issue from here, data is retuning null unable to append the value to text view 
    String s1 = resultp.get("GetQuestion");
    System.out.println("The demo String is....1234 "+s1);

    textView1_lv_123.setText(resultp.get("GetQuestion"));

     // if I am printing only resultp value it is returning the value/result which is an last value of array 

    System.out.println("The Result value is  "+resultp);


    return itemView;
}

}

4

2 回答 2

0

嗨 Aditya 检查 AboutmeFragment 页面中的字符串声明我希望它们与您的代码重叠

于 2015-03-26T06:56:21.517 回答
0

您应该在创建 ListAdapter 时进行更改,您应该在 AsyncTask onPostExecute 中创建。尝试这个:

AboutMeFragment.class:

    public class AboutMeFragment extends Fragment { 

    ListViewAdapter la;
    String add_users;
    View rootView;
    ArrayList<HashMap<String, String>> contactList;
    static String idStr,getMaxAnsLengthStr,userIdStr,userRankStr,de;

    static String getQuestionsStr = "GetQuestion";

    ListView list;



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_aboutme,
                container, false);



        list = (ListView)rootView.findViewById(R.id.listView1);
        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;
                    System.out.println("The Length of JSON Array is..."+jsonArray.length());
                    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);

            //  ListViewAdapter la = new ListViewAdapter(getSherlockActivity().getBaseContext(),contactList);
            //  ListViewAdapter la = new ListViewAdapter(getActivity().getBaseContext(),contactList);

            //list.setAdapter(la);
            if(contactList!=null && contactList.size()>0{

                 la =new ListViewAdapter(getActivity().getBaseContext(),contactList));
                 list.setAdapter(la);

            }else{
              // handle no data Example:show toast "contactList have no data"
            }

}}

您应该在适配器中更改 getItem() 方法。尝试这个:

BaseAdapter.class:

public class ListViewAdapter extends BaseAdapter{

int tempI =0;
int tempII = 7;

Context context;
ArrayList<HashMap<String, String>> data;
LayoutInflater inflater;
HashMap<String, String> resultp = new HashMap<String, String>();

public ListViewAdapter(Context baseContext,
        ArrayList<HashMap<String, String>> contactList) {
    // TODO Auto-generated constructor stub

    this.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
    System.out.println("The Item Value is..."+data.get(position));
    return data.get(position);
}

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


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

    // facing issue from here, data is retuning null unable to append the value to text view 
    String s1 = resultp.get("GetQuestion");
    System.out.println("The demo String is....1234 "+s1);

    textView1_lv_123.setText(resultp.get("GetQuestion"));

     // if I am printing only resultp value it is returning the value/result which is an last value of array 

    System.out.println("The Result value is  "+resultp);


    return itemView;
}
于 2015-03-25T13:18:27.160 回答