我正在关注ListView的教程。在教程中有一个TextView,它包含固定的单词'description',然后是前面的字符串'country'的子集。
我一直在尝试用另一个TextView替换它; '货币'而不是'描述'与每个相应的货币,但我无法弄清楚如何。
感谢任何帮助
以下是教程的列表视图代码:
MainActivity.java
public class MainActivity extends Activity{
ListView list;
String[] country={
"France",
"India",
"Germany",
"The US",
"Japan",
"Korea",
"China",
"Canada"
};
Integer[] countryid={
R.drawable.pic1,
R.drawable.pic2,
R.drawable.pic3,
R.drawable.pic4,
R.drawable.pic5,
R.drawable.pic6,
R.drawable.pic7,
R.drawable.pic8,
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CustomListAdapter adapter=new CustomListAdapter(this, country, countryid);
list=(ListView)findViewById(R.id.list);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
String Slectedcountry= country[+position];
Toast.makeText(getApplicationContext(), Slectedcountry, Toast.LENGTH_SHORT).show();
}
});
}
}
CustomListAdapter.java
public class CustomListAdapter extends ArrayAdapter<String> {
private final Activity context;
private final String[] country;
private final Integer[] countryid;
public CustomListAdapter(Activity context, String[] country, Integer[] countryid) {
super(context, R.layout.mylist, country);
this.context=context;
this.country=country;
this.countryid=countryid;
}
public View getView(int position,View view,ViewGroup parent) {
LayoutInflater inflater=context.getLayoutInflater();
View rowView=inflater.inflate(R.layout.mylist, null,true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.item);
ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
TextView extratxt = (TextView) rowView.findViewById(R.id.textView1);
txtTitle.setText(country[position]);
imageView.setImageResource(countryid[position]);
extratxt.setText("Description "+country[position]); //I want to replace this
return rowView;
};
}
答案 0 :(得分:2)
你需要
TextView txtTitle = (TextView) rowView.findViewById(R.id.item);
txtTitle.setText(country[position]);
修改main( MainActivity.java )以设置新的数组值。类似的东西: -
String[] currency={
"Franc",
"Rupee",
"pfenning",
"Greenback",
"yen",
"something",
"something else",
"Pound"
};
答案 1 :(得分:1)
首先创建一个对象类,它存储国家,图像和货币等数据。
public class CountryObject{
public String country_Name;
public String country_Currency;
public int country_Image;
}
然后是ModelObject的创建arraylist。 List<CountryObject> countryList= new ArrayList<CountryObject>();
CountryObject object1= new CountryObject;
object.country_Name="India";
object.country_currency="Rupee";
cobject.country_Image=R.drawable.pic1;
countryList.add(object1);
创建类似的CountryObject
添加到arraylits。将它传递给适配器类以使用listview进行出价。
CustomListAdapter adapter=new CustomListAdapter(this, countryList);
您的适配器类。请使用Viewholder模式减少getview方法中的调用,建议使用。
public class CustomListAdapter extends BaseAdapter {
private final Activity context;
List<CountryObject> countrylist
public CustomListAdapter(Activity context, List<CountryObject> list) {
this.context=context;
this.countrylist=list;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public CountryObject getItem(int position) {
return countrylist.get(position);
}
@Override
public int getCount() {
return countrylist!= null ? countrylist.size() : 0;
}
public View getView(int position,View convertView,ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.mylist, parent, false);
holder = new ViewHolder();
holder.tvName = (TextView) convertView.findViewById(R.id.item);
holder.ivCountryImage = (ImageView) convertView.findViewById(R.id.icon);
holder.tvCurrency = (TextView) convertView.findViewById(R.id.textView1);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.tvName.setText(countrylist.get(position).country_Name);
holder.ivCountryImage.setImageResource(countrylist.get(position).country_Image);
//holder.tvCurrency.setText("currency "+countrylist.get(position).country_Currency);
holder.tvCurrency.setText(countrylist.get(position).country_Currency);
return convertView;
}
public class ViewHolder {
TextView tvName, tvCurrency;
ImageView ivCountryImage;
}
}
或者你可以使用ArrayAdapter而不是String。