English 中文(简体)
在什么地方可以节省检查箱在清单活动中选定的接触?
原标题:Where to save checkbox selected contacts in ListActivity?

I am using a custom List adapter to display a list of all contacts, and the user will then select contacts by clicking a checkbox. What is a good method of saving the selected contacts to preferences, so that I may test against them elsewhere? I was thinking of using SharedPreferences, but so far I have been unable to find a way to save an array to SharedPrefs. I could go the sqlite route, but that seems a bit excessive considering they are already contained in a db, and why couldn t I just reference them there. Just not sure how to even begin to do that... Also, I was thinking of calling this method onDestroy, though that could have some issues as well, so a recommendation on that could help as well. Here is some code (let me know if you need more,,,there s always more) thanks for your supreme knowledge.

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>

联系人 活动(为了简练而重订的一些法典):

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();
}
}
最佳回答

If all you need is to store a list of objects (users) I would skip the DB and just create a JSON object and store that as a String in a shared preference. It obviously depends on how many users you expect to store, but if it s a few hundred or less, you re not going to see any sort of performance loss, and you can avoid dealing with all the db setup.

问题回答

Yea, 您的最佳途径是使用一个ql数据库。 您实际上只有两种选择,即恒定的数据储存和共享的偏好。 由于你想要实际储存一个更加复杂的单一关键价值配对机,你的最佳用心是建立一个精炼的数据库。 就储存数据而言,我只每次用

* 还有其他选择,但这些选择是唯一可以使用的半麻省办法。





相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签