简体   繁体   English

我已经用TextView和ImageView创建了一个Place对象,但是当我尝试添加一个place对象时出现此错误

[英]I have created a Place object with a TextView and ImageView but I am getting this error when I try to add a place object

Whenever I try to add an item to the object it gives an error 每当我尝试将项目添加到对象时,都会出现错误

I tried to switch between android.support.v4.app and the other one but it is not working 我试图在android.support.v4.app和另一个之间切换,但是它不起作用

Code of Place Object 放置对象代码

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;
}

}

Code of Place Adapter 放置适配器代码

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;
}

}

This is where I am getting an error when I try to add an item in Place object 当我尝试在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;

    }
}

This is the error message that i am getting 这是我收到的错误消息

Wrong 2nd argument type. 第二个参数类型错误。 Found: 'int', required: 'com.example.tourguide.ui.main.Place' Inspection info: add (int, com.example.tourguide.ui.main.Place) in ArrayList cannot be applied to (int, int) 找到:'int',必需:'com.example.tourguide.ui.main.Place'检查信息:ArrayList中的add(int,com.example.tourguide.ui.main.Place)无法应用于(int,int) )

You're trying to add name and the image to list directly. 您正在尝试添加名称和图像以直接列出。 What you should do is first create a Place Object and then add this to the list. 您应该做的是首先创建一个放置对象,然后将其添加到列表中。

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);

EDIT 编辑

You can add any type of data to a List, but it has to be the same type as you declare when initializing the array. 您可以将任何类型的数据添加到列表中,但是它必须与初始化数组时声明的类型相同。 For example, you are making a List of Places and therefore you can only add Place Object to the array. 例如,您正在制作一个场所列表,因此只能将场所对象添加到数组中。

The add method takes two parameters, first the index and then the object. add方法采用两个参数,首先是索引,然后是对象。 If you just use list.add(E element) it will append the Object to the end of the list. 如果仅使用list.add(E element) ,它将把Object附加到列表的末尾。

public void add(int index, E element) public void add(int index,E元素)

Inserts the specified element at the specified position in this list. 将指定的元素插入此列表中的指定位置。 Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices). 将当前在该位置的元素(如果有)和任何后续元素右移(将其索引添加一个)。

You can find more about ArrayList from this doc 您可以从此文档中找到有关ArrayList的更多信息

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我正在使用带有freemarker模板的Gson。 当我尝试将数组对象检查为null时出现错误 - Am using Gson with freemarker template. When i try to check an array object as null then am getting error 为什么在尝试使用数组创建 Scoreboard 对象时出现错误? - Why am I getting an error when I try to create the Scoreboard object with an array? 当我尝试在Android ToggleButton中放置onclicklistener时获取nullpointerexception - Getting nullpointerexception when i try to place a onclicklistener in android ToggleButton 当我尝试将新列表 object 添加到预定义列表 object 时出现错误 - Getting error when I am trying to add a new List object to a pre-defined List object 当我尝试使用(来自keySet())打印每个键时,为什么我会得到对象地址? - Why am i getting object address when i try to print each key using (from keySet())? 我为 para 构造函数创建的 class 的 object 显示错误。 我得到的错误是找不到符号 - The object that I created of the class for para constructer is showing a error. The error that I am getting is that that the symbol cant be found 当我尝试添加 mysql 连接器依赖项时出现错误 - I am getting an error when i try to add mysql connector dependency 将对象添加到ArrayList时为什么出现此错误-Java - Why am I getting this error when adding object to ArrayList - Java 当我尝试将TextView放在ImageView前面时,TextView消失了 - TextView disappearing when I try to put it in front of my ImageView 当我尝试使用Swing将Container对象添加到主JFrame对象时,为什么会出现此错误? - Why I obtain this error when I try to add the Container object to the main JFrame object using Swing?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM