将ListView添加到片段时出错

吉尔伯特·李

我正在尝试将listview添加到现有的android片段。我为列表视图中的项目制作了一个布局xml文件,修改了先前的fragment.xml,编写了一个新的arrayadapter,并向fragment.java中添加了一些内容。但是,每当我在fragment.java中调用notifyDataSetChanged()方法时,都会出现错误:android.content.res.Resources $ NotFoundException:字符串资源ID#0x0,然后应用程序崩溃。请在下面找到我的代码。

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

<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

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);}

} 

片段

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();
        }
    }
}   
激肽

确保checkInRec.providerNamecheckInRec.checkInDatecheckInRec.providerPoints值在设置为文本的TextView有型的String,它投射到String,如果它的整数。

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

UPD:

看来ListView位于标题视图的后面。尝试使用属性fragment.xmlfrom的根视图更改FrameLayout像这样:LinearLayoutandroid:orientation="vertical"

<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>

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用ArrayAdapter将新数据添加到ListView时出错

来自分类Dev

将片段添加到ViewPager时,活动已被破坏

来自分类Dev

将新片段添加到ViewPager时发生NullPointerException

来自分类Dev

尝试将parserHook添加到MediaWiki时出错

来自分类Dev

将补丁添加到补丁集时出错

来自分类Dev

将add_header添加到nginx时出错

来自分类Dev

尝试将android平台添加到Cordova时出错

来自分类Dev

为什么在将变量添加到ArrayList时出错?

来自分类Dev

将tableadapter添加到数据集时出错

来自分类Dev

将CSRF令牌添加到Laravel表单时出错

来自分类Dev

将CardView添加到布局时出错

来自分类Dev

我将library()添加到if函数时出错

来自分类Dev

将日期添加到数据库时出错

来自分类Dev

将JPA存储库添加到Spring时出错

来自分类Dev

将Google登录添加到网站时出错

来自分类Dev

将tableadapter添加到数据集时出错

来自分类Dev

将CardView添加到布局时出错

来自分类Dev

将枚举类型添加到Swift数组时出错

来自分类Dev

尝试将parserHook添加到MediaWiki时出错

来自分类Dev

尝试将视图添加到RelativeLayout时出错

来自分类Dev

将事件列表添加到画布时出错

来自分类Dev

将CoreData添加到现有项目时出错

来自分类Dev

将add_header添加到nginx时出错

来自分类Dev

为什么在将变量添加到ArrayList时出错?

来自分类Dev

将NSEntity添加到NSOrderedSet时出错

来自分类Dev

将Facebook SDK添加到android项目时出错

来自分类Dev

将GameObject添加到类中的列表时出错

来自分类Dev

将Google登录添加到网站时出错

来自分类Dev

将FIRST记录添加到表时出错