[英]Am using Gson with freemarker template. When i try to check an array object as null then am getting error
[英]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
每當我嘗試將項目添加到對象時,都會出現錯誤
我試圖在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中的add(int,com.example.tourguide.ui.main.Place)無法應用於(int,int) )
您正在嘗試添加名稱和圖像以直接列出。 您應該做的是首先創建一個放置對象,然后將其添加到列表中。
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);
編輯
您可以將任何類型的數據添加到列表中,但是它必須與初始化數組時聲明的類型相同。 例如,您正在制作一個場所列表,因此只能將場所對象添加到數組中。
add方法采用兩個參數,首先是索引,然后是對象。 如果僅使用list.add(E element)
,它將把Object附加到列表的末尾。
public void add(int index,E元素)
將指定的元素插入此列表中的指定位置。 將當前在該位置的元素(如果有)和任何后續元素右移(將其索引添加一個)。
您可以從此文檔中找到有關ArrayList的更多信息
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.