Recycler View不能正确显示文本

时间:2017-04-27 14:19:10

标签: android android-recyclerview

我正在尝试在我的Android应用中实现回收站视图。要显示的数据来自服务器,数据包括也应显示的时间戳。

使用recycleler视图设置数据的代码如下:

try{
                JSONArray jsonArray = new JSONArray(response);
                if(jsonArray.length()!=0) {
                    TimeStamp = new String[jsonArray.length()];

                    for(int i=0;i<jsonArray.length();i++){
                        JSONObject jsonObject = jsonArray.getJSONObject(i);
                        TimeStamp[i] = jsonObject.getString("TimeStamp1");
                        int ij = TimeStamp[i].indexOf('T');
                        String s = TimeStamp[i].substring(0,ij-1);
                        String s2 = TimeStamp[i].substring(ij+1,TimeStamp[i].length()-1);
                        sb.append(s+"\n"+s2+"\n");
                        String mod = s+"\n"+s2+"\n";
                        dataset.add(new DataEntryModel(1,mod,10,1));
                    }
                    CustomAdapter myAdapter = new CustomAdapter(dataset);
                    recyclerView.setAdapter(myAdapter);
                    resultView.setText(sb.toString());
                }else{

                }
            }catch (Exception e){
                sb.append(e.toString());
                resultView.setText(sb.toString());
            }

ScreenGrab

正如您在屏幕截图中看到的那样,回收器视图未显示完整字符串。我在按钮上方的textview中显示了字符串。这似乎工作正常。是什么导致回收者视图无法正确呈现项目。如果我首先尝试渲染s2,那么显示第二个:的时间。

卡的布局:

<?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="wrap_content">
    <android.support.v7.widget.CardView
        android:id="@+id/card_view"
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dp"
        android:clickable="true"
        card_view:cardElevation="1dp"
        card_view:cardUseCompatPadding="true">
    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        xmlns:android="http://schemas.android.com/apk/res/android">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/textBlack"
        android:text="UserName"
        android:id="@+id/userNametext"
        android:layout_alignBaseline="@+id/userName"
        android:layout_alignBottom="@+id/userName"
        android:layout_alignParentStart="true"
        android:layout_marginStart="15dp" />
        <TextView
            android:text="Loc of userName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="75dp"
            android:textColor="@color/textBlack"
            android:id="@+id/userName"
            android:layout_toLeftOf="@+id/userNametext"
            android:layout_marginTop="8dp"
            android:layout_alignParentTop="true"
            android:layout_alignParentEnd="true" />


    <TextView
        android:text="Time Of Entry"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/Time_of_Entry"
        android:layout_marginStart="15dp"
        android:textColor="@color/textBlack"
        android:layout_below="@+id/textView"
        android:layout_alignStart="@+id/textView"
        android:layout_marginTop="50dp" />

    <TextView
        android:text="Loc of Time"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:textColor="@color/textBlack"
        android:id="@+id/timestamp_of_user"
        android:layout_alignBaseline="@+id/Time_of_Entry"
        android:layout_alignBottom="@+id/Time_of_Entry"
        android:layout_alignStart="@+id/userName"
        android:layout_marginStart="14dp" />


</RelativeLayout>

    </android.support.v7.widget.CardView>

</LinearLayout>

回收者视图的适配器是

public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.MyViewHolder> {
    ArrayList<DataEntryModel> dataSet;
    CardView cards;
    public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
        TextView UserName, TimeStamp;
        public MyViewHolder(View itemView){
            super(itemView);
            this.UserName = (TextView)itemView.findViewById(R.id.userName);
            TimeStamp = (TextView)itemView.findViewById(R.id.timestamp_of_user);
            cards = (CardView)itemView.findViewById(R.id.card_view);
        }

        @Override
        public void onClick(View view) {

        }
    }
    public CustomAdapter(ArrayList<DataEntryModel> data){
        dataSet = data;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_fragment,parent,false);
        MyViewHolder myViewHolder = new MyViewHolder(view);
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        TextView userName = holder.UserName;
        TextView TimeStamp = holder.TimeStamp;
        userName.setText(SingletonDataClass.SharedPrefsUserNameForSession);
        TimeStamp.setText(dataSet.get(position).getTimeStamp());
    }

    @Override
    public int getItemCount() {
        return dataSet.size();
    }
}

回收者视图的奇怪行为是什么原因?我该如何解决这个问题?

0 个答案:

没有答案
相关问题