android List View适配器并将项目对象发送到另一个活动

时间:2018-10-09 22:51:42

标签: android

我是android的新手,我正在尝试创建笔记应用程序来练习.. 我遇到了一个问题,我试图对此进行大量搜索,但是找不到我想要的东西。 简单,我想要一个包含所有注释的列表视图(数据库中对象的ArrayList),并且该列表视图显示注释的标题。 然后单击列表中的一个项目以打开另一个显示标题和内容的活动。 到目前为止,我已经有了这段代码。 这是笔记之类

    public class notesTable {
    public  int id;
    public String title;
    public String content;

    public notesTable(){

    }
    public notesTable( String title,String content){

        this.title = title;
        this.content = content;

    }
    public notesTable(int id, String title,String content){
        this.id = id;
        this.title = title;
        this.content = content;

    }
    @Override
    public String toString() {
        return this.id + ". " + this.title;
    }
}

这是具有列表视图的.xml文件

    <ListView
    android:id="@+id/notesLV"
    android:layout_width="368dp"
    android:layout_height="495dp"
    android:layout_marginBottom="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginTop="8dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.0" />

这是活动的.java文件

    ListView lv;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_notes_home);

    DatabaseHelper db = new DatabaseHelper(this);
    lv = (ListView) findViewById(R.id.notesLV);
    ArrayList<notesTable> notes_list = db.getAllNotes();

    noteAdapter adapter = new noteAdapter(this, notes_list);
    ListView listView = (ListView) findViewById(R.id.notesLV);
    listView.setAdapter(adapter);


    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1,
                                int arg2, long arg3) {
            Intent i = new Intent(notesHomeActivity.this, SecondActivity.class);
            i.putExtra("noteObj", lv.getItemAtPosition(arg2));
            startActivity(i);
        }
    });
}

这是noteAdapter类

public class noteAdapter extends ArrayAdapter<notesTable> {
public noteAdapter(Context context, ArrayList<notesTable> notes) {
    super(context, 0, notes);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // Get the data item for this position
    notesTable note = getItem(position);
    // Check if an existing view is being reused, otherwise inflate the view
    if (convertView == null) {
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_note, parent, false);
    }
    // Lookup view for data population
    TextView noteTitle = (TextView) convertView.findViewById(R.id.noteTitle);
    TextView noteContent = (TextView) convertView.findViewById(R.id.noteContent);
    // Populate the data into the template view using the data object
    noteTitle.setText(note.title);
    noteContent.setText(note.content);
    // Return the completed view to render on screen
    return convertView;
}

}

好,到目前为止,列表显示的非常好,但是我想转到详细信息活动并将所选对象发送到该视图以显示详细信息,或者至少发送ID 我该怎么办???

此行给我错误

i.putExtra("noteObj", lv.getItemAtPosition(arg2));

1 个答案:

答案 0 :(得分:0)

您需要在您的notesTable类中实现Parcelable:

public class NotesTable implements Parcelable {
    public static final Creator<NotesTable> CREATOR = new Creator<NotesTable>(() {
        @Override
        public NotesTable createFromParcel(Parcel parcel) {
            return new NotesTable(parcel);
        }

        @Override
        public NotesTable[] newArray(int size) {
            return new NotesTable[size];
        }
    }

    public int id;
    public String title;
    public String content;

    public NotesTable() {}

    public NotesTable(String title, String content) {
        this.title = title;
        this.content = content;
    }

    public NotesTable(int id, String title, String content) {
        this.id = id;
        this.title = title;
        this.content = content;
    }

    public NotesTable(Parcel parcel) {
        this(parcel.readInt(), parcel.readString(), parcel.readString());
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(id);
        dest.writeString(title);
        dest.writeString(content);
    }

    @Override
    public String toString() {
        return this.id + ". " + this.title;
    } 
}

您现在可以在Intent中传递它。

不过,我确实需要指出一些有关示例的内容:

  • Java中的命名方案为ClassNamevariableName。类名中的每个“单词”都大写,包括第一个。对于变量,它是相同的,但是第一个字母为小写。如果您遵循此规则,则我们和您都可以更轻松地阅读。

  • 在编写代码时请注意格式化。与我的第一点一样,它使我们更容易阅读和理解,并且将来使您更易于参考。如果在编程时无法执行此操作,则Android Studio会提供漂亮的 Ctrl + Alt + Shift + L < / kbd>,它将为您设置格式。

  • 与我提出的第一点一致,我将您的班级从notesTable重命名为NotesTable。在您的IDE中单击您当前的班级名称,然后按 Shift + F6 (在Linux以及macOS上将F6替换为6),然后将班级重命名为NotesTable。然后,您将能够复制并粘贴我已发布的内容(尽管我建议至少通读它)。