gpt4 book ai didi

View 中的 android Cursor 必须是最终的?

转载 作者:行者123 更新时间:2023-11-30 03:10:42 24 4
gpt4 key购买 nike

我有一个 Activity 在 ListView 中显示来自数据库的用户消息。 ListView 正确显示有关消息的各种信息,并且 listView 中的每一行都有一个 onClickListener,它会启动另一个 Activity 以显示有关消息的更多信息。

一切正常。我在列表行中添加了一个复选框。选中 CheckBox 以标记已选择要删除的消息。每条消息都有一个唯一的 GUID 标识符,该标识符存储在数据库中,因此可通过 View 中的游标和适配器使用。单击一行时,此 quid 将通过 onclicklistener 传递到下一个 Activity。

一切正常。我现在向复选框添加了一个 onCheckedChanged 监听器。当我选中复选框时,我想从 getView 中的光标获取这个唯一的 guid 标识符。

当复选框设置了它的 onCheckedChanged 监听器时,它使用匿名内部类,这就是为什么游标需要是最终的。

不幸的是,这意味着如果数据库中有 5 条消息并且我检查了所有要删除的消息,那么所有消息都具有相同的 guid,因为一旦它被设置为第一个值就无法更改(最终)。

我理解为什么它必须是最终的,就好像匿名内部类的持续时间比创建游标的地方更长,然后监听器将引用不存在的对象。

link

我该如何解决这个问题?基本上,如何在不使游标最终化的情况下使游标中的数据在复选框的 onCheckedChanged 方法中可用?

提前致谢

public class ViewMessagesActivity extends Activity{

private static final String TAG = ViewMessagesActivity.class.getSimpleName();
final String BACKPRESS_ACTION = "com.carefreegroup.rr3.BACKPRESS_ACTION";

NfcScannerApplication nfcscannerapplication;
Cursor cursorMessages;
ListView listView;
SimpleCursorAdapter adapter;
MyAdapter myAdapter;
TextView noMessages;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.viewmessages);

nfcscannerapplication = (NfcScannerApplication) getApplication();





//transactionCount = (TextView)findViewById(R.id.textviewtransactionsfordaycount);
listView = (ListView) findViewById(R.id.listviewmessages);

noMessages = (TextView)findViewById(R.id.textviewnomessageslabel);

// get data
cursorMessages = nfcscannerapplication.loginValidate.queryAllFromMessage();
cursorMessages.moveToLast();
startManagingCursor(cursorMessages);

// setup adapter and show the data





if(cursorMessages.getCount() == 0){


noMessages.setVisibility(View.VISIBLE);
listView.setVisibility(View.GONE);

}else{

listView.setVisibility(View.VISIBLE);
noMessages.setVisibility(View.GONE);

}




String[] from = {
LoginValidate.C_MESSAGE_CREATED_AT, LoginValidate.C_MESSAGE_TEXT, LoginValidate.C_MESSAGE_SENDER};
int[] to = { R.id.messagecreatedat, R.id.messagetext, R.id.messagesender};

myAdapter = new MyAdapter(nfcscannerapplication, R.layout.rowmessages, cursorMessages, from, to);
listView.setAdapter(myAdapter);
listView.setOnItemClickListener(myAdapter);



}//end of onCreate







@Override
protected void onResume() {
super.onResume();


}




private class MyAdapter extends SimpleCursorAdapter implements OnItemClickListener {

public MyAdapter(Context context, int layout, Cursor c, String[] from,
int[] to) {
super(context, layout, c, from, to);


}

@Override
public
View getView(int position, View convertView, ViewGroup parent) {
Log.e(TAG, "inside myadapter getview for messages");
View v = super.getView(position, convertView, parent);
if(v == null)
return null;

Cursor c = (Cursor)getItem(position);

v.setTag(c);








String messageSender = c.getString(c.getColumnIndex(LoginValidate.C_MESSAGE_SENDER));
String isRepliedTo = c.getString(c.getColumnIndex(LoginValidate.C_MESSAGE_REPLIED));
String isStandAlone = c.getString(c.getColumnIndex(LoginValidate.C_MESSAGE_IS_STANDALONE));




((TextView)v.findViewById(R.id.messagecreatedat)).setText(formattedMessCreatedAt );
((TextView)v.findViewById(R.id.messagetext)).setText(messageText);
((TextView)v.findViewById(R.id.messagesender)).setText(messageSender);

//#003F87 = blue

((TextView)v.findViewById(R.id.messagecreatedat)).setTextColor(Color.parseColor("#003F87"));
((TextView)v.findViewById(R.id.messagesender)).setTextColor(Color.parseColor("#003F87"));
((TextView)v.findViewById(R.id.messagetext)).setTextColor(Color.parseColor("#FF0000"));




CheckBox cb = ((CheckBox)v.findViewById(R.id.list_checkbox));
cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {


String messageGuid = c.getString(c.getColumnIndex(LoginValidate.C_MESSAGE_GUID));

if(isChecked == true){

Log.e(TAG, "checkBox true and guid = " + messageGuid);
}else{
Log.e(TAG, "checkBox false and guid = " + messageGuid);
}

}
});


return v;
}

@Override
public void onItemClick(AdapterView<?> parent, View view, int pos,
long id) {

Cursor itemCursor = (Cursor) view.getTag();

String messageGuid = itemCursor.getString(itemCursor.getColumnIndex(LoginValidate.C_MESSAGE_GUID));



String messageText = itemCursor.getString(itemCursor.getColumnIndex(LoginValidate.C_MESSAGE_TEXT));
String messageCreatedAt = itemCursor.getString(itemCursor.getColumnIndex(LoginValidate.C_MESSAGE_CREATED_AT));
String messageSender = itemCursor.getString(itemCursor.getColumnIndex(LoginValidate.C_MESSAGE_SENDER));
String messageReplied = itemCursor.getString(itemCursor.getColumnIndex(LoginValidate.C_MESSAGE_REPLIED));
String messageSeen = itemCursor.getString(itemCursor.getColumnIndex(LoginValidate.C_MESSAGE_SEEN));
String isStandAlone = itemCursor.getString(itemCursor.getColumnIndex(LoginValidate.C_MESSAGE_IS_STANDALONE));

Intent i = new Intent(ViewMessagesActivity.this, ReplyToMessageActivity.class);
i.putExtra("guid", messageGuid);
i.putExtra("message", messageText);
i.putExtra("createdat", messageCreatedAt);
i.putExtra("sender", messageSender);
i.putExtra("messagereplied", messageReplied);
i.putExtra("messageseen", messageSeen);
i.putExtra("isstandalone", isStandAlone);

startActivity(i);



}



}// end of adapter




}

.

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="5dp"
android:paddingBottom="5dp"



>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"

android:background="@drawable/rounded_corners_white"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
>


<CheckBox
android:id="@+id/list_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:background="@drawable/checkboxbg"
android:layout_marginLeft="5px"
android:layout_marginTop="5px"

></CheckBox>


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:paddingBottom="10dp"
android:paddingTop="10dp" >



<TextView
android:id="@+id/messagecreatedat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.4"
android:text="TextView"
/>

<TextView
android:id="@+id/messagesenderlabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:gravity="right"
android:text="From: "
android:textColor="#003F87"
/>

<TextView
android:id="@+id/messagesender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="left"
android:text="TextView"
/>


</LinearLayout>







<TextView
android:id="@+id/messagetext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="TextView"
android:paddingBottom="10dp"
/>



</LinearLayout>

</RelativeLayout>

[编辑1]

01-09 16:35:15.220: E/ViewMessagesActivity(1302): inside myadapter getview for messages
01-09 16:35:15.225: E/ViewMessagesActivity(1302): (Cursor)getItem(position) = android.database.sqlite.SQLiteCursor@4217c718
01-09 16:35:15.230: E/ViewMessagesActivity(1302): inside myadapter getview for messages
01-09 16:35:15.235: E/ViewMessagesActivity(1302): (Cursor)getItem(position) = android.database.sqlite.SQLiteCursor@4217c718
01-09 16:35:15.245: E/ViewMessagesActivity(1302): inside myadapter getview for messages
01-09 16:35:15.250: E/ViewMessagesActivity(1302): (Cursor)getItem(position) = android.database.sqlite.SQLiteCursor@4217c718

最佳答案

你有这个

Cursor c = (Cursor)getItem(position);

然后

CheckBox cb = ((CheckBox)v.findViewById(R.id.list_checkbox));
cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
String messageGuid = c.getString(c.getColumnIndex(LoginValidate.C_MESSAGE_GUID));

由于其匿名内部类,它要求 Cursor 是 final。查看文档

http://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html#accessing

为了避免将其声明为实例变量。

private class MyAdapter extends SimpleCursorAdapter implements OnItemClickListener {
Cursor c;

然后在getView

c = (Cursor)getItem(position);

您还应该考虑使用 ViewHolder 模式

http://developer.android.com/training/improving-layouts/smooth-scrolling.html .

此外,您可以在 xml 中使用 1 个布局,而不是 2 个布局 LinearLayoutRelativeLayout

关于View 中的 android Cursor 必须是最终的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21024752/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com