1

I have been trying to get a custom adapter working in my first android app.

This is the code from my app:

CustomAdapter class:

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;

List notes activity

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);


    }

and this is my Note class:

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;
}

}

The problem i am having is that when i try and create the

    final Note noteModel = notes.get(index);

at the end of the Custom Adapter class, it gives a error saying "incompatible types, found: string, required: Note"

I know what the error means, but i have been partly copying this example i found on the internet:

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;

and:

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;
}

In that example the person does the same thing i am trying to do, but that code runs, etc. As far as i can see, the code is quite similar to my Note class, and custom adapter.

I have been trying to figure this out for 2 days now, but i cant seem to get it working. the is a couple of other classes in my App, but i dont think they are relevant, so i havent posted them above.

If someone could tell me how to get around this problem, i would be very grateful.

Thanks in advance

Cheers Corey

4

1 回答 1

2

Change the declaration from List<String> notes; to List<Note> notes; in your CustomListAdapter class.

List<Note> notes;

The above declaration declares a List containing Note objects.

于 2013-06-11T11:06:55.213 回答