Ich habe eine Klasse Country.java erstellt und meinen benutzerdefinierten Adapter myListAdapter, die ArrayAdapter erweitert. Ich erhalte keinen messeges in meinem LogCat entweder aber die Anwendung abstürzt in Zeile:ListView mit benutzerdefinierten Adaptern
countName.setText(" "+currentCountry.getCountryName());
so was falsch ist hier, wie kann ich Country bekommen?
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;
}
}
hier ist Klasse 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 ist:
<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" />
Was ist 'myCountries'? Woher kommt das? –
http://androidexample.com/How_To_Create_A_Custom_Listview_-_Android_Example/index.php?view=article_discription&aid=67 verweisen Sie auf diesen Link. Es ist gut, ViewHolder für Adapter zu verwenden. –