خطا در ست کردن adapter به lst_content.setAdapter(adapter)
با سلام در انجام آموزش لیست ویو آموزش قدیم اندروید (من تو محیط اندروید استودیو کد زدم من) هنگام ست کردن مقدار به ارور زیر بر میخورم از چی ایراد می گیره ؟
Error:(24, 15) error: no suitable constructor found for ArrayAdapter(ArrayList<StructNote>)
constructor ArrayAdapter.ArrayAdapter(Context,int) is not applicable
(actual and formal argument lists differ in length)
constructor ArrayAdapter.ArrayAdapter(Context,int,int) is not applicable
(actual and formal argument lists differ in length)
constructor ArrayAdapter.ArrayAdapter(Context,int,Object[]) is not applicable
(actual and formal argument lists differ in length)
constructor ArrayAdapter.ArrayAdapter(Context,int,int,Object[]) is not applicable
(actual and formal argument lists differ in length)
constructor ArrayAdapter.ArrayAdapter(Context,int,List) is not applicable
(actual and formal argument lists differ in length)
constructor ArrayAdapter.ArrayAdapter(Context,int,int,List) is not applicable
(actual and formal argument lists differ in length)
شما adapter رو در بالای صفحه از نوع ArrayAdapter تعریف کردی. چرا در پایین وقتی new کردی، از AdapterNote استفاده کردی؟
بالایی رو هم به AdapterNote تغییر بده، مشکلت حل میشه.
کد های قسمت ActivityMain
package app.developer.listviewtest;
import java.util.ArrayList;
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class ActivityMain extends Activity {
public ArrayList<StructNote> notes = new ArrayList<StructNote>();
public AdapterNote adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView lsd_content = (ListView) findViewById(R.id.lsd_content);
adapter = new AdapterNote(notes);
for (int i = 0; i < 10; i++) {
StructNote note = new StructNote();
note.title = "Title" + i;
note.description = "Description " + i;
note.done = false;
notes.add(note);
}
adapter.notifyDataSetChanged();
lsd_content.setAdapter(adapter);
}
}
کد های قسمت adapter_note.java
package app.developer.listviewtest;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class adapter_notes extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.adapter_notes);
}
}
کد های کلاس AdapterNote.java
package app.developer.listviewtest;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.TextView;
import java.util.ArrayList;
public class AdapterNote extends ArrayAdapter<StructNote> {
public AdapterNote(ArrayList<StructNote> array) {
super(G.context, R.layout.adapter_notes, array);
}
private static class ViewHolder {
public TextView txt_title;
public TextView txt_description;
public CheckBox chk_done;
public ViewHolder(View view) {
txt_title = (TextView) view.findViewById(R.id.txt_title);
txt_description = (TextView) view.findViewById(R.id.txt_description);
chk_done = (CheckBox) view.findViewById(R.id.chk_done);
}
public void fill(ArrayAdapter<StructNote> adapter, StructNote item, int position) {
txt_title.setText(item.title);
txt_description.setText(item.description);
chk_done.setChecked(item.done);
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
StructNote item = getItem(position);
if (convertView == null) {
convertView = G.inflater.inflate(R.layout.adapter_notes, parent, false);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.fill(this, item, position);
return convertView;
}
}
کدهای قسمت StructNote
package app.developer.listviewtest;
public class StructNote {
public String title;
public String description;
public boolean done;
}
این قسمت از کد
public class ActivityMain extends Activity {
public ArrayList<StructNote> notes = new ArrayList<StructNote>();
public AdapterNote adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView lsd_content = (ListView) findViewById(R.id.lsd_content);
adapter = new AdapterNote(notes);
for (int i = 0; i < 10; i++) {
StructNote note = new StructNote();
note.title = "Title" + i;
note.description = "Description " + i;
note.done = false;
notes.add(note);
}
adapter.notifyDataSetChanged();
lsd_content.setAdapter(adapter);
}
}
به این صورت عوض کنید :
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView lsd_content = (ListView) findViewById(R.id.lsd_content);
// adapter = new AdapterNote(notes); // اداپتر با آرایه ایی بسازید که پر از اطلاعات باشه
for (int i = 0; i < 10; i++) {
StructNote note = new StructNote();
note.title = "Title" + i;
note.description = "Description " + i;
note.done = false;
notes.add(note);
}
adapter = new AdapterNote(notes); // بعد از اینکه اطلاعات رو پر کردید اداپتر رو بسازید
lsd_content.setAdapter(adapter); // اداپتری که پر از اطلاعات هست رو به لیست ویو بدید
adapter.notifyDataSetChanged(); // حالا از اداپتر بخواید اطلاعات نمایش رو رفرش کنه
}
پاسخگویی و مشاهده پاسخ های این سوال تنها برای اعضای ویژه سایت امکان پذیر است .
چنانچه تمایل دارید به همه بخش ها دسترسی داشته باشید میتوانید از این بخش لایسنس این آموزش را خریداری نمایید .
