在Android中使用edittext和recyclerview

时间:2018-12-07 17:13:03

标签: android android-recyclerview

我希望edittext内容显示在recyclerview上,但它不起作用,这是适配器

package c.androidchatapp.recyclerview.Adapter;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import c.androidchatapp.recyclerview.Model.Book;
import c.androidchatapp.recyclerview.R;

import java.util.ArrayList;
import java.util.List;


public class BookAdapter extends 
RecyclerView.Adapter<BookAdapter.BookViewHolder>{

private  LayoutInflater layoutInflater;
private EditText edittext1;
private Button button1;
public static ArrayList<Book> bookList;
public BookAdapter(Context context, String[] mDataset) {
    layoutInflater = LayoutInflater.from(context);
    bookList = new ArrayList<Book>(mDataset.length);
}
@Override
public BookViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.book_list_row, parent, false);
    return new BookViewHolder(itemView);

}
@Override
public void onBindViewHolder(BookViewHolder holder, int position) {
    holder. editText.setText(bookList.get(position).getEditTextValue());
    Log.d("print","yes");
     }

    @Override
    public int getItemCount() {
    return bookList.size();
}
    public class BookViewHolder extends RecyclerView.ViewHolder {

        protected EditText editText;

        public BookViewHolder(View itemView) {
            super(itemView);

            editText = (EditText) itemView.findViewById(R.id.edittext1);

            editText.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                }

                @Override
                public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                    bookList.get(getAdapterPosition()).setEditTextValue(editText.getText().toString());
                }

                @Override
                public void afterTextChanged(Editable editable) {

                }
            });

    }
} }

这是我用来获取代码的主要活动

package c.androidchatapp.recyclerview;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.support.v7.widget.DefaultItemAnimator;

import android.support.v7.widget.LinearLayoutManager;

import android.support.v7.widget.RecyclerView;
import android.widget.Button;
import android.widget.TextView;
import c.androidchatapp.recyclerview.Adapter.BookAdapter;
import c.androidchatapp.recyclerview.Model.Book;


import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {


private Button button1;
public ArrayList<Book> bookList;
private RecyclerView recyclerView;
private BookAdapter mAdapter;


@Override

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    recyclerView = (RecyclerView) findViewById(R.id.recycler_view);

    button1 = (Button) findViewById(R.id.button1);
    final TextView title = (TextView) findViewById(R.id.title);
    bookList = populateList();

    RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());

    recyclerView.setLayoutManager(mLayoutManager);

    recyclerView.setItemAnimator(new DefaultItemAnimator());

// recyclerView.addItemDecoration(new ItemDividerDecoration(this, LinearLayoutManager.VERTICAL));

    recyclerView.setAdapter(mAdapter);



    initBookData();

}

private ArrayList<Book> populateList() {

        ArrayList<Book> list = new ArrayList<>();

        for(int i = 0; i < 8; i++){
            Book editModel = new Book();
            editModel.setEditTextValue(String.valueOf(i));
            list.add(editModel);
        }

        return list;
    }


private void initBookData() {


}
}

我希望内容显示onclick按钮,但是它不起作用。我尝试了不同的方法,但还是一样。您可以告诉我我的错误在哪里,并给我正确的密码,或者编辑我发布的代码,然后将正确的密码发送给我,以便于更正。

2 个答案:

答案 0 :(得分:0)

在将适配器设置为recyclerview之前,您需要对其进行初始化。

类似

BookAdapter mAdapter = new BookAdapter(this, bookList);

在此行之前

recyclerView.setAdapter(mAdapter);

此外,您需要更改适配器实现,以接收ArrayList<Book>而不是String[]作为第二个参数

答案 1 :(得分:0)

package c.androidchatapp.recyclerview.Adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import c.androidchatapp.recyclerview.Model.Book;
import c.androidchatapp.recyclerview.R;

import java.util.ArrayList;
import java.util.List;

public class BookAdapter extends RecyclerView.Adapter<BookAdapter.BookViewHolder>{

private  LayoutInflater layoutInflater;
private EditText edittext1;
private Button button1;
public static ArrayList<Book> bookList;
public BookAdapter(Context context, ArrayList<Book> mDataset) {
    layoutInflater = LayoutInflater.from(context);
    bookList = new ArrayList<Book>(mDataset);
}
@Override
public BookViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.book_list_row, parent, false);
    return new BookViewHolder(itemView);

}
@Override
public void onBindViewHolder(BookViewHolder holder, int position) {
    holder. editText.setText(bookList.get(position).getEditTextValue());
    Log.d("print","yes");
     }

    @Override
    public int getItemCount() {
    return bookList.size();
}
    public class BookViewHolder extends RecyclerView.ViewHolder {

        protected EditText editText;

        public BookViewHolder(View itemView) {
            super(itemView);

            editText = (EditText) itemView.findViewById(R.id.edittext1);

            editText.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                }

                @Override
                public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                    bookList.get(getAdapterPosition()).setEditTextValue(editText.getText().toString());
                }

                @Override
                public void afterTextChanged(Editable editable) {

                }
            });

    }
} }

这是我的新适配器代码,这也是新的mainactivity代码

package c.androidchatapp.recyclerview;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.support.v7.widget.DefaultItemAnimator;

import android.support.v7.widget.LinearLayoutManager;

import android.support.v7.widget.RecyclerView;
import android.widget.Button;
import android.widget.TextView;
import c.androidchatapp.recyclerview.Adapter.BookAdapter;
import c.androidchatapp.recyclerview.Model.Book;


import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {


private Button button1;
public ArrayList<Book> bookList;
private RecyclerView recyclerView;
private BookAdapter mAdapter;


@Override

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    recyclerView = (RecyclerView) findViewById(R.id.recycler_view);

    button1 = (Button) findViewById(R.id.button1);
    final TextView title = (TextView) findViewById(R.id.title);
    bookList = populateList();

    RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());

    recyclerView.setLayoutManager(mLayoutManager);

    recyclerView.setItemAnimator(new DefaultItemAnimator());

// recyclerView.addItemDecoration(new ItemDividerDecoration(this, LinearLayoutManager.VERTICAL));
BookAdapter mAdapter = new BookAdapter(this, bookList);
    recyclerView.setAdapter(mAdapter);



    initBookData();

}

private ArrayList<Book> populateList() {

        ArrayList<Book> list = new ArrayList<>();

        for(int i = 0; i < 8; i++){
            Book editModel = new Book();
            editModel.setEditTextValue(String.valueOf(i));
            list.add(editModel);
        }

        return list;
    }


private void initBookData() {


}
}

请我需要快速回复