0

私のアプリケーションではリストビューがありますが、問題は、使用しているコードがマークされた位置を取得したいということです。

        @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
            System.out.println("Entered Delete Button");
                String selected = "";
                int cntChoice = lv.getCount();
                System.out.println("The Number Of Contacts"+cntChoice);
                SparseBooleanArray x = lv.getCheckedItemPositions();
                System.out.println(x);
                for(int i = 0; i < cntChoice; i++){
                    if(x.get(i)) {
                        selected += lv.getItemAtPosition(i).toString() + "\n";  

                    }
                }   
                Toast.makeText(ViewSelectedContacts.this, 
                        selected, 
                        Toast.LENGTH_LONG).show();
            }
        });

問題は、SparseBooleanArrayが要素を入力していないことです。また、私は追加しました

    android:choiceMode="multipleChoice"

リストビューで..誰かが私が問題を解決するのを手伝ってください

4

1 に答える 1

1
  Try this code (works for me):

// MainActivity

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

       final ListView listTags = (ListView) findViewById(R.id.listPack);
       listTags.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item, mStrings));
       listTags.setOnItemClickListener(new OnItemClickListener() {

           @Override
           public void onItemClick(AdapterView<?> parent, View view,
                   int position, long id) {
               listTags.setSelection(position);
//               Toast.makeText(getParent(), "hello", Toast.LENGTH_LONG).show();
           }
       });



    }


     private String[] mStrings = {

        "Comte", "Coolea", "Cooleney", "Coquetdale", "Corleggy", "Cornish Pepper",
        "Cotherstone", "Cotija", "Cottage Cheese", "Cottage Cheese (Australian)",
        "Cougar Gold", "Coulommiers", "Coverdale", "Crayeux de Roncq", "Cream Cheese",
        "Cream Havarti", "Crema Agria", "Crema Mexicana", "Creme Fraiche", "Crescenza",
        "Croghan", "Crottin de Chavignol", "Crottin du Chavignol", "Crowdie", "Crowley",
        "Cuajada", "Curd", "Cure Nantais", "Curworthy", "Cwmtawe Pecorino",
       };

// Main xml

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

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <ListView
        android:id="@+id/listPack"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginBottom="5dip"
        android:layout_marginLeft="5dip"
        android:layout_marginRight="5dip"
        android:layout_weight="1" >
    </ListView>

        </LinearLayout>

// list_item.xml // layout

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp"
    android:textSize="16sp" >
</TextView>
于 2013-02-08T09:37:12.483 に答える