使用EditText字符串动态膨胀RecyclerView listItem

时间:2017-07-26 23:51:00

标签: android android-recyclerview

我正在尝试学习如何使用RecyclerViews,最好的学习方法就是这样做:)所以我试图从使用简单待办事项列表中的ListView过渡到Recyclerview。我正在尝试从EditText获取String值,并在单击按钮时将该值添加到我的RecyclerView列表中。以下是我的实现。

这是我的自定义适配器。我正在使用ViewHolder来使用基本的单个TextView自定义列表项。

public class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.ViewHolder>{

    private List<todo> todoList;

    public class ViewHolder extends RecyclerView.ViewHolder{
        public TextView toDoTextView;

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

            toDoTextView = (TextView)itemView.findViewById(R.id.to_do);
        }
    }

    public ItemAdapter(List<todo> todoList){
        this.todoList = todoList;
    }

    @Override
    public ItemAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);
        return new ViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(ItemAdapter.ViewHolder holder, int position) {
        todo toDo = todoList.get(position);
        holder.toDoTextView.setText(toDo.getToDo());
    }


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

这是我的MainActivity。这是我要将EditText值添加到RecyclerView List的地方。实施在setOnClickListener中完成,但它给我一个错误。

public class MainActivity extends AppCompatActivity {                                                                   

    private List<todo> items = new ArrayList<>();                                                                       
    private ItemAdapter itemAdapter;                                                                                    
    private RecyclerView listItemsRecyclerView;                                                                         
    EditText itemsInput;                                                                                                
    Button addingItems;                                                                                                 

    @Override                                                                                                           
    protected void onCreate(Bundle savedInstanceState) {                                                                
        super.onCreate(savedInstanceState);                                                                             
        setContentView(R.layout.activity_main);                                                                         

        itemsInput = (EditText)findViewById(R.id.to_do_editText);                                                       
        addingItems = (Button)findViewById(R.id.to_do_btn);                                                             
        listItemsRecyclerView = (RecyclerView) findViewById(R.id.to_do_list);                                           

        final String todoItem = String.valueOf(itemsInput.getText());                                                   

        itemAdapter = new ItemAdapter(items);                                                                           

        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());                    
        listItemsRecyclerView.setLayoutManager(layoutManager);                                                          
        listItemsRecyclerView.setItemAnimator(new DefaultItemAnimator());                                               
        listItemsRecyclerView.setAdapter(itemAdapter);                                                                  

        addingItems.setOnClickListener(new View.OnClickListener() {                                                     
            @Override                                                                                                   
            public void onClick(View view) {                                                                            
                if(todoItem != null){                                                                                   
                    items.add(todoItem);                                                                                
                    itemsInput.setText("");                                                                             
                }else {                                                                                                 
                    Toast.makeText(MainActivity.this, "Please enter something to do", Toast.LENGTH_SHORT).show();       
                }                                                                                                       
                itemAdapter.notifyDataSetChanged();                                                                     
            }                                                                                                           
        });                                               

这是我的待办班级:

public class todo {
    private String toDo;

    public todo(){}

    public todo(String somethingToDo){
        somethingToDo = toDo;
    }

    public String getToDo() {
        return toDo;
    }

    public void setToDo(String toDo) {
        this.toDo = toDo;
    }
}

1 个答案:

答案 0 :(得分:0)

如果条件在按钮OnClickListener

内,则修改你的代码
if(todoItem != null){                      
    todo mTodo = new todo(todoItem); //Create object of todo using the input done by user.                                             
    items.add(mTodo);                                                                                
    itemsInput.setText("");                                                                             
}

你的todo类构造函数代码也是错误的

public todo(String somethingToDo){
        //somethingToDo = toDo;
       toDo = somethingToDo; //need to assign the value received as signature to the class variable.
}