实际上我正在尝试 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 时,问题就解决了。
以前按textview后是这样显示的
但我仍然不知道为什么这个带有内部 LinearLayout 的布局文件不起作用。