2017-08-26 68 views
3

我想要建立一個簡單的使用RecyclerView與自定義適配器do列表應用程序。我已經構建了一個模型數據類來獲取和設置我的待辦事項。我的問題是,每次我點擊按鈕添加我的項目,什麼也沒有發生。我確實打電話給notifyDataSetChanged()方法,但它仍然不起作用。如何通過onClick將用戶數據添加到我的Recyclerview?

這是我建立學習RecyclerViews和自定義適配器,所以我會很感激,如果有人可以解釋爲什麼它不工作,也許我解釋一下我該如何解決實踐應用。

這是我的代碼,在那裏我檢索來自外地的EditText用戶輸入並把它放在我的數據:

public class MainActivity extends AppCompatActivity { 

     private ToDoData toDoData; 

     private List<ToDoData> toDoList = new ArrayList<>(); 

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

      final ToDoAdapter toDoAdapter = new ToDoAdapter(toDoList, this); 
      toDoData = new ToDoData(this); 

      final EditText toDoInput = (EditText)findViewById(R.id.add_todo); 
      Button toDoAdd = (Button)findViewById(R.id.add_item); 

      RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this); 
      RecyclerView toDoDisplay = (RecyclerView) findViewById(R.id.toDoDisplayRecyclerView); 
      toDoDisplay.setAdapter(toDoAdapter); 
      toDoDisplay.setHasFixedSize(true); 
      toDoDisplay.setLayoutManager(layoutManager); 


      toDoAdd.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View view) { 
        toDoData.setToDo(toDoInput.getText().toString()); 
        toDoAdapter.notifyDataSetChanged(); 
       } 
      }); 

     } 
    } 

這是getter和setter方法我的模型類。該列表項目只有1個字段,因此它相當小:

public class ToDoData { 
    private String toDoString; 

    public ToDoData(String todoString){ 
     this.toDoString = todoString; 
    } 

    public ToDoData(Context context){} 

    public String getToDo() { 
     return toDoString; 
    } 

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

而這是我的自定義適配器。我也跟着通過一個教程,同時建立這一點,但然而,我認爲我的問題來自適配器莖:

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

    private List<ToDoData> toDoList; 
    private Context context; 

    public ToDoAdapter(List<ToDoData> todoList, Context context) { 
     this.toDoList = todoList; 
     this.context = context; 
    } 

    public class ViewHolder extends RecyclerView.ViewHolder { 
     TextView todoView; 

     public ViewHolder(View itemView) { 
      super(itemView); 
      todoView = (TextView) itemView.findViewById(R.id.to_do_display); 
     } 
    } 

    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.to_do_list_item, parent, false); 
     return new ViewHolder(view); 
    } 

    @Override 
    public void onBindViewHolder(ViewHolder holder, int position) { 
     ToDoData todo = toDoList.get(position); 

     holder.todoView.setText(todo.getToDo()); 
    } 

    @Override 
    public int getItemCount() { 
     return (toDoList == null) ? 0 : toDoList.size(); 
    } 
} 
+2

您是如何將數據添加到'toDoList'?你在哪裏調用適配器上的'notifyDataSetChanged()'?從您的代碼中,您已將空的'toDoList'傳遞給適配器! –

+0

@AtefHares對不起更正*我在我的按鈕onClick上調用了'notifyDataSetChanged',但它不起作用。 –

+2

你的'toDoList'是空的它沒有任何項目,因爲你沒有使用'toDoList.add(toDoData)',因此'notifyDataSetChanged()'沒有效果! –

回答

2

在適配器類添加一個setter方法來設置集合,然後調用notifyDataSetChanged

聲明ToDoAdapter爲類的成員,然後調用上Button點擊setter函數如下,

adapter.setTodoList(/* pass collection */); 
adapter.notifyDataSetchanged(); 
+0

有點兒糊塗集合的一部分。我從我的MainActivity中檢索用戶輸入。我怎麼能在適配器中做到這一點,爲什麼? –

+1

檢查適配器實現,在適配器初始化時傳遞集合。那麼你將如何從活動更新適配器內的集合? – young

+0

「你將如何更新從活動適配器內部集」 - 嗯,名單是活動的字段(成員變量),所以它是沒有問題的一個項目中的onClick添加到這個列表()。適配器保持在列表中,所以對「活動」列表的更改是對適配器列表的更改。 – 0X0nosugar

相關問題