Android CHOICE_MODE_MULTIPLE框未显示

时间:2011-12-09 22:01:27

标签: android list

我有以下代码从数据库返回一个列表。我想使用复选框列表,以便人们可以在具有某些功能的海滩上进行搜索,但我无法显示复选框,只显示正常列表!

public class FacilitySearch extends ListActivity  {

    private DatabaseHelper dbBookHelper = null;
    private Cursor ourCursor = null;
    private BookAdapter adapter = null;
    public final static String ID_EXTRA="com.fnesse.beachguide._ID";

    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        ListView myListView = getListView();
        myListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        myListView.setOnItemClickListener(onListClick);

        dbBookHelper = new DatabaseHelper(this);
        dbBookHelper.createDatabase();       
        ourCursor = dbBookHelper.getFacilities(); 
        startManagingCursor(ourCursor);
        adapter=new BookAdapter(ourCursor);
        myListView.setAdapter(adapter);
        myListView.setItemsCanFocus(false);
    }

    private AdapterView.OnItemClickListener onListClick = new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
        {       
            Intent i = new Intent(FacilitySearch.this, Administration.class);
            i.putExtra(ID_EXTRA, String.valueOf(id));
            startActivity(i);
        }
    };

    class BookAdapter extends CursorAdapter {
        BookAdapter(Cursor c) {
          super(FacilitySearch.this, c);
        }
        @Override
        public void bindView(View row, Context ctxt, Cursor c) {
            BookHolder holder = (BookHolder)row.getTag();
            holder.populateFrom(c, dbBookHelper);
        }
        @Override
        public View newView(Context ctxt, Cursor c, ViewGroup parent) {
          LayoutInflater inflater=getLayoutInflater();
          View row=inflater.inflate(R.layout.row2, parent, false);
          BookHolder holder=new BookHolder(row);
          row.setTag(holder);
          return(row);
        }
      }

    static class BookHolder {
        private TextView name = null;

        BookHolder(View row) {
            name=(TextView)row.findViewById(R.id.beachListText);
        }

        void populateFrom(Cursor c, DatabaseHelper r) {
            name.setText(r.getName(c));
        }
    }

有什么想法吗?

干杯,

麦克

1 个答案:

答案 0 :(得分:0)

我认为您需要在列表项中包含带

的CheckedTextView
android:id="@android:id/text1" 

如android.R.layout.simple_list_item_multiple_choice resource所示。

当你扩展ListActivity并定义自己的主要布局时,你需要有一个带有id的ListView

android:id="@id/android:list"