java - 不兼容的类型以及尝试 .get 自定义适配器 (Android)

标签 java android

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

这是我应用程序的代码:

自定义适配器类:

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;

列出笔记 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);


    }

这是我的笔记类:

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

在 Custom Adapter 类的末尾,它给出了一个错误,指出“不兼容的类型,找到:字符串,需要:注意”

我知道这个错误是什么意思,但我一直在部分复制我在互联网上找到的这个例子:

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 天了,但我似乎无法让它工作。 这是我的应用程序中的其他几个类,但我认为它们不相关,所以我没有在上面发布它们。

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

提前致谢

干杯 科里

最佳答案

List<String> notes; 更改声明至 List<Note> notes;在你的CustomListAdapter类。

List<Note> notes;

上面的声明声明了一个List包含 Note对象。

关于java - 不兼容的类型以及尝试 .get 自定义适配器 (Android),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17042565/

相关文章:

java - Restful Spring 服务中的 session 范围

更新应用程序后Android服务停止

java - 将获取的数据转换为字符串、字符串数组映射

java - 如何将jar文件添加到jar文件中?

JavaFX TabPane 取消选项卡更改

android - 如何使用用户输入的数据填充我的列表

android - 使用应用通知 facebook 为我的应用邀请新用户

android - 如何获得Soundpool的持续时间

java.sql.SQLException : No suitable driver found for jdbc:mysql

java - Java Android 中的 Double/Float 输出问题