我是android开发领域的新手。我正在为我的学校工作创建一个维生素顾问应用程序。我将游标值存储到此ArrayList中,并遇到此错误:

The method add(int, VitaminInfo) in the type ArrayList<VitaminInfo> is not applicable for the arguments (int, String).


我的dbAdapter来自另一个包含所有sql数据库执行的类。除了使用results.add之外,我还可以使用其他什么方法?谢谢。这是我的编码。

VitaminInfo.java

public class VitaminInfo
{
    //private variables
    int _id;
    String _vitamins;
    String _vitaminsfunction;
    String _vitaminssources;
    int _vitaminsimages;
    String _vitaminscategory;

    // Empty constructor
    public VitaminInfo(){

    }
    // constructor
    public VitaminInfo(int id, String vitamins, String vitaminsfunction, String vitaminssources, int vitaminsimages, String vitaminscategory)
    {
        this._id = id;
        this._vitamins = vitamins;
        this._vitaminsfunction= vitaminsfunction;
        this._vitaminssources = vitaminssources;
        this._vitaminsimages = vitaminsimages;
        this._vitaminscategory = vitaminscategory;
    }

    // constructor
    public VitaminInfo(String vitamins, String vitaminsfunction, String vitaminssources, int vitaminsimages, String vitaminscategory)
    {
        this._vitamins = vitamins;
        this._vitaminsfunction= vitaminsfunction;
        this._vitaminssources = vitaminssources;
        this._vitaminsimages = vitaminsimages;
        this._vitaminscategory = vitaminscategory;
    }

    // getting ID
    public int getID(){
        return this._id;
    }

    // setting id
    public void setID(int id){
        this._id = id;
    }

    // getting vitamins
    public String getVitamins(){
        return this._vitamins;
    }

    // setting vitamins
    public void setVitamins(String vitamins){
        this._vitamins = vitamins;
    }

    // getting symptoms
    public String getFunctions(){
        return this._vitaminsfunction;
    }

    // setting symptoms
    public void setFunctions(String function){
        this._vitaminsfunction = function;
    }

    // getting sources
    public String getSources(){
        return this._vitaminssources;
    }

    // setting sources
    public void setSources(String sources){
        this._vitaminssources = sources;
    }

    // getting images
    public Integer getImages(){
        return this._vitaminsimages;
    }

    // setting images
    public void setImages(int sources){
        this._vitaminsimages = sources;
    }

    // getting category
    public String getCategory(){
        return this._vitaminscategory ;
    }

    // setting category
    public void setCategory(String category){
        this._vitaminscategory = category;
    }

}


DisplayInfoActivityAdapter.java

public class DisplayInfoActivityAdapter extends BaseAdapter
{
    private static ArrayList<VitaminInfo> vitaminDetailsArrayList;

    private Integer[] imgid =
    {
       R.drawable.vb1,
       R.drawable.vb2,
       R.drawable.vb3,
       R.drawable.vb6,
       R.drawable.fc,
       R.drawable.vb12,
       R.drawable.vbc,
       R.drawable.va,
       R.drawable.vd,
       R.drawable.ve,
       R.drawable.vk
    };

    private LayoutInflater l_Inflater;

    public DisplayInfoActivityAdapter(Context context, ArrayList<VitaminInfo> results)
    {
        vitaminDetailsArrayList = results;
        l_Inflater = LayoutInflater.from(context);
     }

     public int getCount()
     {
         return vitaminDetailsArrayList.size();
     }

     public Object getItem(int position)
     {
         return vitaminDetailsArrayList.get(position);
     }

     public long getItemId(int position)
     {
         return position;
     }

     public View getView(int position, View convertView, ViewGroup parent)
     {
         ViewHolder holder;
         if (convertView == null)
         {
             convertView = l_Inflater.inflate(R.layout.info_rowlayout, null);
             holder = new ViewHolder();
             holder.vitaminImage = (ImageView) convertView.findViewById(R.id.vitaminImage);
             holder.vitaminName = (TextView) convertView.findViewById(R.id.vitaminName);
             holder.vitaminFunction = (TextView) convertView.findViewById(R.id.vitaminfunction);
             holder.vitaminSources = (TextView) convertView.findViewById(R.id.vitaminsources);
             holder.vitaminCategory = (TextView) convertView.findViewById(R.id.vitamincategory);
             convertView.setTag(holder);
         }

         else
         {
             holder = (ViewHolder) convertView.getTag();
         }

         holder.vitaminName.setText(vitaminDetailsArrayList.get(position).getVitamins());
       holder.vitaminFunction.setText(vitaminDetailsArrayList.get(position).getFunctions());
         holder.vitaminSources.setText(vitaminDetailsArrayList.get(position).getSources());
         holder.vitaminCategory.setText(vitaminDetailsArrayList.get(position).getCategory());
         holder.vitaminImage.setImageResource(imgid[vitaminDetailsArrayList.get(position).getImages() - 1]);

         return convertView;
     }

     static class ViewHolder
     {
         TextView vitaminName;
         TextView vitaminFunction;
         TextView vitaminSources;
         ImageView vitaminImage;
         TextView vitaminCategory;
     }
}


DisplayInfoActivity.java

public class DisplayInfoActivity extends Activity {

    ListView tablelayout_Vitamins = null;
    ArrayAdapter<String> listAdapter;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.info_layout);

        RelativeLayout view = (RelativeLayout) findViewById(R.id.inforelativelayout1);
        view.setBackgroundColor( Color.CYAN );

        ArrayList<VitaminInfo> image_details = GetSearchResults();
        final ListView lv1 = (ListView) findViewById(R.id.contentlist);
        lv1.setAdapter(new DisplayInfoActivityAdapter(this, image_details));

    }

    // ------ for "View" Button -------
    private ArrayList<VitaminInfo> GetSearchResults()
    {
        ArrayList<VitaminInfo> results = new ArrayList<VitaminInfo>();
        VitaminInfo vitaminInfo;

        DatabaseHandler dbAdaptor = new DatabaseHandler(getApplicationContext());

        Cursor cursor = null;

        try{
            dbAdaptor.open();

            cursor = dbAdaptor.getAllInfo();
            cursor.moveToFirst();

            do{
                int  id = cursor.getInt(0);
                String vitamin = cursor.getString(1);
                String function = cursor.getString(2);
                String sources = cursor.getString(3);
                String image = cursor.getString(4);
                String category = cursor.getString(5);

                results.add(1, vitamin);
                results.add(2, function);
                results.add(3, sources);
                results.add(4, image);
                results.add(5, category);
            }
            while (cursor.moveToNext());
        }
        catch (Exception e){
            Log.d("Contact Manager", e.getMessage());
        }
        finally{
            if (cursor != null)
                cursor.close();

            if (dbAdaptor != null)
                dbAdaptor.close();
        }
        return results;

    }//refreshTable

}

最佳答案

您不能添加不将VitaminInfo扩展到ArrayList<VitaminInfo>的类型的对象。 String不扩展VitaminInfo。您必须创建一个VitaminInfo对象并将该对象添加到列表中:

int imageAsInt = // convert your image to int here or get it as int in the first place
results.add(new VitaminInfo(id, vitamin, function, sources, imageAsInt, category));

10-07 19:15