片段活动中的 ListView 适配器上下文错误,我该怎么办?
Posted
技术标签:
【中文标题】片段活动中的 ListView 适配器上下文错误,我该怎么办?【英文标题】:ListViewAdapter context on fragment activity error, what should I do? 【发布时间】:2019-08-20 12:44:27 【问题描述】:晚安机器人大师。请帮我。也许比我更了解的人可以帮助我解决我现在面临的问题。
1https://imgur.com/S2TU08q
我在片段上做了一个列表视图。我停下来,因为我对如何进行感到困惑。也许下面的代码可以帮助你解决我的问题!
这是我制作的片段。我将它命名为 Frag_Tour.java
public class Frag_Tour extends Fragment
ListView listView;
ListViewAdapterTour adapter;
String[] judul1;
String[] judul2;
String[] durasi;
String[] harga;
int[] gambarTour;
ArrayList<Model> arrayList = new ArrayList<Model>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
View rootView = inflater.inflate(R.layout.frag_tour, container, false);
judul1 = new String[]"Paket Hemat", "Paket Reguler", "Paket Honeymoon";
judul2 = new String[]"Wisata Belitung", "Wisata Belitung", "Wisata Belitung";
durasi = new String[]"3 Hari 2 Malam", "4 Hari 3 Malam", "3 Hari 1 Malam";
harga = new String[]"Rp 750.000/pax", "Rp 750.000/pax", "Rp 750.000/pax";
gambarTour = new int[]
R.drawable.mercusuar_1, R.drawable.mercusuar_2, R.drawable.mercusuar_3
;
listView = rootView.findViewById(R.id.listView);
for (int i = 0; i < judul1.length; i++)
Model model = new Model(judul1[i], judul2[i], durasi[i], harga[i], gambarTour[i]);
arrayList.add(model);
adapter = new ListViewAdapterTour(this, arrayList);
listView.setAdapter((ListAdapter) arrayList);
return rootView;
这是 xml 文件。 frag_tour.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/parent_view"
android:orientation="vertical"
android:layout_
android:layout_>
<ListView
android:id="@+id/listView"
android:layout_
android:layout_/>
</LinearLayout>
[2]https://imgur.com/ehPanZM
这是我创建的 ListViewAdapter。我将它命名为 ListViewAdapterTour.java
public class ListViewAdapterTour extends BaseAdapter
Context mContext;
LayoutInflater inflater;
List<Model> modellist;
ArrayList<Model> arrayList;
public ListViewAdapterTour (Context context, List<Model> modellist)
mContext = context;
this.modellist = modellist;
inflater = LayoutInflater.from(mContext);
this.arrayList = new ArrayList<Model>();
this.arrayList.addAll(modellist);
public class ViewHolder
TextView Judul1, Judul2, Durasi, Harga;
ImageView GambarTour;
@Override
public int getCount()
return modellist.size();
@Override
public Object getItem(int i)
return modellist.get(i);
@Override
public long getItemId(int i)
return i;
@Override
public View getView(final int position, View view, ViewGroup parent)
ViewHolder holder;
if (view==null)
holder = new ViewHolder();
view = inflater.inflate(R.layout.row_tour, null);
holder.Judul1 = view.findViewById(R.id.judul1);
holder.Judul2 = view.findViewById(R.id.judul2);
holder.Durasi = view.findViewById(R.id.durasi);
holder.Harga = view.findViewById(R.id.harga);
holder.GambarTour = view.findViewById(R.id.GambarTour);
view.setTag(holder);
else
holder = (ViewHolder)view.getTag();
holder.Judul1.setText(modellist.get(position).getJudul1());
holder.Judul2.setText(modellist.get(position).getJudul2());
holder.Durasi.setText(modellist.get(position).getDurasi());
holder.Harga.setText(modellist.get(position).getHarga());
holder.GambarTour.setImageResource(modellist.get(position).getGambartour());
view.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
if (modellist.get(position).getJudul1().equals("Paket Hemat 3 Hari 2 Malam"))
Intent intent = new Intent(mContext, TourDetail.class);
intent.putExtra("contentTv","PAKET HEMAT");
mContext.startActivity(intent);
);
return view;
这是 xml 文件。 row_tour.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:focusableInTouchMode="true"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:layout_
android:layout_
android:layout_margin="@dimen/spacing_middle"
android:layout_weight="1"
app:cardCornerRadius="2dp"
app:cardElevation="2dp">
<RelativeLayout
android:layout_
android:layout_
android:orientation="vertical">
<ImageView
android:id="@+id/GambarTour"
android:layout_
android:layout_
android:scaleType="centerCrop"
android:src="@drawable/mercusuar_1"
tools:ignore="ContentDescription" />
<View
android:layout_
android:layout_
android:background="@color/overlay_dark_20" />
<LinearLayout
android:layout_
android:layout_
android:layout_alignTop="@id/GambarTour"
android:layout_margin="@dimen/spacing_medium"
android:orientation="vertical"
tools:ignore="UselessParent">
<TextView
android:id="@+id/judul1"
style="@style/TextAppearance.AppCompat.Large"
android:layout_
android:layout_
android:text="Paket Hemat"
android:textColor="@color/White"
android:textStyle="bold" />
<TextView
android:id="@+id/judul2"
style="@style/TextAppearance.AppCompat.Large"
android:layout_
android:layout_
android:text="Wisata Belitung"
android:textColor="@color/White"
android:textStyle="bold" />
<TextView
android:id="@+id/durasi"
style="@style/TextAppearance.AppCompat.Caption"
android:layout_
android:layout_
android:text="@string/_3_hari_2_malam"
android:textColor="@color/White" />
</LinearLayout>
<ImageView
android:layout_
android:layout_
android:layout_alignEnd="@id/GambarTour"
android:layout_alignRight="@id/GambarTour"
android:layout_margin="@dimen/spacing_medium"
android:src="@drawable/ic_favorite_border"
android:tint="@color/White" />
<TextView
android:id="@+id/harga"
android:layout_
android:layout_
android:layout_alignEnd="@id/GambarTour"
android:layout_alignRight="@id/GambarTour"
android:layout_alignBottom="@id/GambarTour"
android:layout_margin="@dimen/spacing_large"
android:background="@drawable/gradient_green"
android:padding="@dimen/spacing_xmedium"
android:text="Rp 750.000/pax"
android:textColor="@color/White" />
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
[3]https://imgur.com/h7WiBgK
这是我使用的 model.java 的代码
package com.androidrion.sejengkalapp;
class Model
private String judul1;
private String judul2;
private String durasi;
private String harga;
private int gambartour;
//constructor
Model(String judul1, String judul2, String durasi, String harga, int gambartour)
this.judul1 = judul1;
this.judul2 = judul2;
this.durasi = durasi;
this.harga = harga;
this.gambartour = gambartour;
//getters
String getJudul1()
return this.judul1;
String getJudul2()
return this.judul2;
String getDurasi()
return this.durasi;
String getHarga()
return this.harga;
int getGambartour()
return this.gambartour;
希望安卓的老师和高手能帮我解决这个问题。我想要的是 row_tour.xml,我将其设计为 frag_tour.xml 中的列表视图
[4]https://imgur.com/VLzOUiK
【问题讨论】:
你应该创建一个类,而不是多个字符串数组,然后创建一个该类的列表。 listView.setAdapter((ListAdapter) arrayList);不错的类转换异常.. ListViewAdapterTour(this, arrayList).. 这是一个片段和构造函数 prolly 需要 Context 尝试使用getActivity()
而不是this
出现错误的行。
【参考方案1】:
上下文应该是持有片段的活动。因此,不要在适配器构造函数中使用 this
,而是使用 getActivity()
adapter = new ListViewAdapterTour(getActivity(), arrayList);
【讨论】:
非常感谢,这很有帮助。我的代码没有更多错误。但无法运行。我不知道哪个部分是错误的。我只想在活动片段中添加一个列表视图。在我搜索了互联网但没有人可以之后。以上是关于片段活动中的 ListView 适配器上下文错误,我该怎么办?的主要内容,如果未能解决你的问题,请参考以下文章