代码
public class ChatData extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "MessagePlus";
public ChatData(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public Cursor getAllQuestions3(MessagesAdapter usageSettings2) {
return this.getWritableDatabase().query(TABLE_CHAT_DATA,null,null,null,null,null,null);
}
}
适配器
final ChatData mHelper = new ChatData(this);
final Cursor csr = mHelper.getAllQuestions3(this);
对于适配器中的上下文没有任何作用。这显示了将Helper类上下文更改为Adapters名称的消息,如果我这样做,则在helper的上下文中会有一条红线...如果我直接尝试访问 ChatData.getWritableDatabase ,则表明您无法从静态类访问非静态方法,如果我在助手静态方法中使该方法显示错误,则说明类不能静态...一个错误导致了另一个错误,我不知道该怎么办,有人可以帮我吗请
编辑
完整的适配器代码
public class MessagesAdapter extends RecyclerView.Adapter<MessagesAdapter.MessageViewHolder>{
private List<SQLiteHelper> mMessagesHelperList;
private FirebaseAuth mAuth;
ChatData mHelper = new ChatData(this);
Cursor csr = mHelper.getAllQuestions3();
public MessagesAdapter(List<SQLiteHelper> mMessagesHelperList) {
this.mMessagesHelperList = mMessagesHelperList;
}
public class MessageViewHolder extends RecyclerView.ViewHolder{
public TextView messageText;
public MessageViewHolder(View view) {
super(view);
messageText = (TextView)view.findViewById(R.id.message_text_layout);
}
}
@Override
public MessageViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View V = LayoutInflater.from(parent.getContext()).inflate(R.layout.custom_activity_chat,parent,false);
mAuth = FirebaseAuth.getInstance();
return new MessageViewHolder(V);
}
@Override
public void onBindViewHolder(final MessageViewHolder holder, int position) {
String mSender = null;
String mMessage = null;
String mTime;
String mSeen = null;
String mTimer;
String mType;
while (csr.moveToNext()) {
mSender = csr.getString(csr.getColumnIndex(KEY_SENDER));
mMessage = csr.getString(csr.getColumnIndex(KEY_MESSAGE));
mTime = csr.getString(csr.getColumnIndex(KEY_TIME));
mSeen = csr.getString(csr.getColumnIndex(KEY_SEEN));
mTimer = csr.getString(csr.getColumnIndex(KEY_TIMER));
mType = csr.getString(csr.getColumnIndex(KEY_TYPE));
}
SQLiteHelper messagesHelper = mMessagesHelperList.get(position);
@Override
public int getItemCount() {
return mMessagesHelperList.size();
}
}
活动
final MainData mHelper = new MainData(this); //Change the name to your Helper Class name
final Cursor csr = myDBHlpr.getAllQuestions3(this);
messageList.setAdapter(mAdapter);
while (csr.moveToNext()) {
String mSender = csr.getString(csr.getColumnIndex(KEY_SENDER));
String mMessage = csr.getString(csr.getColumnIndex(KEY_MESSAGE));
String mTime = csr.getString(csr.getColumnIndex(KEY_TIME));
String mSeen = csr.getString(csr.getColumnIndex(KEY_SEEN));
String mTimer = csr.getString(csr.getColumnIndex(KEY_TIMER));
String mType = csr.getString(csr.getColumnIndex(KEY_TYPE));
messages.add(new SQLiteHelper(mSender, mMessage, mTime, mSeen, mTimer, mType));
}
答案 0 :(得分:0)
以下内容可能会引起您的某些问题。就是说,应该将方法传递给MesagesAdapter,因为就目前而言,没有理由将任何内容传递给 getAllQuestions3 方法。
public Cursor getAllQuestions3(MessagesAdapter usageSettings2) {
return this.getWritableDatabase().query(TABLE_CHAT_DATA,null,null,null,null,null,null);
}
尝试将以上内容更改为
public Cursor getAllQuestions3() {
return this.getWritableDatabase().query(TABLE_CHAT_DATA,null,null,null,null,null,null);
}
并使用
ChatData mHelper = new ChatData(this);
Cursor csr = mHelper.getAllQuestions3();
代替
final ChatData mHelper = new ChatData(this,ChatData.DATABASE_NAME,null,ChatData.DATABASE_VERSION);
final Cursor csr = mHelper.getAllQuestions3(this);
请注意,以上几行应位于Activity的onCreate方法或从onCreate调用的方法中,以便您具有有效的上下文(即this)。
请注意,以上内容已根据ChatData进行了更改,您需要提供4个用于实例化ChatData对象的参数。
我建议将构造函数更改为:-
public ChatData(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
然后可以使用ChatData mHelper = new ChatData(this);
。
以下是基于您提供的代码的有效示例。
数据库助手 ChatData.java
public class ChatData extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "MessagePlus";
public static final String TABLE_CHAT_DATA = "chatdata";
public static final String COL_CHATDATA_ID = BaseColumns._ID;
public static final String COL_CHATDATA_TIMESTAMP = "timestamp";
public static final String COL_CHATDATA_MESSAGE = "message";
public static final String COL_CHATDATA_USER = "user";
public ChatData(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String crt_sql = "CREATE TABLE IF NOT EXISTS " + TABLE_CHAT_DATA + "(" +
COL_CHATDATA_ID + " INTEGER PRIMARY KEY, " +
COL_CHATDATA_TIMESTAMP + " TEXT DEFAULT CURRENT_TIMESTAMP," +
COL_CHATDATA_MESSAGE + " TEXT, " +
COL_CHATDATA_USER + " INTEGER" +
")";
db.execSQL(crt_sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public Cursor getAllQuestions3() {
return this.getWritableDatabase().query(TABLE_CHAT_DATA,null,null,null,null,null,null);
}
public long addMessage(String message, long user) {
ContentValues cv = new ContentValues();
cv.put(COL_CHATDATA_MESSAGE,message);
cv.put(COL_CHATDATA_USER,user);
SQLiteDatabase db = this.getWritableDatabase();
return db.insert(TABLE_CHAT_DATA,null,cv);
}
}
自定义适配器(光标适配器) MessageAdapter.java
public class MessageAdapter extends CursorAdapter {
public MessageAdapter(Context context, Cursor c, boolean autoRequery) {
super(context, c, autoRequery);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return super.getView(position, convertView, parent);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View rv = LayoutInflater.from(context).inflate(
R.layout.messagelist_item,
parent,
false
);
return rv;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView mUser = view.findViewById(R.id.user);
TextView mTimestamp = view.findViewById(R.id.timestamp);
TextView mMessage = view.findViewById(R.id.message);
mUser.setText(cursor.getString(cursor.getColumnIndex(ChatData.COL_CHATDATA_USER)));
mTimestamp.setText(cursor.getString(cursor.getColumnIndex(ChatData.COL_CHATDATA_TIMESTAMP)));
mMessage.setText(cursor.getString(cursor.getColumnIndex(ChatData.COL_CHATDATA_MESSAGE)));
}
}
列表 messagelist_item.xml
中使用的布局<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/user"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/timestamp"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/message"
android:layout_width="0dp"
android:layout_weight="8"
android:layout_height="wrap_content" />
</LinearLayout>
活动中用于通过适配器列出消息的代码( MainActivty.java )
public class MainActivity extends AppCompatActivity {
ChatData mDBHlpr;
Cursor mCsr;
MessageAdapter mMesaageAdapter;
ListView mMessageList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mMessageList = this.findViewById(R.id.messagelist);
mDBHlpr = new ChatData(this);
addSomeData();
mCsr = mDBHlpr.getAllQuestions3();
mMesaageAdapter = new MessageAdapter(this,mCsr,false);
mMessageList.setAdapter(mMesaageAdapter);
}
private void addSomeData() {
mDBHlpr.addMessage("Hello",1);
mDBHlpr.addMessage("Hi",2);
mDBHlpr.addMessage("How are you?",1);
mDBHlpr.addMessage("I'm OK thanks, and you?",2);
mDBHlpr.addMessage("Good.",1);
}
}