使用数组的Fragment中的Android ListView

时间:2016-12-10 14:22:45

标签: android listview android-fragments

我正在尝试在我的片段中实现listview,但是我的片段类和适配器中出现错误。

我的片段xml:

        <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/divider2">

        <ListView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/MyAccountList"
            android:layout_below="@+id/divider2"/>

    </LinearLayout>

我的列表浏览器:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">


<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- icon -->
    <ImageView
        android:id="@+id/option_icon"
        android:layout_width="64dp"
        android:layout_height="64dp"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="@dimen/activity_horizontal_margin"
        android:layout_marginRight="@dimen/activity_horizontal_margin"
        android:layout_marginTop="@dimen/activity_horizontal_margin"
        android:layout_marginBottom="@dimen/activity_horizontal_margin"
        android:padding="@dimen/activity_horizontal_margin"
        android:contentDescription="icon" />


    <!-- title -->
    <TextView
        android:id="@+id/option_title"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/option_icon"
        android:textColor="@android:color/darker_gray"
        android:padding="@dimen/activity_horizontal_margin"
        android:layout_marginTop="@dimen/activity_horizontal_margin"
        android:layout_alignTop="@+id/option_icon"
        android:layout_centerVertical="true"/>

    </RelativeLayout>

适配器:

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;


public class MyAccount_Adapter extends BaseAdapter {


private final Activity context;
private final String[] option_title;
private final Integer[] option_icon;


public MyAccount_Adapter(Activity context, String[] option_title, Integer[] option_icon) {
    //super(context, R.layout.myaccount_options_viewholder, option_title);
    // TODO Auto-generated constructor stub

    this.context=context;
    this.option_title=option_title;
    this.option_icon=option_icon;
}

@Override
public int getCount() {
    return option_title.length; //returns total of items in the array
}

@Override
public Object getItem(int position) {
    return option_title[position]; //returns item at the specified position
}

@Override
public long getItemId(int position) {
    return position;
}

public View getView(int position, View view, ViewGroup parent) {
    LayoutInflater inflater=context.getLayoutInflater();
    View rowView=inflater.inflate(R.layout.myaccount_options_viewholder, null,true);

    TextView txtTitle = (TextView) rowView.findViewById(R.id.option_title);
    ImageView imageView = (ImageView) rowView.findViewById(R.id.profile_icon);

    txtTitle.setText(option_title[position]);
    imageView.setImageResource(option_icon[position]);
    return rowView;

};
}

我的片段:

public class MyAccount extends Fragment {

ListView listView;
String[] optionTitle ={
        "booking record",
        "comments",
        "language",
};

Integer[] option_icon={

        R.drawable.booking_record_02,
        R.drawable.comment_record_02,
        R.drawable.language_02,
};


public MyAccount() {
}

public static MyAccount newInstance(String text){
    Bundle args = new Bundle();
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

final View rootView = inflater.inflate(R.layout.my_account, container, false);

    MyAccount_Adapter adapter=new MyAccount_Adapter(getActivity(), optionTitle, option_icon);
    listView=(ListView)getActivity().findViewById(R.id.MyAccountList);
    listView.setAdapter(adapter);

return rootView;

}

我收到错误fragmentpageradapter无法在我的适配器中应用:

super(context, R.layout.myaccount_options_viewholder, option_title);

并且无法应用另一个错误MyAccount_Adapter,并且无法在我的片段中应用适配器:

MyAccount_Adapter adapter=new MyAccount_Adapter(this, optionTitle, option_icon);
listView.setAdapter(adapter);

这是由上下文引起的吗?我不确定如何解决它。

2 个答案:

答案 0 :(得分:2)

  • 您无法将FragmentPagerAdapter设置为ListView

    因为ListView中的setAdapter()需要ListAdapter(实现ListAdapter的适配器)作为参数。

    您可以使用BaseAdapter

  • 您无法将this作为适配器的上下文传递。

    因为片段中的this引用了该片段,而不是活动。

    因此,您需要通过getActivity()

示例适配器:

public class MyAccount_Adapter extends BaseAdapter {

private final Activity context;
private final String[] option_title;
private final Integer[] option_icon;
public MyAccount_Adapter(Activity context, String[] option_title, Integer[] option_icon) {

    this.context=context;
    this.option_title=option_title;
    this.option_icon=option_icon;
}

@Override
public int getCount() {
    return option_title.length; //returns total of items in the array
}

@Override
public Object getItem(int position) {
    return option_title[position]; //returns item at the specified position
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View view, ViewGroup parent) {
    LayoutInflater inflater=context.getLayoutInflater();
    View rowView=inflater.inflate(R.layout.myaccount_options_viewholder, null,true);

    TextView txtTitle = (TextView) rowView.findViewById(R.id.option_title);
    ImageView imageView = (ImageView) rowView.findViewById(R.id.profile_icon);

    txtTitle.setText(option_title[position]);
    imageView.setImageResource(option_icon[position]);
    return rowView;

};
}

有关优化,请参阅此链接:Using a BaseAdapter with ListView

<强>更新

您只能从listView获得rootView,显然是因为它存在于此{&};无法使用getActivity()

从您的活动中获取
listView=(ListView) rootView.findViewById(R.id.MyAccountList);

答案 1 :(得分:0)

试试此代码

MyAccount_Adapter adapter = new MyAccount_Adapter(getActivity(),optionTitle,option_icon); listView.setAdapter(适配器);