Android:Get 从 ListView 中选择的项目

Android:Get Selected Item from ListView

我正在编写一段代码,用户可以在其中选择一些敏感的应用程序来锁定它们。我正在使用 API22 中名为 android.R.layout.simple_list_item_multiple_choice 的预定义布局,其中包含 CheckedTextView。我无法检索用户选择了哪些项目。我在 Listener 中尝试了一些东西,但我无法获得所需的输出。 请帮助我走上正轨。

SensitiveApps.java

package com.sr.droidlock;

import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Switch;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class SensitiveApps extends ActionBarActivity {

    ListView listView;

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

        getSupportActionBar().setDisplayShowHomeEnabled(true);
        getSupportActionBar().setIcon(R.drawable.sensitive);

        final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
        mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);

        final List pkgAppsList = getPackageManager().queryIntentActivities(mainIntent, 0);
        ArrayList<String> tasks = new ArrayList<String>();
        String title;

        for (Object object : pkgAppsList) {
            ResolveInfo info = (ResolveInfo) object;
            title = (String) ((info != null) ? getBaseContext().getPackageManager().getApplicationLabel(info.activityInfo.applicationInfo) : "???");
            tasks.add(title);
        }

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, tasks);
        adapter.sort(new Comparator<String>() {
            @Override
            public int compare(String lhs, String rhs) {
                return lhs.compareToIgnoreCase(rhs);
            }
        });
        listView = (ListView) findViewById(R.id.listView);
        listView.setAdapter(adapter);
        listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

        listView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String str = listView.getItemAtPosition(position).toString();
                Toast.makeText(getApplicationContext(), str, Toast.LENGTH_LONG).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                //Do nothing
            }
        });
    }
}

也许这对你有帮助。

int len = listView.getCount();
SparseBooleanArray checked = listView.getCheckedItemPositions();
for (int i = 0; i < len; i++)
if (checked.get(i)) {
 String item = cont_list.get(i);/* do whatever you want with the checked item */
}