2016-04-27 114 views
0

我正在做一個RecyclerView的例子。問題是,當我在列表中添加第一個項目時,它在recyclerview上正確顯示項目,但我添加的下一個項目未顯示在回收視圖中。回收站查看不刷新

請幫幫我。

適配器代碼:

public class PersonAdapter extends RecyclerView.Adapter<PersonAdapter.PersonViewHolder> { 
private ArrayList<Person> mData; 
LayoutInflater inflater; 

public PersonAdapter() { 
} 

public PersonAdapter(Context context, ArrayList<Person> data) { 
    inflater = LayoutInflater.from(context); 
    update(data); 
} 

public void update(ArrayList<Person> data) { 
    mData = data; 
    notifyDataSetChanged(); 
} 

@Override 
public PersonViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
    View view = inflater.inflate(R.layout.custom_row, parent, false); 
    PersonViewHolder holder = new PersonViewHolder(view); 
    return holder; 
} 

@Override 
public void onBindViewHolder(PersonViewHolder holder, int position) { 
    Person person = mData.get(position); 

    holder.ivImagen.setImageResource(R.drawable.ic_action_android); 
    holder.tvNombre.setText(person.getName()); 
    holder.tvApellido.setText(person.getLast_name()); 
} 

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

public static class PersonViewHolder extends RecyclerView.ViewHolder { 
    ImageView ivImagen; 
    TextView tvNombre; 
    TextView tvApellido; 

    public PersonViewHolder(View itemView) { 
     super(itemView); 
     ivImagen = (ImageView) itemView.findViewById(R.id.cr_iv_imagen); 
     tvNombre = (TextView) itemView.findViewById(R.id.cr_tv_name); 
     tvApellido = (TextView) itemView.findViewById(R.id.cr_tv_last_name); 
    } 
}} 

主要活動代碼是:

public class MainActivity extends AppCompatActivity implements View.OnClickListener { 
ArrayList<Person> persons = new ArrayList<>(); 
EditText et_name; 
EditText et_last_name; 
RecyclerView rv_names; 
PersonAdapter adapter; 
Button btAgregar; 

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

    et_name = (EditText) findViewById(R.id.tv_name); 
    et_last_name = (EditText) findViewById(R.id.tv_last_name); 
    btAgregar = (Button) findViewById(R.id.bt_agregar); 
    rv_names = (RecyclerView) findViewById(R.id.rv_nombres); 

    LinearLayoutManager manager = new LinearLayoutManager(this); 
    rv_names.setLayoutManager(manager); 
    adapter = new PersonAdapter(this, persons); 
    rv_names.setAdapter(adapter); 

    btAgregar.setOnClickListener(this); 
} 

@Override 
public void onClick(View v) { 
    Person person = new Person(et_name.getText().toString(), et_last_name.getText().toString()); 
    persons.add(person); 

    Toast.makeText(this, "Items: " + String.valueOf(persons.size()), Toast.LENGTH_SHORT).show(); 
} } 
+1

添加XML代碼也。有時發生在設置'RecycleView'高度'wrap_content' –

+0

@Zahidul在上次支持包更新中未解決此問題? – yennsarah

回答

1

試試這個代碼onClick功能

@Override 
public void onClick(View v) { 
Person person = new Person(et_name.getText().toString(), et_last_name.getText().toString()); 
persons.add(person); 

adapter.update(persons); 

Toast.makeText(this, "Items: " + String.valueOf(persons.size()), Toast.LENGTH_SHORT).show(); 
} 

UPDATE:

在適配器代碼,請嘗試以下變化..

public void update(ArrayList<Person> data) { 
mData.clear(); 
mData.addAll(data) 
notifyDataSetChanged(); 
} 
+0

Hello Fabin我添加了行:adapter.update(個人);但不工作:(我添加第一個項目後沒有顯示在Recycler視圖:( – Hugo

+0

@Hugo我已經更新了答案。 –

+0

感謝Fabin。 ! – Hugo

1

在onclick要添加一個人,但你是不是通過調用傳遞更新列表到適配器適配器的更新方法。這可能是原因嗎?

+0

正是這個原因 –

+0

你好傑拉爾多。我在添加項目後在onClick中添加了更新方法,但不工作:(onClick代碼現在是:Person person = new Person(et_name.getText()。toString(),et_last_name.getText()。toString()); persons.add(person); adapter.update(persons); Toast.makeText(this,「Items:」+ String.valueOf(persons.size()),Toast.LENGTH_SHORT).show(); – Hugo