【问题标题】:The constructor ArrayAdapter<ArrayList<HashMap<String, String>>>(Context, int, ArrayList<HashMap<String, String>>) is undefined构造函数 ArrayAdapter<ArrayList<HashMap<String, String>>>(Context, int, ArrayList<HashMap<String, String>>) 未定义
【发布时间】:2013-07-21 17:43:17
【问题描述】:

我对 Android 完全陌生。

这个问题可能是一个基本问题。

但我为此苦苦挣扎了四天。请帮我。

我正在制作水平列表视图,以提供博客中几个类别的内容。

(与Pulse新闻应用类似的界面)

我得到了水平列表视图的开源,我正在修改它。

此代码是CustomArrayAdapter.java

但是当我尝试写super();在构造函数内部,它会出现这样的错误:

The constructor ArrayAdapter<ArrayList<HashMap<String, String>>>(Context, int, ArrayList<HashMap<String, String>>) is undefined

Eclipse 建议是这样的:

Remove argument to match 'ArrayAdapter<ArrayList<HashMap<String, String>>>(Context, int)'

我不知道这个 (Context, int) 参数是从哪里来的。

请检查下面的CustomArrayAdapter.java 有什么问题:

package com.xxxx.xxxxxxxxx;

import java.util.ArrayList;
import java.util.HashMap;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/** An array adapter that knows how to render views when given CustomData classes */
public class CustomArrayAdapter extends ArrayAdapter<ArrayList<HashMap<String, String>>> {
    private Context context;
    private ArrayList<HashMap<String, String>> data;
    private int viewId;

    private LayoutInflater mInflater;

    public CustomArrayAdapter(Context c, int textViewResourceId, ArrayList<HashMap<String, String>> d) {
        super(c, textViewResourceId, d);

        this.context = c;
        this.viewId = textViewResourceId;
        this.data = d; 
    }

     @Override
     public int getCount() {
         return data.size();
     }

    /*
     * We are overriding the getView method here - this is what defines how each
     * list item will look.
     */
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {         
        // Assign the view we are converting to a local variable
        View vi = convertView;
        Holder holder;      

        if (convertView == null) {

            // Inflate the view since it does not exist
            if (vi == null) {
                mInflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                vi = mInflater.inflate(R.layout.custom_data_view, null);
            }

            /*
             * Recall that the variable position is sent in as an argument to this method.
             * The variable simply refers to the position of the current object in the list. (The ArrayAdapter
             * iterates through the list we sent it)
             * 
             * Therefore, i refers to the current Item object.
             */

            // This is how you obtain a reference to the TextViews.
            // These TextViews are created in the XML files we defined.

            // Create and save off the holder in the tag so we get quick access to inner fields
            // This must be done for performance reasons

            holder = new Holder();
            holder.textView = (TextView) vi.findViewById(R.id.title_view);
            holder.imageView = (ImageView) vi.findViewById(R.id.thumbnail_view);

            vi.setTag(holder);                                  

        } else {
            holder = (Holder) vi.getTag();
        }

        // check to see if each individual textview is null.
        // if not, assign some text!
        // Populate the text 

        HashMap<String, String> currentData = new HashMap<String, String>();
        currentData = data.get(position);

        if (currentData != null) {
            holder.textView.setText(currentData.get(MainActivity.KEY_TITLE));
            holder.imageLoader = new ImageLoader(context.getApplicationContext());    
            holder.imageLoader.DisplayImage(currentData.get(MainActivity.KEY_THUMBNAIL), holder.imageView);
        }       

        // Set the color
        vi.setBackgroundColor(Color.DKGRAY);
        return vi;
    }

    /** View holder for the views we need access to */
    private static class Holder {
        public TextView textView;
        public ImageView imageView;
        public ImageLoader imageLoader;
    }
}

【问题讨论】:

  • developer.android.com/reference/android/widget/…。检查arrayadapter的公共构造函数。你在哪里调用你的自定义适配器?
  • @Raghunandan 感谢您的评论。实际上我已经检查了那个页面,但当时我找不到什么问题。幸运的是,问题已经解决了。

标签: android arraylist undefined custom-adapter horizontallist


【解决方案1】:

应该是:

public class CustomArrayAdapter extends ArrayAdapter<HashMap<String, String>>

【讨论】:

  • 非常感谢。你救了我:) 我按照你的指导修改了代码,现在它可以正常工作了。谢谢!
【解决方案2】:

编译器告诉你改变这个

super(c, textViewResourceId, d);

super(c, textViewResourceId);

【讨论】:

  • 感谢您的回答。是的,当然我试过了。但是在没有参数 d 的情况下调用 super(c, textViewResourceId); 会产生另一个问题 - 它使 listview 的项目计数为零。所以在这种情况下我不得不与 IndexOutOfBoundsException 作斗争。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-07-29
  • 1970-01-01
  • 1970-01-01
  • 2017-08-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多