简体   繁体   English

将ListView添加到片段时出错

[英]Errors on Adding ListView to Fragment

I am trying to add a listview to an existing android fragment. 我正在尝试将listview添加到现有的android片段。 I made a layout xml file for items in the listview, modified the previous fragment.xml, wrote a new arrayadapter and added something to the fragment.java. 我为列表视图中的项目制作了一个布局xml文件,修改了先前的fragment.xml,编写了一个新的arrayadapter并将一些内容添加到fragment.java中。 But whenever I call the notifyDataSetChanged() method in the fragment.java, I got an error: android.content.res.Resources$NotFoundException: String resource ID #0x0, and then the app crashes. 但是,每当我在fragment.java中调用notifyDataSetChanged()方法时,都会出现错误:android.content.res.Resources $ NotFoundException:字符串资源ID#0x0,然后应用程序崩溃。 Below please find my codes. 请在下面找到我的代码。

layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="7dp">

    <ImageView
        android:layout_width="72dp"
        android:layout_height="72dp"
        android:id="@+id/iv_checkIcon"
        android:layout_margin="7dp"
        android:background="#EEEEEE"
        android:contentDescription="Provider icon"
        />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_toRightOf="@id/iv_checkIcon"
        android:layout_marginTop="4dp"
        android:layout_marginBottom="4dp"
        android:layout_marginLeft="5dp"
        >


        <TextView
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:id="@+id/tv_checkName"
            android:text="Provider name"
            android:textSize="20dp"
            />

        <TextView
            android:layout_width="250dp"
            android:layout_height="wrap_content"
            android:id="@+id/tv_checkInDate"
            android:text="Check In Date"
            android:textSize="14dp"
            android:textColor="#888888"
            android:layout_marginTop="4dp"/>

    </LinearLayout>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tv_checkPoints"
        android:layout_alignParentRight="true"
        android:text="100分"
        android:layout_marginTop="10dp"
        android:layout_marginRight="10dp"
        android:textSize="18dp"
        android:textColor="#CF1A12"
        />

</RelativeLayout>

fragment.xml fragment.xml之

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".fragment.MeActivity">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/me_bg2"
        >

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/fake_head"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="40dp"
            android:id="@+id/iv_fake_head"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_below="@id/iv_fake_head"
            android:text="--"
            android:textSize="22dp"
            android:textColor="#ffffff"
            android:layout_marginTop="15dp"
            android:id="@+id/tv_name"
            />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_below="@id/tv_name"
            android:text="积分"
            android:textColor="#ffffff"
            android:layout_marginTop="15dp"
            android:textSize="20dp"
            android:background="@drawable/me_points_bg"
            android:id="@+id/tv_points"
            />

    </RelativeLayout>

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/lv_histories"
        android:layout_gravity="bottom" />

</FrameLayout>

Arrayadapter.java Arrayadapter.java

public class RecordArrayAdapter extends ArrayAdapter<CheckInRecord.CheckInRec> {
    private int resourceId;
    private Context context;
    private List<CheckInRecord.CheckInRec> checkInRecList;

    public RecordArrayAdapter(Context context, int resourceId, List<CheckInRecord.CheckInRec> checkInRecList)
    {
        super(context, resourceId, checkInRecList);
        this.resourceId = resourceId;
        this.context = context;
        this.checkInRecList = checkInRecList;
    }

    public View getView(int position, View convertView, ViewGroup parent)
    {
        if (convertView == null){
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            convertView = inflater.inflate(resourceId, parent, false);
        }

        TextView textViewName = (TextView) convertView.findViewById(R.id.tv_checkName);
        TextView textViewCheckInDate = (TextView) convertView.findViewById(R.id.tv_checkInDate);
        TextView textViewPoints = (TextView) convertView.findViewById(R.id.tv_checkPoints);
        ImageView imageViewIcon = (ImageView) convertView.findViewById(R.id.iv_checkIcon);

        CheckInRecord.CheckInRec checkInRec = checkInRecList.get(position);
        textViewName.setText(checkInRec.providerName);
        textViewCheckInDate.setText(checkInRec.checkInDate);
        textViewPoints.setText(checkInRec.providerPoints);
        ImageLoader.getInstance().displayImage(checkInRec.providerIcon, imageViewIcon, Utility.displayImageOptions);

        return convertView;
    }

    public int getIsPrize(int position) {return (this.checkInRecList.get(position).isPrize);}

} 

fragment.java fragment.java

public class MeFragment extends Fragment implements ApiRequestDelegate {


    private TextView textViewName;
    private TextView textViewPoints;
    private ProgressDialog progressDialog;

    private RecordArrayAdapter recordArrayAdapter;
    private List<CheckInRecord.CheckInRec> checkInRecList = new ArrayList<CheckInRecord.CheckInRec>();


    public MeFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ApiManager.getInstance().checkInHistories(AppDataManager.getInstance().getUserToken(), AppDataManager.getInstance().getUserPhone(),
                Utility.getPictureSize(), this);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View fragmentView =  inflater.inflate(R.layout.fragment_me, container, false);

        textViewName = (TextView) fragmentView.findViewById(R.id.tv_name);
        textViewPoints = (TextView) fragmentView.findViewById(R.id.tv_points);

        ListView listViewRec = (ListView) fragmentView.findViewById(R.id.lv_histories);

        recordArrayAdapter = new RecordArrayAdapter(this.getActivity(), R.layout.row_record, checkInRecList);
        listViewRec.setAdapter(recordArrayAdapter);

        return fragmentView;
    }

    @Override
    public void apiCompleted(ApiResult apiResult, HttpRequest httpRequest) {
        if (progressDialog!=null){
            progressDialog.dismiss();
        }

        if (!apiResult.success){
            ApiManager.handleMessageForReason(apiResult.failReason, getActivity());
            return;
        }

        CheckInRecord checkInRecord = (CheckInRecord) apiResult.valueObject;
        if (checkInRecord != null){
            textViewName.setText(checkInRecord.userName);
            textViewPoints.setText(String.format("积分%d分", checkInRecord.userPoints));
            this.checkInRecList.clear();
            this.checkInRecList.addAll(checkInRecord.checkInRecList);

            recordArrayAdapter.notifyDataSetChanged();
        }
    }
}   

Make sure checkInRec.providerName , checkInRec.checkInDate and checkInRec.providerPoints values that you set as TextView text have a type of String and cast it to String if it's integer. 确保您设置为TextView文本的checkInRec.providerNamecheckInRec.checkInDatecheckInRec.providerPoints值具有String类型,如果为整数则将其checkInRec.checkInDate checkInRec.providerPointsString

// cast to String if checkInRec.checkInDate is integer
textViewPoints.setText(String.valueOf(checkInRec.providerPoints));

UPD: UPD:

It seems ListView is located behind header view. 看来ListView位于标题视图的后面。 Try to change root view of fragment.xml from FrameLayout to LinearLayout with android:orientation="vertical" attribute. 尝试使用android:orientation="vertical"属性将fragment.xml根视图从FrameLayout更改为LinearLayout Like this: 像这样:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
             xmlns:tools="http://schemas.android.com/tools"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:orientation="vertical"
             tools:context=".fragment.MeActivity">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/me_bg2"
        >

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/fake_head"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="40dp"
            android:id="@+id/iv_fake_head"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_below="@id/iv_fake_head"
            android:text="--"
            android:textSize="22dp"
            android:textColor="#ffffff"
            android:layout_marginTop="15dp"
            android:id="@+id/tv_name"
            />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_below="@id/tv_name"
            android:text="积分"
            android:textColor="#ffffff"
            android:layout_marginTop="15dp"
            android:textSize="20dp"
            android:background="@drawable/me_points_bg"
            android:id="@+id/tv_points"
            />

    </RelativeLayout>

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/lv_histories"
        android:layout_gravity="bottom" />

</LinearLayout>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM