首页 > 解决方案 > 我创建了一个带有 TextView 和 ImageView 的 Place 对象,但是当我尝试添加一个 place 对象时出现此错误

问题描述

每当我尝试向对象添加项目时,它都会出错

我试图在 android.support.v4.app 和另一个之间切换,但它不起作用

地点对象代码

package com.example.tourguide.ui.main;
public class Place {


/** String resource ID for the name of the place */
private int mDefaultTranslationId;

private int mImageResourceId;

public Place(int defaultTranslationId, int imageResourceId) {
    mDefaultTranslationId = defaultTranslationId;
    mImageResourceId = imageResourceId;
}

/**
 * Get the string resource ID for the name of the place.
 */
public int getPlaceName() {
    return mDefaultTranslationId;
}

/**
 * Return the image resource ID of the place.
 */
public int getImageResourceId() {
    return mImageResourceId;
}

}

地方适配器代码

package com.example.tourguide.ui.main;

import android.content.Context;
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 com.example.tourguide.R;

import java.util.ArrayList;

public class PlaceAdapter extends ArrayAdapter<Place> {

public PlaceAdapter(Context context, ArrayList<Place> places) {
    super(context, 0, places);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // Check if an existing view is being reused, otherwise inflate the 
view
    View listItemView = convertView;
    if (listItemView == null) {
        listItemView = LayoutInflater.from(getContext()).inflate(
                R.layout.list_item, parent, false);
    }


    Place currentPlace = getItem(position);

    TextView placeName = (TextView) 
listItemView.findViewById(R.id.ImageViewText);

    placeName.setText(currentPlace.getPlaceName ());

    ImageView imageView = (ImageView) 
listItemView.findViewById(R.id.ImageView);

        imageView.setImageResource ( currentPlace.getImageResourceId () );

    return listItemView;
}

}

这是我尝试在 Place 对象中添加项目时遇到错误的地方

package com.example.tourguide.ui.main;


import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import androidx.fragment.app.Fragment;

import com.example.tourguide.R;

import java.util.ArrayList;

public class MonumentsFragment extends Fragment {

public MonumentsFragment() {
    // Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate( R.layout.places_list, container, 
  false);

    final ArrayList<Place> places = new ArrayList<> ();

    places.add(R.string.app_name, R.drawable.google_maps);


    PlaceAdapter adapter = new PlaceAdapter (getActivity(), places);

    ListView listView = (ListView) rootView.findViewById(R.id.list);

    listView.setAdapter(adapter);


    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() 
{
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int 
position, long l) {

            Place place = places.get(position);
        }
    });

    return rootView;

    }
}

这是我收到的错误消息

第二个参数类型错误。找到:'int',必需:'com.example.tourguide.ui.main.Place' 检查信息:ArrayList 中的添加 (int, com.example.tourguide.ui.main.Place) 不能应用于 (int, int )

标签: javaandroidandroid-studioandroid-fragments

解决方案


您正在尝试将名称和图像直接添加到列表中。您应该首先创建一个 Place Object,然后将其添加到列表中。

ArrayList<Place> places = new ArrayList<> ();
// create a Place Object
Place place = new Place(R.string.app_name, R.drawable.google_maps)
// Then add the object to the list
places.add(place);

编辑

您可以将任何类型的数据添加到 List,但它必须与您在初始化数组时声明的类型相同。例如,您正在制作一个地点列表,因此您只能将地点对象添加到数组中。

add 方法有两个参数,第一个是索引,然后是对象。如果您只是使用list.add(E element)它将对象附加到列表的末尾。

公共无效添加(int索引,E元素)

在此列表中的指定位置插入指定元素。将当前位于该位置的元素(如果有)和任何后续元素向右移动(将其索引加一)。

您可以从此文档中找到有关 ArrayList 的更多信息


推荐阅读