android中的recyclerview适配器

时间:2017-02-01 17:34:05

标签: android android-recyclerview android-adapter

目前我正在开发一个Android项目。 我有两个String [],现在我想在我的activity上显示字符串[]的数据。但是我的代码只能显示一个String []的数据。 请检查我的代码。

文件名:MyAdapter4.java

    public class MyAdapter4 extends RecyclerView.Adapter<MyAdapter4.MyViewHolder> {
    private String[] mDataset;


    // Provide a reference to the views for each data item
    // Complex data items may need more than one view per item, and
    // you provide access to all the views for a data item in a view holder
    public static class MyViewHolder extends RecyclerView.ViewHolder {
        public CardView cardview4;
        public EditText edit4;
        public TextView title4;
        public MyViewHolder(View v) {
            super(v);


            cardview4=(CardView) v.findViewById(R.id.card_view4);
            edit4=(EditText) v.findViewById(R.id.otherEdit);
            title4=(TextView) v.findViewById(R.id.otherTitle);
        }
    }

    // Provide a suitable constructor (depends on the kind of dataset)
    public MyAdapter4(String[] myDataset) {
        mDataset = myDataset;
    }

    // Create new views (invoked by the layout manager)
    @Override
    public MyAdapter4.MyViewHolder onCreateViewHolder(ViewGroup parent,
                                                      int viewType) {
        // create a new view
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.other_file_layout, parent, false);
        // set the view's size, margins, paddings and layout parameters
        MyAdapter4.MyViewHolder vh = new MyAdapter4.MyViewHolder(v);
        return vh;
    }

    @Override
    public void onBindViewHolder(MyAdapter4.MyViewHolder holder, int position) {
        holder.title4.setText(mDataset[position]);
    }

    @Override
    public int getItemCount() {
        return mDataset.length;
    }
}

使用此代码我只能显示Textview ....但editText不显示数据

这是我用来附加适配器的代码

public class FourFragment extends Fragment {
    public static String[] arrayOtherTitle, arrayOtherData;

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

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

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_four, container, false);

        RecyclerView rv = (RecyclerView) rootView.findViewById(R.id.rv_recycler_view4);
        rv.setHasFixedSize(true);
        MyAdapter2 adapter = new MyAdapter2(arrayOtherTitle);
        rv.setAdapter(adapter);

        LinearLayoutManager llm = new LinearLayoutManager(getActivity());
        rv.setLayoutManager(llm);

        return rootView;
    }
}

布局文件:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_margin="4dp"
    android:elevation="4dp"
    android:id="@+id/card_view4"
    android:layout_height="wrap_content">
    <LinearLayout
        android:layout_margin="16dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp"
            android:orientation="horizontal">
            <TextView
                android:layout_width="0dp"
                android:textStyle="bold"
                android:layout_weight="1"
                android:id="@+id/otherTitle"
                android:layout_height="wrap_content"
                />
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/copy"
                android:layout_margin="4dp"/>
        </LinearLayout>

        <EditText
            android:layout_width="match_parent"
            android:id="@+id/otherEdit"
            android:enabled="false"
            android:layout_height="wrap_content"
            android:background="@android:color/transparent"
            />

    </LinearLayout>


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

如你所见, 1)在我的布局文件中,有一个textview和一个edittext。 2)在类中,FourFragment.java有两个String [] arrayOtherTitle和arrayOtherData。 3)所以,我想在我的活动上显示它们。 textview将包含arrayOtherTitle的数据, 和editext将包含arrayOtherData的数据。

但是根据我的代码,目前我只能在textview上显示arrayOtherTitle的数据。 我是这个主题的新手,PLZ任何人都帮我在edittext上显示其他arrayOtherData数据。

0 个答案:

没有答案
相关问题