2016-07-04 22 views
2

我有我的ChordsListActivity,看起来像这样:的Android - ArrayList和RecycleView.Adapter

public class ChordsListActivity extends Activity { 
private RecyclerView chordsList; 
private RecyclerView.LayoutManager listLayoutManager; 

private ArrayList<Accordo> getChords() { 
    ArrayList<Accordo> chords = new ArrayList<>(); 

    Accordo a=new Accordo(); 
    a.setName("Do maggiore"); 
    a.setNote("Do, Mi, Sol"); 
    a.setImage(R.drawable.do_maggiore); 
    chords.add(a); 

    a=new Accordo(); 
    a.setName("do 5"); 
    a.setNote("na, na, na"); 
    a.setImage(R.drawable.do5); 
    chords.add(a); 

    return chords; 
} 

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

    chordsList = (RecyclerView) findViewById(R.id.chords_recycler); 

    //use a linear layout manager 
    listLayoutManager = new LinearLayoutManager(this); 
    chordsList.setLayoutManager(listLayoutManager); 

    ChordsListAdapter adapter = new ChordsListAdapter(this, getChords()); 

    /** start SearchActivity when ImageButton is pressed */ 
    ImageButton cerca = (ImageButton) findViewById(R.id.search); 
    cerca.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Intent intent = new Intent(getApplicationContext(), SearchActivity.class); 

      ArrayList<Accordo> chords = new ArrayList<Accordo>(); 
      intent.putExtra("chords", chords); 


      startActivity(intent); 
     } 
    }); 

} 
} 

有在这条线的错误:

ChordsListAdapter adapter = new ChordsListAdapter(this, getChords()); 

这是错误:

Error:(126, 38) error: constructor ChordsListAdapter in class ChordsListAdapter cannot be applied to given types; required: ArrayList found: ChordsListActivity,ArrayList reason: actual and formal argument lists differ in length

这是我的ChordsListAdapter

public class ChordsListAdapter extends RecyclerView.Adapter<ChordsListAdapter.MyViewHolder> { 
private ArrayList<Accordo> chordsList; 

public class MyViewHolder extends RecyclerView.ViewHolder { 
    public TextView nome, note; 
    public ImageView immagine; 

    public MyViewHolder(View view) { 
     super(view); 
     nome = (TextView) view.findViewById(R.id.nome); 
     note = (TextView) view.findViewById(R.id.note); 
     immagine = (ImageView) view.findViewById(R.id.immagine_accordo); 
    } 
} 

public ChordsListAdapter(ArrayList<Accordo> chordsList) { 
    this.chordsList = chordsList; 
} 

@Override 
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
    View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.row, parent, false); 

    return new MyViewHolder(itemView); 
} 

@Override 
public void onBindViewHolder(MyViewHolder holder, int position){ 
    Accordo chord = chordsList.get(position); 
    holder.nome.setText(chord.getNome()); 
    holder.note.setText(chord.getNote()); 
    holder.immagine.setImageResource(chord.getImage()); 
} 

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

你能帮我弄清楚问题是什么以及如何解决它?

+0

你可以发布getChords()方法的代码 –

+0

@DeepanshuGandhi已经存在,它的方法返回你正在传递的ChordsListActivity – Daniele

+0

中的ArrayList(this,getChords())两个参数,并且你有1个参数的构造函数 –

回答

2

此行

ChordsListAdapter adapter = new ChordsListAdapter(this, getChords()); 

是调用您的ChordsListAdapter的构造函数,看起来像这样在你ChordsListAdapter.class

public ChordsListAdapter(ArrayList<Accordo> chordsList) { 
    this.chordsList = chordsList; 
} 

要么你就需要改变你的构造函数接受ContextActivity

public ChordsListAdapter(Context context, ArrayList<Accordo> chordsList) { 
    this.chordsList = chordsList; 
} 

,或者你可以删除调用的第一个参数:

ChordsListAdapter adapter = new ChordsListAdapter(getChords()); 
+0

谢谢!很好的回答!即使我试图运行它时,我的回收视图仍然看起来是空的。你知道我该如何解决这个问题吗? – Daniele

+0

这似乎是另一个问题的问题,因为到目前为止我看不到代码存在问题;)可能的原因:您的'xml'(活动布局,行布局)编码不正确。 – yennsarah

+0

奇怪的...错误必须在那里,因为我的类只有这两个和我的XML布局用于改变我的ArrayList之前工作 – Daniele

1

你只有一个构造函数

public ChordsListAdapter(ArrayList<Accordo> chordsList) { 
    this.chordsList = chordsList; 
} 

它不具有活动作为参数,所以你必须改变你创建你的适配器这样的方式:

ChordsListAdapter adapter = new ChordsListAdapter(getChords()); 
0

你只有一个构造函数

ChordsListAdapter adapter = new ChordsListAdapter(this, getChords()); 

public ChordsListAdapter(ArrayList<Accordo> chordsList) { 
    this.chordsList = chordsList; 
} 

它不具有活动作为参数,所以你必须更改适配器,如下所示:

ChordsListAdapter adapter = new ChordsListAdapter(getChords());