Listview不显示数据,但它显示在logcat中

时间:2013-08-30 09:32:16

标签: android json listview

实际上我正在尝试JSON解析示例以及解析但未在列表视图中显示的数据。在logcat中数据即将到来,但它没有显示在列表中。 我正在使用模拟器运行。我无法找到错误。任何人都可以帮忙吗?

下面的代码显示了自定义列表适配器。

 public class ListAdapter extends BaseAdapter{

    ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String,String>>();
    Context context;

    public ListAdapter(ArrayList<HashMap<String, String>> list,
            Context context) {
        Log.i("ListAdapter", "ListAdapter");
        this.list = list;
        this.context = context;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return list.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
        Log.i("ListAdapter", "getView");
        if(convertView == null){
            convertView = LayoutInflater.from(context).inflate(R.layout.list, null);

        }
        HashMap<String, String> hashmap = new HashMap<String, String>();

        TextView trank = null,tcountry = null,tpopulation = null;
        ImageView image;

            trank = (TextView) convertView.findViewById(R.id.rank);
            tcountry = (TextView) convertView.findViewById(R.id.country);
            tpopulation = (TextView) convertView.findViewById(R.id.population);
            image = (ImageView) convertView.findViewById(R.id.flag);
            hashmap = list.get(position);
            Log.i("list", ""+list.get(position) + "hashmp " + hashmap + hashmap.get("rank"));
        String rank = hashmap.get("rank").toString();
        String country = hashmap.get("country").toString();
        String population = hashmap.get("population").toString();
        String image_url = hashmap.get("flag");
            Log.i("ListAdapter", ""+rank + " "+ country + " " +population );

        trank.setText(rank);
        tcountry.setText(country);
        tpopulation.setText(population);
        return convertView;
    }

}

主要活动类

public class MainActivity extends Activity {

TextView webpage,trank,tcountry,tpopulation;
ListView lv;
ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String,String>>();
String RANK = "rank";
String COUNTRY = "country";
String POPULATION = "population";
String FLAG = "flag";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    lv = (ListView) findViewById(R.id.listview);
    trank = (TextView) findViewById(R.id.rank);
    tcountry = (TextView) findViewById(R.id.country);
    tpopulation = (TextView) findViewById(R.id.population);
    webpage = (TextView) findViewById(R.id.webpage);
    webpage.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            String data = getDataFromWebPage();
            list = parseDataFromWeb(data);
            ListAdapter adapter = new ListAdapter(list, getApplicationContext());
            lv.setAdapter(adapter);
        }



    });
}

自定义列表布局文件

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >
    <LinearLayout         
         android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        >
    <TextView 
        android:id="@+id/rank"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="rank"
        />
      <TextView 
        android:id="@+id/country"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="country"
        />
        <TextView 
        android:id="@+id/population"
        android:text="population"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
</LinearLayout>
          <ImageView 
        android:id="@+id/flag"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
</LinearLayout>

我得到了解决方案,实际问题在于此布局文件。当我删除内部LinearLayout问题时,问题就解决了。

Previously it was displayed like this 以前在按下textview

后显示如下

但我仍然不知道为什么这个内部LinearLayout的布局文件不起作用。

4 个答案:

答案 0 :(得分:1)

你应该在getItem()方法中返回位置:

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return position;
}

你必须改变:

ListAdapter adapter = new ListAdapter(getApplicationContext(),list);

答案 1 :(得分:1)

在此处更改代码:

@Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

答案 2 :(得分:1)

试试这个

          ListAdapter adapter = new ListAdapter( MainActivity.this,list);
        lv.setAdapter(adapter);

答案 3 :(得分:0)

问题与自定义列表布局有关。因为内部线性布局数据没有显示。删除内部布局后,它工作