0

我正在制作我的第一个安卓应用程序。我目前正在尝试让自定义适配器工作。

这是代码:

自定义适配器类:

class CustomListAdapter extends BaseAdapter {

Context mContext;
//    List <Note> notesR = new ArrayList<Note>();
ArrayList<Note> notesR;

public CustomListAdapter (List notes) {
    // mContext = context;
//       notesR =  = notes;



    notesR = new ArrayList<Note>(3);
    notesR.add(new Note("Hello", "bye", new Date()));
    notesR.add(new Note("Hello", "bye", new Date()));
    notesR.add(new Note("Hello", "bye", new Date()));
}



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

@Override
public Object getItem(int i) {
    return notesR.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 = notesR.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());
    // here you inflate the layout you want for the row
    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();
    }

}



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

    //notes.add(new Note("Added note", "blah", new Date()));
    //populateList();

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


    return true;


}




// Populate Method

// Populate Method
private void populateList() {

    CustomListAdapter customAdapter = new CustomListAdapter(notes);
     notesListView.setAdapter(customAdapter);
    //CustomAdapter customAdapter = new CustomAdapter();
    //notesListView.setAdapter(customAdapter);

我一直试图让 apapter 开始使用虚拟数据,但它仍然不断崩溃。

我一直试图找到断点问题的根源,如果我在第一个@Overrride 之前放置一个吱吱声点,它会一直运行到那里,但是一旦我让它运行到@Override 它就会崩溃. 我尝试删除第一个覆盖,然后代码运行良好,直到下一个覆盖,它再次崩溃。

我整天都在努力让这个工作,你甚至把它发给了我更远的 C# 开发人员,他也不知道。

如果有人能告诉我究竟是什么原因造成的,我将不胜感激。

我下载了一个示例,它使用与我的非常相似的代码,并且运行良好。这是示例中的代码:

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;

编辑1:

LogCat(我只为我的应用过滤了它):

06-12 15:26:46.359 8851-8851/com.fishingfon.notetakerui
D/dalvikvm: 延迟启用 CheckJNI 06-12 15:26:46.399
8851-8851/com.fishingfon.notetakerui W/ActivityThread: 应用程序 com.fishingfon .notetakerui 正在端口 8100 上等待调试器... 06-12 15:26:46.409 8851-8851/com.fishingfon.notetakerui
I/System.out: Sending WAIT chunk 06-12 15:26:46.419
8851-8856 /com.fishingfon.notetakerui I/dalvikvm:调试器处于活动状态 06-12 15:26:46.614 8851-8851/com.fishingfon.notetakerui
I/System.out:调试器已连接 06-12 15:26:46.614
8851-8851 /com.fishingfon.notetakerui I/System.out: 等待调试器解决... 06-12 15:26:46.814
8851-8851/com.fishingfon.notetakerui I/System.out: 等待调试器解决... 06-12 15:26:47.014
8851-8851/com.fishingfon.notetakerui I/System.out: 等待调试器解决... 06-12 15:26:47.214
8851-8851/com.fishingfon.notetakerui I/System.out: 等待调试器解决... 06-12 15:26:47.289
2137-2137/com.google .android.youtube D/YouTube MDX: 收到意图 android.intent.action.SCREEN_OFF 06-12 15:26:47.414
8851-8851/com.fishingfon.notetakerui I/System.out: 等待调试器解决... 06 -12 15:26:47.614
8851-8851/com.fishingfon.notetakerui I/System.out: 等待调试器解决... 06-12 15:26:47.814
8851-8851/com.fishingfon.notetakerui I/System.out: 调试器已解决 (1315) 06-12 15:26:48.149
8851-8851/com.fishingfon.notetakerui D/AndroidRuntime: 关闭 VM 06-12 15: 26:48.149 8851-8851/com.fishingfon.notetakerui
W/dalvikvm: threadid=1: 线程退出但未捕获异常 (group=0x40f4d930) 06-12 15:26:48.169
209 8851-8851/com.fishingfon.notetakerui I/Process:发送信号。PID:8851 SIG:9

编辑2:

新适配器代码:

class CustomListAdapter extends BaseAdapter {

Context mContext;
//    List <Note> notesR = new ArrayList<Note>();
ArrayList<Note> notesR;

public CustomListAdapter (Context context, Object notes) {
     mContext = context;
//       notesR =  = notes;



    notesR = new ArrayList<Note>(3);
    notesR.add(new Note("Hello", "bye", new Date()));
    notesR.add(new Note("Hello", "bye", new Date()));
    notesR.add(new Note("Hello", "bye", new Date()));
}



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

@Override
public Object getItem(int position) {
    return notesR.get(position);
}

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

}

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

    View vi = convertView;

    if (vi == null) {

        LayoutInflater inflater;
       // inflater = LayoutInflater.from(parent.getContext());
        vi = View.inflate(mContext, R.layout.item_list, null);

    }

    final Note noteModel = notesR.get(position);

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


    return vi;

在此先感谢 干杯科里

4

1 回答 1

0

这里不需要此代码

final View view = View.inflate(mContext, R.layout.item_list, null);
return view;

只需添加这个而不是上面

return vi;

在 CustomListAdapter 的 getView() 中

编辑

public CustomListAdapter (Context context, int id, ArrayList<Note> notes) {
   super(context,id,notes);
   mContext = context;
   notesR =  = notes;
}

在活动中

CustomListAdapter customAdapter = new CustomListAdapter(this, R.layout.listitem, notes);
notesListView.setAdapter(customAdapter);
于 2013-06-12T05:05:11.730 回答