设置模型类和自定义适配器以从onListItemClick获取SQLite DB rowID

问题描述 投票:0回答:1

我有一个应用程序是从一个预先存在的SQLite数据库中填充的。现在,单击列表项时,我需要获取rowID(_id)。我认为我没有正确设置我的应用程序才能做到这一点。我有很多人为我提供有关操作的信息,但在尝试将其应用到我的应用程序时似乎无法理解。有人可以使用我的代码提供示例吗?我真的很感激。

这是我的模特班:

package com.mypackage.myapp.model;

import android.database.Cursor;

public class Verse {

private Cursor verseCursor;
private String verseNotificationString;
private String verseTitleString;
private int dBrowIdInt;
private String bookNameString;
private String chapterNumberString;
private String verseNumberString;
private String verseString;

public Verse(Cursor cursor, String bookNameString, String chapterNumberString, String verseNumberString, String verseString) {
    verseCursor = cursor;
    verseTitleString = bookNameString + " " + chapterNumberString + ":" + verseNumberString;
    verseNotificationString = verseString;
}

public Verse(int dBrowId) {
    dBrowIdInt = dBrowId;
}

public Cursor getVerseCursor() {
    return verseCursor;
}

public void setVerseCursor(Cursor verseCursor) {
    this.verseCursor = verseCursor;
}

public String getVerseTitleString() {
    return verseTitleString;
}

public String getNotificationString() {
    return verseNotificationString;
}

public Integer getDbRowId() {
    return dBrowIdInt;
}

public void setNotificationString(String notificationString) {
    this.verseNotificationString = notificationString;
}

public void setTitleString(String titleString) {
    this.verseTitleString = titleString;
}

}

这是我的自定义适配器:

    package com.mypackage.myapp;

    import android.content.Context;
    import android.content.SharedPreferences;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import java.util.List;

public class VerseAdapter extends ArrayAdapter<String> {

private Context mContext;
private int mResource1;
private int mResource2;
public List<String> mVerses;
private List<Long> mRowId;
private TextView mVerseTextView;
private TextView mVerseNumberTextView;
private TextView mRowIdTextView;
private boolean mNightModeSwitchState;
private boolean mNightModeTwoSwitchState;

public VerseAdapter(Context context, int resource1, List<String> 
verses, List<Long> rowId) {
    super(context, resource1, verses);
    this.mContext = context;
    this.mResource1 = resource1;
    this.mVerses = verses;
    this.mRowId = rowId;

}

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

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

@NonNull
@Override
public View getView(int position, @Nullable View convertView, 
@NonNull ViewGroup parent) {

    View listItem = convertView;
    int pos = position + 1;
    if (listItem == null) {
        listItem = 
LayoutInflater.from(mContext).inflate(mResource1, parent, false);
    }

    //mRowIdTextView = (TextView) 
listItem.findViewById(R.id.rowId);
//mRowIdTextView.setText(String.valueOf(getItemId(position)));
    //mRowIdTextView.setText(String.valueOf(mRowId.get(position)));

    mVerseNumberTextView = (TextView) 
listItem.findViewById(R.id.verseNumber);
    mVerseNumberTextView.setText(String.valueOf(pos) + " ");

    mVerseTextView = (TextView) 
listItem.findViewById(R.id.verseWords);
    mVerseTextView.setText(mVerses.get(position));

    SharedPreferences nightModeSwitchState = 
getContext().getSharedPreferences("SettingsActivity", 0);
    mNightModeSwitchState = 
nightModeSwitchState.getBoolean("NightModeSwitchState", false);

    SharedPreferences rowPosition = 
getContext().getSharedPreferences("RowPosition", 0);
    rowPosition.edit().putInt("RowPositionSqlite", 
position).apply();

    return listItem;
}

}

这是我需要从中访问rowID的类中的onListItemClick方法(您可以看到一些我尝试过但不起作用的内容被注释掉了:]

mListView.setOnItemClickListener(new 
AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View 
view, int position, long id) {

            String selected = (String) 
mListView.getItemAtPosition(position);
            mVerseTextView = findViewById(R.id.verseWords);
            mVerseNumberTextView = findViewById(R.id.verseNumber);

            mAdapterView = adapterView;
            mPositionForRow = position;
            //mIdLong = id;
            // VerseAdapter verseAdapter = (VerseAdapter) 
adapterView.getAdapter();
            // mSelectedVerse = (VerseForId) 
mListView.getItemAtPosition(position);
            // mDbRowId = mSelectedVerse.getDbRowId();

            //  mIdLong = verseAdapter.getItemId(position);

            //  mRowIdString = String.valueOf(id);

            // VerseAdapter da = (VerseAdapter) 
    adapterView.getAdapter();
            //mRowIdString = 
    String.valueOf(da.mVerses.get(position).getId());
            // Toast.makeText(BibleBookVersesOldTestActivity.this, 
    mRowIdString + " id", Toast.LENGTH_LONG).show();

            mBookCopied = 
mVerseHeaderBookNameTextView.getText().toString();
            mChapterNumberCopied = 
Integer.toString(mChapterSelected + 1);
            mVerseNumberCopied = 
mVerseNumberTextView.getText().toString();
            mPosition = position + 1;
            mCopiedVerseListItem = mBookCopied + " " + 
mChapterNumberCopied + ":" + mPosition + "\n\n" + selected;

            showMenuVerseAction(view);
        }
    });

我知道我需要在模型类和自定义适配器中进行一些设置,以使其能够在onlistitemclick上检索数据库rowID。有人可以使用我的代码给我一个例子吗?谢谢您的帮助。

*******添加...。这是我的Databaseaccess类...。在其中的一部分中,我从SQLite数据库的某本书中提取了经文。我已经调整了另一个填充列表的类,因此不得不更改以下操作以接受模型类Verse而不是列表中的String ...我遇到的问题是读取list.add(cursor的下面的行.getString(6))(6)指的是从中检索经文的列索引...给出一个错误,我无法将getString应用于Verse ...如何解决此问题?

public List<Verse> getVersesChapterOne() {
    List<Verse> list = new ArrayList<>();
    Cursor cursor = database.rawQuery("SELECT * FROM bookcombinedfull 
LIMIT 31", null);
    cursor.moveToFirst();
    while (!cursor.isAfterLast()) {
        list.add(cursor.getString(6));
        cursor.moveToNext();
    }
    cursor.close();
    return list;
}
android sqlite listview adapter rowid
1个回答
1
投票

一个想法是将自定义适配器的列表从String替换为您的模型Verse。这样,您可以使用.getItem()从模型类中检索所需的任何信息。

// public class VerseAdapter extends ArrayAdapter<String> {
public class VerseAdapter extends BaseAdapter {

  public List<String> mVerses;

  // public VerseAdapter(Context context, int resource1, List<String> verses, List<Long> rowId) {
  public VerseAdapter(Context context, int resource1, List<Verse> verses) {
    //...
    mVerses = verses;
    //...

  @Override
  public Object getItem(int i) {
    return (Verse)mList.get(i);
  }

// ...
// In your MainActivity.java, do this to get your item:

Verse verse = (Verse) mAdapter.getItem(position);
Log.d(TAG, verse.getVerseTitleString());
© www.soinside.com 2019 - 2024. All rights reserved.