我可以在SimpleCursorAdapter listView中放置按钮吗?

时间:2012-02-09 08:07:23

标签: java android

我想在listView中放两个按钮。

   public class Imenik extends ListActivity {

@Override
public long getSelectedItemId() {
 // TODO Auto-generated method stub
 return super.getSelectedItemId();
}

@Override
public int getSelectedItemPosition() {
 // TODO Auto-generated method stub
 return super.getSelectedItemPosition();
}
ListView lv;
Cursor Cursor1;

   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);

       //create a cursor to query the Contacts on the device to start populating a listview
    Cursor1 = getContentResolver().query(
      ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
      null,
         null,
         null,
         null);
    startManagingCursor(Cursor1);

       String[] from = {
         ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
         ContactsContract.CommonDataKinds.Phone.NUMBER,
         ContactsContract.CommonDataKinds.Phone._ID}; // get the list items for the listadapter could be TITLE or URI

 int[] to = {android.R.id.text1,
    android.R.id.text2}; //sets the items from above string to listview

 //new listadapter, created to use android checked template
 SimpleCursorAdapter listadapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_2, Cursor1, from, to );
 setListAdapter(listadapter);

 //adds listview so I can get data from it
 lv = getListView();
 lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

}

}

使用此代码我获取所有联系人姓名和号码并放入列表中,但我想要两个按钮并且在每行checkBox中,用户可以单击项目 - 标记checkBox并单击确认按钮以获取数组中的值。 帮助,谢谢你,沃尔夫。

2 个答案:

答案 0 :(得分:1)

再次......

构建使用您的数据的自定义适配器......

  

每当你想用ListView中的视图进行处理时   需要创建一个可以处理逻辑的自定义适配器   实现并在必要时将该信息传递给视图。

实施例

http://android.vexedlogic.com/2011/04/02/android-lists-listactivity-and-listview-ii-%E2%80%93-custom-adapter-and-list-item-view/

答案 1 :(得分:0)

如果你不想制作customAdapter那么你要为按钮处理做两件事( 它对onitemclick和onitemlongclik 没有影响)

  1. 在xml文件中,按钮视图中的 android:focusable =" false"

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/LinearLayout1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <TextView
            android:id="@+id/tv_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView"
            android:textColor="@android:color/black" />
    
        <TextView
            android:id="@+id/tv_city"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView"
            android:textColor="@android:color/black" />
    
        <TextView
            android:id="@+id/tv_phone"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/button_selector1"
            android:text="TextView" />
    
        <Button
            android:id="@+id/bt_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" 
            android:focusable="false"/>
    

  2. 在简单的游标适配器

    private void showdataquery(){         // TODO自动生成的方法存根

        mhelper = new DAtahelper(this);
        db = mhelper.getReadableDatabase();
    
        String columns[] = { "_id,name,city,phone" };
    
        Cursor c = db.query("vishal", columns, null, null, null, null, null);
        c.moveToFirst();
    
        String[] from = { "name", "city", "phone" };
        int[] to = { R.id.tv_name, R.id.tv_city, R.id.tv_phone };
        SimpleCursorAdapter adapter = new SimpleCursorAdapter(
                getApplicationContext(), R.layout.showdata, c, from, to, 0) {
    
            @Override
            public View newView(Context context, Cursor cursor, ViewGroup parent) {
                // TODO Auto-generated method stub
                return super.newView(context, cursor, parent);
            }
    
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                // TODO Auto-generated method stub
    
    
                return super.getView(position, convertView, parent);
            }
    
            @Override
            public void bindView(View view, Context context, Cursor cursor) {
                // TODO Auto-generated method stub
                final int row_id = cursor.getInt(cursor.getColumnIndex("_id")); //
    
    
                Button button = (Button) view.findViewById(R.id.bt_button);
    
    
                button.setOnClickListener(new OnClickListener() {
    
                    @Override
                    public void onClick(View arg0) {
                        // TODO Auto-generated method stub
                        Log.i("button click","button");
                    }
                });
    
            }
    
        };
    
        lv_database.setAdapter(adapter);
    
    }
    
    @Override
    public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2,
            long arg3) {
        // TODO Auto-generated method stub
        Log.e("", "long");
        return false;
    }
    
    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        // TODO Auto-generated method stub
    
        Log.e("", "item");
    
    }