android ListView是空的Logcat什么都没显示

时间:2016-03-11 09:39:11

标签: android json listview

我试图在listview中显示json数据但listview是空的logcat显示没有错误,应用程序也没有崩溃我无法找到错误这里是将json数据添加到适配器的方法

 public void setTextToTextView(JSONArray jsonArray) {
            contactAdapter = new ContactAdapter(this,R.layout.row_layout);
            listView.setAdapter(contactAdapter);
                try {
                    int count = 0;
                    String stop;
                    while(count < jsonArray.length())
                    {
                        JSONObject JO = jsonArray.getJSONObject(count);
                        stop = JO.getString("stop");
                        Contacts  contacts = new Contacts(stop);
                        contactAdapter.add(contacts);
                        count++;
                    }
    }
                catch (JSONException e) {
                    e.printStackTrace();
                }
           }

这是Adapter类

public class ContactAdapter extends ArrayAdapter{
    List list = new ArrayList();

    public ContactAdapter(Context context, int resources){
        super(context,resources);
    }

    public void add(Contacts object)
    {
       super.add(object);
        list.add(object);
    }
    @Override
    public int getCount() {return list.size();}

    @Override
    public Object getItem(int position) {return list.get(position);}

    @Override
    public View getView(int position,View convertView,ViewGroup parent)
    {
        View row;
        row = convertView;
        ContactHolder contactHolder;
        if(row == null)
        {
            LayoutInflater layoutInflater = (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = layoutInflater.inflate(R.layout.row_layout,parent,false);
            contactHolder = new ContactHolder();
            contactHolder.tx_stop = (TextView)row.findViewById(R.id.tx_stop);
            row.setTag(contactHolder);
        }
        else
        {
            contactHolder = (ContactHolder)row.getTag();
        }
        Contacts contacts = (Contacts)this.getItem(position);
        contactHolder.tx_stop.setText(contacts.getStop());
        return row;
    }
    static class ContactHolder
    {
        TextView tx_stop;
    }

}

2 个答案:

答案 0 :(得分:1)

致电

   contactAdapter.notifyDataSetChanged();

在你的while循环之后。

答案 1 :(得分:1)

您可以创建ArrayList<Contacts> contactsArray = new ArrayList<Contacts>();,然后在其中推送所有对象,以使用传递的数组设置适配器。您的方法应如下所示:

public void setTextToTextView(JSONArray jsonArray) {
    ArrayList<Contacts> contactsArray = new ArrayList<Contacts>();
    try {
        int count = 0;
        String stop;
        while(count < jsonArray.length()) {
            JSONObject JO = jsonArray.getJSONObject(count);
            stop = JO.getString("stop");
            Contacts  contacts = new Contacts(stop);
            contactsArray.add(contacts);
            count++;
        }
        contactAdapter = new ContactAdapter(this,R.layout.row_layout, contactsArray);
        listView.setAdapter(contactAdapter);
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

在适配器中执行以下操作:

ArrayList<Contacts> list = new ArrayList<Contacts>();

public ContactAdapter(Context context, int resources, ArrayList<Contacts> list){
    super(context,resources);
    this.list = list;
}

这就是全部。希望能帮到你!