在追加到列表视图时返回基本适配器类中的空值

时间:2015-03-25 12:45:21

标签: android actionbarsherlock baseadapter android-json-rpc

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

的值返回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));
    }

}}

我的Base适配器类就在这里......

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

}

2 个答案:

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

答案 1 :(得分:0)

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