Java 如何在BaseAdapter';什么是GetView()方法?
我正在尝试解析以下JsonJava 如何在BaseAdapter';什么是GetView()方法?,java,android,android-adapter,baseadapter,Java,Android,Android Adapter,Baseadapter,我正在尝试解析以下Json { "effect_list": [{ "1":[ { "effects_id":"1", "effects_name":"Band 1" }, { "effects_id":"2", "effects_name":"Band 2" } ], "2":
{
"effect_list": [{
"1":[
{
"effects_id":"1",
"effects_name":"Band 1"
},
{
"effects_id":"2",
"effects_name":"Band 2"
}
],
"2": [
{
"effects_id":"4",
"effects_name":"Background Blur"
},
{
"effects_id":"5",
"effects_name":"Blemish Removal"
}
]
}]
}
My BaseAdapter仅显示ListView中的第一项。我正在尝试检索地图中的值。在迭代过程中,值被覆盖。当我打印值时,它正确打印
MyContactAdapter2.java
public class MyContactAdapter2 extends BaseAdapter {
List<Map<String, List<EffectList>>> contactList;
Context context;
private LayoutInflater mInflater;
// Constructors
public MyContactAdapter2(Context context, List<Map<String, List<EffectList>>> objects) {
this.context = context;
this.mInflater = LayoutInflater.from(context);
contactList = objects;
}
public MyContactAdapter2() {
System.out.println("hai");
}
@Override
public int getCount() {
int count = contactList.size();
System.out.println("Count size" + count);
return count;
}
@Override
public Map<String, List<EffectList>> getItem(int position) {
return contactList.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
System.out.println(10);
final MyContactAdapter2.ViewHolder vh;
if (convertView == null) {
View view = mInflater.inflate(R.layout.get_layout_row_view, parent, false);
vh = MyContactAdapter2.ViewHolder.create((RelativeLayout) view);
view.setTag(vh);
} else {
vh = (MyContactAdapter2.ViewHolder) convertView.getTag();
}
for (Map<String, List<EffectList>> map : contactList) {
for (Map.Entry<String, List<EffectList>> entry : map.entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();
System.out.println("key :" + key + "value :" + value);
EffectList item = getItem(position).get(key).get(0);
vh.textViewName.setText(item.getEffectsId());
vh.textViewEmail.setText(item.getEffectsName());
}
}
return vh.rootView;
}
private static class ViewHolder {
public final RelativeLayout rootView;
public final ImageView imageView;
public final TextView textViewName;
public final TextView textViewEmail;
private ViewHolder(RelativeLayout rootView, ImageView imageView, TextView textViewName, TextView textViewEmail) {
this.rootView = rootView;
this.imageView = imageView;
this.textViewName = textViewName;
this.textViewEmail = textViewEmail;
}
public static MyContactAdapter2.ViewHolder create(RelativeLayout rootView) {
ImageView imageView = (ImageView) rootView.findViewById(R.id.imageView);
TextView textViewName = (TextView) rootView.findViewById(R.id.textViewName);
TextView textViewEmail = (TextView) rootView.findViewById(R.id.textViewEmail);
return new MyContactAdapter2.ViewHolder(rootView, imageView, textViewName, textViewEmail);
}
}
}
公共类MyContactAdapter2扩展BaseAdapter{
列出联系人名单;
语境;
私人停车场;
//建设者
公共MyContactAdapter2(上下文、列表对象){
this.context=上下文;
this.mInflater=LayoutInflater.from(上下文);
联系人列表=对象;
}
公共MyContactAdapter2(){
系统输出打印号(“hai”);
}
@凌驾
public int getCount(){
int count=contactList.size();
系统输出打印项次(“计数大小”+计数);
返回计数;
}
@凌驾
公共地图getItem(内部位置){
返回联系人列表。获取(位置);
}
@凌驾
公共长getItemId(int位置){
返回0;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
系统输出打印Ln(10);
最终MyContactAdapter2.ViewHolder vh;
if(convertView==null){
视图视图=mInflater.flate(R.layout.get\u layout\u row\u视图,父视图,false);
vh=MyContactAdapter2.ViewHolder.create((RelativeLayout)视图);
视图.设置标签(vh);
}否则{
vh=(MyContactAdapter2.ViewHolder)convertView.getTag();
}
用于(映射:联系人列表){
对于(Map.Entry:Map.entrySet()){
String key=entry.getKey();
对象值=entry.getValue();
System.out.println(“键:+key+”值:+value”);
EffectList项=getItem(位置).get(键).get(0);
vh.textViewName.setText(item.getEffectsId());
vh.textViewEmail.setText(item.getEffectsName());
}
}
返回vh.rootView;
}
私有静态类视图持有者{
公共最终相对论Yout rootView;
公共最终图像视图;
公共最终文本视图文本视图名称;
公开最终文本查看文本查看电子邮件;
私有视图持有者(RelativeLayout rootView、ImageView ImageView、TextView textViewName、TextView textViewEmail){
this.rootView=rootView;
this.imageView=imageView;
this.textViewName=textViewName;
this.textViewEmail=textViewEmail;
}
公共静态MyContactAdapter2.ViewHolder创建(RelativeLayout rootView){
ImageView ImageView=(ImageView)rootView.findViewById(R.id.ImageView);
TextView textViewName=(TextView)rootView.findViewById(R.id.textViewName);
TextView textViewEmail=(TextView)rootView.findViewById(R.id.textViewEmail);
返回新的MyContactAdapter2.ViewHolder(rootView、imageView、textViewName、textViewEmail);
}
}
}
所以我的问题是,如何迭代“BaseAdapter”中“GetView()”中的“Map”值?
是否可以在BaseAdapter中的GetView()方法中迭代值?您不能执行以下操作:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// ... blah blah ...
final Map<String, List<EffectList>> effectMap = getItem(position);
for (Map.Entry<String, List<EffectList>> entry : effectMap.entrySet()) {
final String key = entry.getKey();
final List<EffectList> value = entry.getValue();
// ... do stuff ...
}
// ... blah blah
}
@覆盖
公共视图getView(int位置、视图转换视图、视图组父视图){
//……诸如此类。。。
最终地图效果图=获取项目(位置);
对于(Map.Entry:effectMap.entrySet()){
最后一个字符串key=entry.getKey();
最终列表值=entry.getValue();
//…做事。。。
}
//……诸如此类
}
?运行是否正常?是的,运行时没有错误可以在getView()内迭代映射,但不要太多,这可能会减慢应用程序的运行速度。您可以为上述代码中的问题提供解决方案吗?我收到错误:(147,58)错误:对于每个不适用于所需表达式类型的:array或java.lang.Iterable发现:Mapcompile time错误。错误:(148,41)错误:找不到符号方法getKey()和getValue()@user7357013您是否编写了
Map.Entry
而不仅仅是Map
?请参阅并正确打印值,但仅显示Listview中的第一项。并且我的getCount()大小始终为1。