Java 片段活动上的ListViewAdapter上下文错误,我应该怎么做?
安卓大师晚安。请帮帮我。也许你们中比我更了解的人可以帮助我解决我现在面临的问题 我在片段上做了一个列表视图。我停了下来,因为我不知道该怎么做。也许下面的代码可以帮助你解决我的问题 这是我做的碎片。我把它命名为Frag_Tour.javaJava 片段活动上的ListViewAdapter上下文错误,我应该怎么做?,java,android,listview,android-fragments,Java,Android,Listview,Android Fragments,安卓大师晚安。请帮帮我。也许你们中比我更了解的人可以帮助我解决我现在面临的问题 我在片段上做了一个列表视图。我停了下来,因为我不知道该怎么做。也许下面的代码可以帮助你解决我的问题 这是我做的碎片。我把它命名为Frag_Tour.java public class Frag_Tour extends Fragment { ListView listView; ListViewAdapterTour adapter; String[] judul1; String
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;
}
}
我希望安卓的老师和大师们能帮助我解决这个问题。我想要的是row_tour.xml,我将它设计为frag_tour.xml中的listview
<?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_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
[4] 上下文应该是包含片段的活动。因此,在适配器构造函数中使用
getActivity()
您应该创建一个类,然后创建该类的列表,而不是多个字符串数组。listView.setAdapter((ListAdapter)arrayList);很好的类强制转换异常。。ListViewAdapterTour(此,arrayList)。。这是一个片段,构造函数通常希望ContextTry在出现错误的地方使用
getActivity()
而不是this
。非常感谢,这非常有用。我的代码没有更多的错误。但是不能跑。我不知道哪一部分是错误的。我只想向活动片段添加一个listview。我在网上搜索了一下,但是没有人能找到。
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 version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
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_width="match_parent"
android:layout_height="0dp"
android:layout_margin="@dimen/spacing_middle"
android:layout_weight="1"
app:cardCornerRadius="2dp"
app:cardElevation="2dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="170dp"
android:orientation="vertical">
<ImageView
android:id="@+id/GambarTour"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
android:src="@drawable/mercusuar_1"
tools:ignore="ContentDescription" />
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/overlay_dark_20" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
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_width="match_parent"
android:layout_height="wrap_content"
android:text="Paket Hemat"
android:textColor="@color/White"
android:textStyle="bold" />
<TextView
android:id="@+id/judul2"
style="@style/TextAppearance.AppCompat.Large"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Wisata Belitung"
android:textColor="@color/White"
android:textStyle="bold" />
<TextView
android:id="@+id/durasi"
style="@style/TextAppearance.AppCompat.Caption"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="@string/_3_hari_2_malam"
android:textColor="@color/White" />
</LinearLayout>
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
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_width="wrap_content"
android:layout_height="wrap_content"
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>
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;
}
}
adapter = new ListViewAdapterTour(getActivity(), arrayList);