Android ArrayAdapter显示错误:选项卡式活动和列表视图
我正在尝试创建一个包含列表的选项卡式活动。我已经创建了选项卡式活动,我想在片段中创建列表。每个列表项包含2个图像视图和4个文本视图。因此,我创建了一个自定义类和一个自定义ArrayAdapter。这是代码-Android ArrayAdapter显示错误:选项卡式活动和列表视图,android,listview,android-tabbed-activity,Android,Listview,Android Tabbed Activity,我正在尝试创建一个包含列表的选项卡式活动。我已经创建了选项卡式活动,我想在片段中创建列表。每个列表项包含2个图像视图和4个文本视图。因此,我创建了一个自定义类和一个自定义ArrayAdapter。这是代码- java-这是选项卡式活动的片段 package com.example.android.cotescol; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; imp
package com.example.android.cotescol;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import java.util.ArrayList;
public class Coins extends android.support.v4.app.Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.currency_list, container, false);
final ArrayList<CoinObject> coins = new ArrayList<Currency>();
coins.add(new CoinObject(1, "India", 1947));
coins.add(new CoinObject(1, "India", 1947));
CoinAdapter itemsAdapter = new CoinAdapter(this, coins);
ListView listView = (ListView) findViewById(R.id.list);
listView.setAdapter(itemsAdapter);
return rootView;
}
}
package com.example.android.cotescol;
导入android.os.Bundle;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.ListView;
导入java.util.ArrayList;
公共类硬币扩展了android.support.v4.app.Fragment{
@凌驾
CreateView上的公共视图(布局、充气机、视图组容器、捆绑包保存状态){
视图根视图=充气机。充气(R.layout.currency\u列表,容器,false);
最终ArrayList硬币=新ArrayList();
新增(新货币对象(1,“印度”,1947年);
新增(新货币对象(1,“印度”,1947年);
CoinAdapter itemsAdapter=新的CoinAdapter(此为硬币);
ListView ListView=(ListView)findViewById(R.id.list);
setAdapter(itemsAdapter);
返回rootView;
}
}
package com.example.android.cotescol;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
public class CoinObject {
private final static int NO_IMAGE_AVAILABLE = R.drawable.nia;
private final static String NOT_AVAILABLE = "Material Not Specified";
private final static double NOT_KNOWN = 0.00;
private int denomination;
private String country;
private String countryCode;
private int year;
private int obverseImageResourceId = NO_IMAGE_AVAILABLE;
private int reverseImageResourceId = NO_IMAGE_AVAILABLE;
private String material = NOT_AVAILABLE;
private double diameter = NOT_KNOWN;
private double weight = NOT_KNOWN;
private double thickness = NOT_KNOWN;
private double value = NOT_KNOWN;
public CoinObject(int denomination, String country, int year, int obverseImageResourceId, int reverseImageResourceId , double diameter, double thickness, String material, double weight, double value) {
this.denomination = denomination;
this.country = country;
this.year = year;
this.obverseImageResourceId = obverseImageResourceId;
this.reverseImageResourceId = reverseImageResourceId;
this.diameter = diameter;
this.thickness = thickness;
this.material = material;
this.weight = weight;
this.value = value;
this.countryCode = getCountryCode(country);
}
public CoinObject(int denomination, String country, int year, double diameter, double thickness, String material, double weight, double value) {
this.denomination = denomination;
this.country = country;
this.year = year;
this.diameter = diameter;
this.thickness = thickness;
this.material = material;
this.weight = weight;
this.value = value;
this.countryCode = getCountryCode(country);
}
public CoinObject(int denomination, String country, int year) {
this.denomination = denomination;
this.country = country;
this.year = year;
this.countryCode = getCountryCode(country);
}
public String getCountryCode(String countryName) {
// Get all country codes in a string array.
String[] isoCountryCodes = Locale.getISOCountries();
Map<String, String> countryMap = new HashMap<>();
// Iterate through all country codes:
for (String code : isoCountryCodes) {
// Create a locale using each country code
Locale locale = new Locale("", code);
// Get country name for each code.
String name = locale.getDisplayCountry();
// Map all country names and codes in key - value pairs.
countryMap.put(name, code);
}
// Get the country code for the given country name using the map.
// Here you will need some validation or better yet
// a list of countries to give to user to choose from.
String countryCode = countryMap.get(countryName); // "NL" for Netherlands.
return countryCode;
}
public int getDenomination() { return denomination; }
public int getYear() { return year; }
public int getObverseImageResourceId() { return obverseImageResourceId; }
public int getReverseImageResourceId() { return reverseImageResourceId; }
public double getDiameter() { return diameter; }
public double getWeight() { return weight; }
public double getThickness() { return thickness; }
public double getValue() { return value; }
public String getCountry() { return country; }
public String getCountryCode() { return countryCode; }
public String getMaterial() { return material; }
}
package com.example.android.cotescol;
导入java.util.HashMap;
导入java.util.Locale;
导入java.util.Map;
公共类对象{
私有最终静态int NO_IMAGE_AVAILABLE=R.drawable.nia;
私有最终静态字符串不可用=“未指定材料”;
私有最终静态双精度未知=0.00;
私人面额;
私人国家;
私有字符串国家代码;
私人国际年;
private int obverseImageResourceId=没有可用的图像;
private int reverseImageResourceId=没有可用的图像;
私有字符串材料=不可用;
私人双直径=未知;
私人双倍重量=未知;
私有双层厚度=未知;
私有双值=未知;
公共铸币对象(整数面额、字符串国家、整数年份、整数obverseImageResourceId、整数反转MAGERESOURCEID、双直径、双厚度、字符串材质、双重量、双值){
这个。面额=面额;
这个国家=国家;
今年=年;
this.obverseImageResourceId=obverseImageResourceId;
this.reverseImageResourceId=reverseImageResourceId;
这个直径=直径;
这个。厚度=厚度;
这个材料=材料;
重量=重量;
这个值=值;
this.countryCode=getCountryCode(国家);
}
公共铸币对象(整数面额、字符串国家、整数年、双直径、双厚度、字符串材质、双重量、双值){
这个。面额=面额;
这个国家=国家;
今年=年;
这个直径=直径;
这个。厚度=厚度;
这个材料=材料;
重量=重量;
这个值=值;
this.countryCode=getCountryCode(国家);
}
公共硬币对象(整数面额、字符串国家、整数年份){
这个。面额=面额;
这个国家=国家;
今年=年;
this.countryCode=getCountryCode(国家);
}
公共字符串getCountryCode(字符串countryName){
//获取字符串数组中的所有国家/地区代码。
字符串[]isoCountryCodes=Locale.getISOCountries();
Map countryMap=newhashmap();
//反复浏览所有国家/地区代码:
for(字符串代码:isoCountryCodes){
//使用每个国家/地区代码创建区域设置
语言环境=新的语言环境(“,代码);
//获取每个代码的国家名称。
字符串名称=locale.getDisplayCountry();
//在键值对中映射所有国家的名称和代码。
countryMap.put(名称、代码);
}
//使用地图获取给定国家名称的国家代码。
//在这里,您需要一些验证或更好
//提供给用户选择的国家/地区列表。
字符串countryCode=countryMap.get(countryName);/“NL”表示荷兰。
返回国家代码;
}
public int getDemination(){return demination;}
public int getYear(){return year;}
public int getObverseImageResourceId(){return obverseImageResourceId;}
public int getReverseImageResourceId(){return reverseImageResourceId;}
public double getDiameter(){return diameter;}
public double getWeight(){return weight;}
public double getThickness(){return thickness;}
public double getValue(){return value;}
公共字符串getCountry(){return country;}
公共字符串getCountryCode(){return countryCode;}
公共字符串getMaterial(){return material;}
}
package com.example.android.cotescol;
import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
public class CoinAdapter extends ArrayAdapter<CoinObject>{
public CoinAdapter(Context context, ArrayList<CoinObject> currencies) {
super(context, 0, currencies);
}
@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View listItemView = convertView;
if (listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);
}
CoinObject currentCoinObject = getItem(position);
TextView denominationTextView = (TextView) listItemView.findViewById(R.id.denomination_text_view);
denominationTextView.setText(currentCoinObject.getDenomination());
TextView countryTextView = (TextView) listItemView.findViewById(R.id.country_text_view);
countryTextView.setText(currentCoinObject.getCountry() + "(" + currentCoinObject.getCountryCode() + ")");
TextView yearTextView = (TextView) listItemView.findViewById(R.id.year_text_view);
yearTextView.setText(currentCoinObject.getYear());
ImageView obverseImageView = (ImageView) listItemView.findViewById(R.id.obverse_image_view);
obverseImageView.setImageResource(currentCoinObject.getObverseImageResourceId());
ImageView reverseImageView = (ImageView) listItemView.findViewById(R.id.reverse_image_view);
reverseImageView.setImageResource(currentCoinObject.getReverseImageResourceId());
return listItemView;
}
}
package com.example.android.cotescol;
导入android.content.Context;
导入android.support.annotation.NonNull;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.ArrayAdapter;
导入android.widget.ImageView;
导入android.widget.TextView;
导入java.util.ArrayList;
公共类CoinAdapter扩展了ArrayAdapter{
公共CoinAdapter(上下文、ArrayList货币){
超级(上下文,0,货币);
}
@非空
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
View listItemView=convertView;
如果(listItemView==null){
listItemView=LayoutInflater.from(getContext()).flate(R.layout.list_项,父项,false);
}
CoinObject currentCoinObject=getItem(位置);
文本六
CoinAdapter itemsAdapter = new CoinAdapter(this, coins);
CoinAdapter itemsAdapter = new CoinAdapter(getActivity(), coins);
ListView listView = (ListView) findViewById(R.id.list);
ListView listView = (ListView) rootView.findViewById(R.id.list);