不兼容的类型很好地尝试.get在自定义适配器(Android)

时间:2013-06-11 11:04:36

标签: java android

我一直在尝试在我的第一个Android应用程序中使用自定义适配器。

这是我的应用程序中的代码:

CustomAdapter类:

class CustomListAdapter extends BaseAdapter {




Context mContext;
List<String> notes;


public CustomListAdapter (Context context, List notes) {
    mContext = context;
    this.notes = notes;

}


@Override
public int getCount() {
    return notes.size();

}

@Override
public String getItem(int i) {
    return notes.get(i);
}

@Override
public long getItemId(int i) {
    return i;

}

// This method is called to draw each row of the list
@Override
public View getView(int index, View convertView, final ViewGroup parent) {

    View vi = convertView;

    if (vi == null) {

        LayoutInflater inflater;
        inflater = LayoutInflater.from(parent.getContext());
        vi = inflater.inflate(R.layout.item_list, parent, false);

    }

    final Note noteModel = notes.get(index);




    TextView date = (TextView) vi.findViewById(R.id.date);
    date.setText("" + noteModel.getDate());
    TextView title = (TextView) vi.findViewById(R.id.title);
    title.setText(noteModel.getTitle());
    TextView content = (TextView) vi.findViewById(R.id.content);
    content.setText(noteModel.getNote());
    final View view = View.inflate(mContext, R.layout.item_list, null);



    return view;

列出备注活动

public class ListNotesActivity extends Activity {

@Override
public boolean onContextItemSelected(MenuItem item) {
    AdapterView.AdapterContextMenuInfo info =   (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
    notes.remove(info.position);
    populateList();

    return true;
}


@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.context_menu, menu);
}




@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_CANCELED){
        return;
    }
    Serializable extra = data.getSerializableExtra("Note");
    if (extra != null){

        Note newNote = (Note)extra;
        if (editingNoteId > -1){

            notes.set(editingNoteId, newNote);
            editingNoteId = -1;
        }
        else {

            notes.add(newNote);
        };
        populateList();
        //populateLateCustomAdapter();

    }

}



public List<Note> notes = new ArrayList<Note>();
private ListView notesListView;
private int editingNoteId = -1;

@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_list_notes);
    ListView notesListView = (ListView)findViewById(R.id.notesListView);

    notesListView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapter, View view, int itemNumber, long id) {

            Intent editNoteIntent = new Intent(view.getContext(),         EditNotesActivity.class);
            editNoteIntent.putExtra("Note", notes.get(itemNumber));
            editingNoteId = itemNumber;
            startActivityForResult(editNoteIntent, 1);


        }
    });

    registerForContextMenu(notesListView);

    notes.add(new Note("1 Note", "blah blah", new Date()));
    notes.add(new Note("2 Note", "blah blah", new Date()));
    notes.add(new Note("3 Note", "blah blah", new Date()));
    notes.add(new Note("4 Note", "blah blah", new Date()));
    notes.add(new Note("5 Note", "blah blah", new Date()));
    notes.add(new Note("6 Note", "blah blah", new Date()));
    notes.add(new Note("7 Note", "blah blah", new Date()));
    notes.add(new Note("8 Note", "blah blah", new Date()));

    populateList();



}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.list_notes, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {


    Intent editNoteIntent = new Intent (this, EditNotesActivity.class);
    startActivityForResult(editNoteIntent, 1);


    return true;


}




    // Populate Method
    private void populateList() {

        CustomListAdapter CustomAdapter = new CustomListAdapter(this, notes);
        notesListView.setAdapter(CustomAdapter);


    }

这是我的Note课程:

public class Note implements Serializable {

private String title;
private String note;
private Date date;

// Constructor
public Note(String title, String note, Date date) {
    this.title = title;
    this.note = note;
    this.date = date;
}



// Title Getter and Setter
public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}




// Note Getter and Setter
public String getNote() {
    return note;
}

public void setNote(String note) {
    this.note = note;
}




// Date Getter and Setter
public Date getDate() {
    return date;
}

public void setDate(Date date) {
    this.date = date;
}

}

我遇到的问题是当我尝试创建

    final Note noteModel = notes.get(index);

在自定义适配器类的末尾,它给出了一个错误说&#34;不兼容的类型,找到:字符串,必需:注意&#34;

我知道这个错误意味着什么,但我已经部分复制了我在互联网上找到的这个例子:

public class CustomAdapter extends BaseAdapter {

private static final String TAG = CustomAdapter.class.getSimpleName();
ArrayList<DataModel> listArray;

public CustomAdapter() {
    listArray = new ArrayList<DataModel>(5);
    listArray.add(new DataModel("Title1", "Java", new Date()));
    listArray.add(new DataModel("name2",  "Python", new Date()));
    listArray.add(new DataModel("name3",  "Django", new Date()));
    listArray.add(new DataModel("name4",  "Groovy", new Date()));
    listArray.add(new DataModel("name5", "Maven", new Date()));
}

@Override
public int getCount() {
    return listArray.size();    // total number of elements in the list
}

@Override
public Object getItem(int i) {
    return listArray.get(i);    // single item in the list
}

@Override
public long getItemId(int i) {
    return i;                   // index number
}

@Override
public View getView(int index, View view, final ViewGroup parent) {

    if (view == null) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        view = inflater.inflate(R.layout.single_list_item, parent, false);
    }

    final DataModel dataModel = listArray.get(index);

    TextView title = (TextView) view.findViewById(R.id.title);
    title.setText(dataModel.getTitle());

    TextView content = (TextView) view.findViewById(R.id.content);
    content.setText(dataModel.getContent());

    TextView date = (TextView) view.findViewById(R.id.date);
    date.setText("" + dataModel.getDate());


    return view;

和:

public class DataModel {
private String title;
private String content;
private Date date;

public DataModel(String title, String content, Date date) {
    this.title = title;
    this.content = content;
    this.date = date;
}


public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public String getContent() {
    return content;
}

public void setContent(String content) {
    this.content = content;
}

public Date getDate() {
    return date;
}

public void setDate(Date date) {
    this.date = date;
}

在那个例子中,这个人做了我想做的同样的事情,但是代码运行等等。 据我所知,代码与我的Note类和自定义适配器非常相似。

我一直试图解决这个问题2天,但我似乎无法让它发挥作用。 这是我的应用程序中的其他几个类,但我不认为它们是相关的,所以我没有在上面发布它们。

如果有人能告诉我如何解决这个问题,我将非常感激。

提前致谢

干杯 科里

1 个答案:

答案 0 :(得分:2)

List<String> notes;课程中的声明从List<Note> notes;更改为CustomListAdapter

List<Note> notes;

上述声明声明List包含Note个对象。