0

我正在编写一个 android 应用程序,但在ListView使用CheckBox.

这是模拟器上的截图: https://dl.dropbox.com/u/24223262/help.png

我已经ListView用 CheckBoxes 做了。但我不知道如何找到每个选中条目的行位置。一旦我得到检查的位置,我想实施一个删除选定的操作。

请帮忙!

这是我的代码:

public class ListTest extends ListActivity implements OnCheckedChangeListener {
    @Override 
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        //--- Importing StringsArray---     
        Bundle gotBasketScanner = getIntent().getExtras();
        String[] StringArrayHistory = gotBasketScanner.getStringArray("fromHistory");

        // ---Make Adapter---
        setListAdapter( new ArrayAdapter<String>(ListTest.this,
                    android.R.layout.simple_list_item_multiple_choice,
                    StringArrayHistory));

        // ---Put Array into a ListView and add Checkboxes to Listview
        ListView list = getListView();
        list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    }
}
4

4 回答 4

1

使用onItemClick()in 而不是onCheckedChangeListener...

getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
                            long id) {
         Toast.makeText(getApplicationContext(), "Item "+position+" is clicked",
                              Toast.LENGTH_SHORT).show();
    }
}

这是获取点击位置和 id 的更好方法。

我希望这能解决你的问题。

编辑

我已更新以下行:-

getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {

现在试试你的代码。

于 2012-06-29T04:57:07.590 回答
0

您可以将PrefrenceScreenCheckBoxPreference一起使用,以实现更轻松且可扩展的实施

于 2012-06-29T05:00:54.340 回答
0

您可以使用 CheckedTextView 来实现相同的功能:

主要的.xml

  <?xml version="1.0" encoding="utf-8"?>
  <CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:clickable="true"
    android:focusable="true"
    android:checkMark="?android:attr/listChoiceIndicatorMultiple">
  </CheckedTextView>

添加列表.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" >

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

  <Button android:id="@+id/click"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Click"/>
 </LinearLayout>

这是活动GetCheckedTextPositionActivity。单击按钮后,您将检查列表项的位置:

 public class GetCheckedTextPositionActivity extends Activity {

private ListView myList;
private EfficientAdapter myAdapter;
private ArrayList<String> data;
private LayoutInflater mInflater;
private Button click;
private ArrayList<Integer> positions;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.addlist);

    positions = new ArrayList<Integer>();

    data = new ArrayList<String>();
    data.add("Bangalore");
    data.add("Delhi");
    data.add("Patna");
    data.add("Mumbai");

    click = (Button)findViewById(R.id.click);
    mInflater = LayoutInflater.from(getApplicationContext());
    myList = (ListView) findViewById(R.id.mainListView);
    myAdapter = new EfficientAdapter(data, getApplicationContext());
    myList.setAdapter(myAdapter);

    click.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            if(positions.size() != 0){
                for(Integer pos : positions){
                Log.e("Positions checked :"," Positions :"+pos);
                }
            }

        }
    });

}

public class EfficientAdapter extends BaseAdapter {
    private ArrayList<String> myData;

    public EfficientAdapter(ArrayList<String> mData, Context ctx) {
        // TODO Auto-generated constructor stub
        this.myData = mData;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return myData.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return myData.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        final int currentPos = position;

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.main, null);
        }

        ((CheckedTextView) convertView).setText(myData.get(position));
        ((CheckedTextView) convertView).setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                        ((CheckedTextView) v).toggle();
                        if(((CheckedTextView) v).isChecked()){
                            positions.add(currentPos);
                        }
                    }
                });
        return convertView;
    }

}
 }

希望这可以帮助。

于 2012-06-29T05:54:31.303 回答