无法看到我的列表旁边的复选框?

时间:2015-10-08 23:05:08

标签: android checkbox android-listview

我已根据此引用创建了一个项目https://www.youtube.com/watch?v=luxE7oEKiic,我正在尝试将其与此引用http://www.geeks.gallery/multiple-checkbox-values-in-listview-storing-retrieving-using-sharedpreferences/结合使用。但是,我遇到一个问题,复选框没有出现在我安装的应用程序列表旁边。我没有包含的文件之一是array.xml,在我的编码中有一行需要该文件。我对此做了以下更改。 ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_multiple_choice); //我删除了getResources()。getStringArray(R.array.Mobile_OS)这就是我面临这个问题的原因吗?我该怎么做才能纠正这个问题?好心提醒。感谢。

public class MainActivity extends ListActivity{

private PackageManager packageManager = null;
private List<ApplicationInfo> applist = null;
private AppAdapter listadapter = null;

ListView myList;
Button getChoice, clearAll, selectAll;
SharedPreferences sharedpreferences;
public static final String MyPREFERENCES = "MyUserChoice" ;
ArrayList<String> selectedItems = new ArrayList<String>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    myList = (ListView)findViewById(android.R.id.list);
    getChoice = (Button)findViewById(R.id.getchoice);
    clearAll = (Button)findViewById(R.id.clearall);
    selectAll = (Button)findViewById(R.id.selectall);

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice);
    myList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    myList.setAdapter(adapter);
    sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
    if(sharedpreferences.contains(MyPREFERENCES)){
        LoadSelections();
    }

    getChoice.setOnClickListener(new Button.OnClickListener() {


        @Override
        public void onClick(View v) {

            String selected = "";
            int cntChoice = myList.getCount();

            SparseBooleanArray sparseBooleanArray = myList.getCheckedItemPositions();
            for (int i = 0; i < cntChoice; i++) {
                if (sparseBooleanArray.get(i)) {
                    selected += myList.getItemAtPosition(i).toString() + "\n";
                    System.out.println("Checking list while adding:" + myList.getItemAtPosition(i).toString());
                    SaveSelections();
                }

            }

            Toast.makeText(MainActivity.this, selected, Toast.LENGTH_LONG).show();

        }
    });

    clearAll.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            ClearSelections();
        }
    });

    selectAll.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            SelectAllSelections();
        }
    });

    packageManager = getPackageManager();

    new LoadApplications().execute();
}
private void SaveSelections() {
// save the selections in the shared preference in private mode for the user

    SharedPreferences.Editor prefEditor = sharedpreferences.edit();
    String savedItems = getSavedItems();
    prefEditor.putString(MyPREFERENCES.toString(), savedItems);
    prefEditor.commit();
}

private String getSavedItems() {
    String savedItems = "";
    int count = this.myList.getAdapter().getCount();
    for (int i = 0; i < count; i++) {
        if (this.myList.isItemChecked(i)) {
            if (savedItems.length() > 0) {
                savedItems += "," + this.myList.getItemAtPosition(i);
            } else {
                savedItems += this.myList.getItemAtPosition(i);
            }
        }
    }
    return savedItems;
}

private void LoadSelections() {
// if the selections were previously saved load them

    if (sharedpreferences.contains(MyPREFERENCES.toString())) {

        String savedItems = sharedpreferences.getString(MyPREFERENCES.toString(), "");
        selectedItems.addAll(Arrays.asList(savedItems.split(",")));

        int count = this.myList.getAdapter().getCount();

        for (int i = 0; i < count; i++) {
            String currentItem = (String) myList.getAdapter()
                    .getItem(i);
            if (selectedItems.contains(currentItem)) {
                myList.setItemChecked(i, true);
                Toast.makeText(getApplicationContext(),
                        "Curren Item: " + currentItem,
                        Toast.LENGTH_LONG).show();
            } else {
                myList.setItemChecked(i, false);
            }

        }
    }
}

private void ClearSelections() {
// user has clicked clear button so uncheck all the items
    int count = this.myList.getAdapter().getCount();
    for (int i = 0; i < count; i++) {
        this.myList.setItemChecked(i, false);
    }
// also clear the saved selections
    SaveSelections();
}

private void SelectAllSelections() {
// user has clicked clear button so uncheck all the items
    int count = this.myList.getAdapter().getCount();
    for (int i = 0; i < count; i++) {
        this.myList.setItemChecked(i, true);
    }
// also clear the saved selections then uncomment the below line.
// SaveSelections();
}

protected void onListItemClick(ListView l, View v, int position, long id){
    super.onListItemClick(l, v, position, id);

    ApplicationInfo app = applist.get(position);

    try{
        Intent intent = packageManager.getLaunchIntentForPackage(app.packageName);

        if(intent != null){
            startActivity(intent);
        }
    }catch(ActivityNotFoundException e){
        Toast.makeText(MainActivity.this,e.getMessage(), Toast.LENGTH_LONG).show();
    }catch(Exception e){
        Toast.makeText(MainActivity.this,e.getMessage(), Toast.LENGTH_LONG).show();
    }
}

private List<ApplicationInfo> checkForLaunchIntent(List<ApplicationInfo> list){
    ArrayList<ApplicationInfo> appList = new ArrayList<ApplicationInfo>();
    for(ApplicationInfo info : list){
        try{
            if(packageManager.getLaunchIntentForPackage(info.packageName)!=null){
                appList.add(info);
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    return appList;
}

private class LoadApplications extends AsyncTask<Void, Void, Void>{
    private ProgressDialog progress = null;

    protected Void doInBackground(Void... params){
        applist = checkForLaunchIntent(packageManager.getInstalledApplications(PackageManager.GET_META_DATA));
        listadapter = new AppAdapter(MainActivity.this, R.layout.activity_list_app, applist);
        return null;
    }
    protected void onPostExecute(Void result){
        setListAdapter(listadapter);
        progress.dismiss();
        super.onPostExecute(result);
    }
    protected void onPreExecute(){
        progress = ProgressDialog.show(MainActivity.this, null, "Loading apps info...");
        super.onPreExecute();
    }
}
}

main.xml中

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

<Button
    android:id="@+id/getchoice"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:text="Get Choice"/>

<Button
    android:id="@+id/clearall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Clear All" />

<Button
    android:id="@+id/selectall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Select All" />

<ListView
    android:id="@android:id/list"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent">
</ListView>

1 个答案:

答案 0 :(得分:0)

不确定为什么你有2个listViews和2个列表适配器。 第一个适配器看起来没问题,因为它使用Android android.R.layout.simple_list_item_multiple_choice内置的布局,但第二个适配器使用由您创建的R.layout.activity_list_app

尝试将R.layout.activity_list_app替换为android.R.layout.simple_list_item_multiple_choice