Java 我已经创建了一个带有TextView和ImageView的Place对象,但是当我尝试添加Place对象时出现了这个错误
每当我尝试向对象添加一个项时,它都会给出一个错误 我试图在android.support.v4.app和另一个应用程序之间切换,但它不起作用 放置对象的代码Java 我已经创建了一个带有TextView和ImageView的Place对象,但是当我尝试添加Place对象时出现了这个错误,java,android,android-studio,android-fragments,Java,Android,Android Studio,Android Fragments,每当我尝试向对象添加一个项时,它都会给出一个错误 我试图在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 mImageRes
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;
}
}
这就是我在尝试添加项就地对象时出错的地方
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您试图将名称和图像直接添加到列表中。您应该首先创建一个Place对象,然后将其添加到列表中 ArrayList places=新的ArrayList; //创建放置对象 Place Place=new PlaceR.string.app\u name,R.drawable.google\u maps //然后将对象添加到列表中 places.addplace; 编辑 您可以向列表中添加任何类型的数据,但它必须与初始化数组时声明的类型相同。例如,您正在制作位置列表,因此只能将位置对象添加到数组中 add方法接受两个参数,首先是索引,然后是对象。如果只使用list.addE元素,它会将对象附加到列表的末尾 公共void附加索引,E元素 在此列表中的指定位置插入指定元素。如果存在任何元素,则移动当前位于该位置的元素,并且任何后续元素向右移动会将一个元素添加到其索引中
你可以从这里找到更多关于ArrayList的信息非常感谢,伙计,它现在可以工作了!!就为了知识,你能告诉我我不能直接添加名称和图像吗?@VihaanMisra我更新了答案,希望它能帮助理解概念,谢谢!!我知道我现在错在哪里了。