我已經創建了一個Country.java類,並使我的自定義適配器myListAdapter擴展了ArrayAdapter。我沒有讓行,我既logcat中任何內留言,但應用程序崩潰:ListView使用自定義適配器
countName.setText(" "+currentCountry.getCountryName());
究竟什麼是錯在這裏我怎樣才能得到國家或地區名稱?
public class Country{
private String countryName;
private String capitalName;
public Country(String countryName, String capitalName) {
this.countryName = countryName;
this.capitalName = capitalName;
}
public String getCountryName() {
return countryName;
}
public String getCapitalName() {
return capitalName;
}
}
這裏是類myListAdapter
private class MyListAdapter extends ArrayAdapter<Country>{
public MyListAdapter() {
super(MainActivity.this, R.layout.item_view, myCountries);
}
public View getView(int position, View convertView, ViewGroup parent) {
View itemView = convertView;
if(itemView == null){
itemView = getLayoutInflater().inflate(R.layout.item_view, parent, false);
}
Country currentCountry = myCountries.get(position);
TextView countName = (TextView) findViewById(R.id.item_countryName);
countName.setText(" "+currentCountry.getCountryName());
TextView capName = (TextView) findViewById(R.id.item_CapName);
capName.setText(currentCountry.getCapitalName());
return itemView;
}
}
item_view.xml是:
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/item_countryName" />
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/item_CapName" />
什麼是myCountries?它從何而來? –
http://androidexample.com/How_To_Create_A_Custom_Listview_-_Android_Example/index.php?view=article_discription&aid=67請參閱此鏈接。適配器使用ViewHolder是很好的。 –