gpt4 book ai didi

android - 在 ListActivity 中保存复选框选中的联系人的位置?

转载 作者:搜寻专家 更新时间:2023-11-01 09:11:24 24 4
gpt4 key购买 nike

我正在使用自定义列表适配器来显示所有联系人的列表,然后用户将通过单击复选框来选择联系人。将所选联系人保存到首选项的好方法是什么,以便我可以在其他地方对它们进行测试?我正在考虑使用 SharedPreferences,但到目前为止我一直无法找到将数组保存到 SharedPrefs 的方法。我可以走 sqlite 路线,但考虑到它们已经包含在数据库中,这似乎有点过分,为什么我不能在那里引用它们。只是不确定如何开始这样做......此外,我正在考虑调用此方法 onDestroy,尽管这也可能有一些问题,因此对此的建议也可能有所帮助。这是一些代码(如果您需要更多,请告诉我,总是有更多)感谢您的至高无上的知识。

ListItemLayout.xml:

<?xml version="1.0" encoding="utf-8"?>
<TwoLineListItem
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >

<RelativeLayout
android:id="@+id/relativelayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<TextView
android:id="@android:id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:textStyle="bold" />

<TextView
android:id="@android:id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@android:id/text1"
android:includeFontPadding="false"
android:paddingBottom="4dip"
android:textSize="15sp"
android:textStyle="normal" />

<CheckBox
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:focusable="false"
/>
</RelativeLayout>
</TwoLineListItem>

ContactPicker Activity (为简洁起见,对一些代码进行了编辑):

public class ContactPicker extends ListActivity implements Runnable{


private List<Contact> contacts = null;
private Contact con;
private ContactArrayAdapter cAdapter;
private ProgressDialog prog = null;
private Context thisContext = this;
private CheckBox c1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
prog = ProgressDialog.show(this, "Contact List", "Getting Contacts", true, false);
Thread thread = new Thread(this);
thread.start();

final CheckBox c1 = (CheckBox)findViewById(R.id.checkbox);

}
public void run() {...
}
private List<Contact> fillContactsList() {...
}
private Handler handler = new Handler() {...
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
TextView label = ((TwoLineListItem) v).getText2();
String phoneNumber = label.getText().toString();
}
}

最佳答案

如果您只需要存储对象(用户)列表,我会跳过数据库,只创建一个 JSON 对象并将其作为字符串存储在共享首选项中。这显然取决于您希望存储多少用户,但如果是几百个或更少,您将不会看到任何类型的性能损失,并且您可以避免处理所有数据库设置。

关于android - 在 ListActivity 中保存复选框选中的联系人的位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8059871/

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