csh_34 2013-04-10 09:57 采纳率: 0%
浏览 3169
已采纳

如何从自定义列表视图获得选中的项?

程序中的自定义列表视图的每一行都包含许多TextViews,当我点击List Item时,OnListItemClick()没有被调用。如何获取一个自定义列表视图中选中的项?
XML for Row:

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

    <TextView
        android:id="@+id/priorityView"
        android:layout_width="20dip"
        android:layout_height="match_parent"
        android:background="#cccccc"
        android:layout_marginTop="2dip"
        android:layout_marginBottom="2dip" />


    <LinearLayout 
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:weightSum="1.0"
        >
        <TextView
        android:id="@+id/dateText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:paddingBottom="2dip"
        android:paddingLeft="5dip"
        android:paddingRight="5dip"
        android:paddingTop="2dip"
        android:text="@string/empty"
        android:textSize="20dip"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/monthText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:padding="5dip"
        android:text="@string/empty"
        android:textSize="10dip" >
    </TextView>

    </LinearLayout>



    <TextView
        android:id="@+id/timeText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:padding="5dip"
        android:text="@string/empty" >
    </TextView>

    <TextView
        android:id="@+id/titleText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:padding="5dip"
        android:text="@string/empty"
        android:textStyle="bold"
        android:layout_weight="1.0" >
    </TextView>

    <CheckBox
        android:id="@+id/selectedCheckbox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:padding="5dip" >
    </CheckBox>

</LinearLayout>

XML for ListView:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true" >
    </ListView>
   </RelativeLayout>

Java

public class MainActivity extends ListActivity {


    private AppointmentsDB dbHelper;
    private Cursor cursor;
    private AppointmentsCursorAdapter cursorAdapter;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        dbHelper = new AppointmentsDB(this);

        StringBuilder selectQuery = new StringBuilder();
        selectQuery.append("SELECT "+AppointmentsDB.KEY_ID+",");
        selectQuery.append(AppointmentsDB.KEY_TITLE + ", ");
        selectQuery.append(AppointmentsDB.KEY_DESCRIPTION + ", ");
        selectQuery.append(AppointmentsDB.KEY_PRIORITY + ", ");
        selectQuery.append(AppointmentsDB.KEY_DATE_TIME + ", ");
        selectQuery.append(AppointmentsDB.KEY_DURATION + ", ");
        selectQuery.append(AppointmentsDB.KEY_ALARM_TIME + " FROM "
                + AppointmentsDB.DATABASE_TABLE + " ");
        selectQuery.append("ORDER BY " + AppointmentsDB.KEY_DATE_TIME + " ASC");

        cursor = dbHelper.openReadableDatabase().rawQuery(
                selectQuery.toString(), null);

        String[] columnNames = new String[] {  };
        int[] ids = new int[] { };

        cursorAdapter = new AppointmentsCursorAdapter(this,
                R.layout.row, cursor, columnNames, ids);
        this.setListAdapter(cursorAdapter);
    }

    // This class sets our customised layout for the ListView
    class AppointmentsCursorAdapter extends SimpleCursorAdapter {

        private int layout;
        private int[] colours;

        @SuppressWarnings("deprecation")
        public AppointmentsCursorAdapter(Context context, int layout, Cursor c,
                String[] from, int[] to) {
            super(context, layout, c, from, to);


            this.layout = layout;
            colours = new int[] {
                    context.getResources().getColor(R.color.light_gray),
                    context.getResources().getColor(R.color.green),
                    context.getResources().getColor(R.color.orange),
                    context.getResources().getColor(R.color.brick_red) };
        }

        public View newView(Context context, Cursor cursor, ViewGroup parent) {
            LayoutInflater inflater = LayoutInflater.from(context);
            View view = inflater.inflate(layout, parent, false);

            TextView titleText = (TextView) view.findViewById(R.id.titleText);
            TextView priorityView = (TextView) view
                    .findViewById(R.id.priorityView);
            TextView dateText = (TextView) view.findViewById(R.id.dateText);
            TextView monthText = (TextView) view.findViewById(R.id.monthText);
            TextView timeText = (TextView) view.findViewById(R.id.timeText);

            String title = cursor.getString(cursor
                    .getColumnIndex(AppointmentsDB.KEY_TITLE));

            int priority = 0;
            String _priority = cursor.getString(cursor.getColumnIndex(AppointmentsDB.KEY_PRIORITY));
            if(_priority != null)
                priority = Integer.parseInt(_priority);

            long dateTime = Long.parseLong(cursor.getString(cursor
                    .getColumnIndex(AppointmentsDB.KEY_DATE_TIME)));

            Calendar calendar = new GregorianCalendar();
            calendar.setTimeInMillis(dateTime);

            SimpleDateFormat timeFormat = new SimpleDateFormat(
                    "dd,MMM,HH:mm aaa");
            String[] tokens = timeFormat.format(calendar.getTime()).split(",");
            dateText.setText(tokens[0]);
            monthText.setText(tokens[1]);
            timeText.setText(tokens[2]);

            titleText.setText(title);
            priorityView.setBackgroundColor(colours[priority]);

            return view;
        }

        @Override
        public void bindView(View view, Context context, Cursor cursor) {
            super.bindView(view, context, cursor);

            TextView titleText = (TextView) view.findViewById(R.id.titleText);
            TextView priorityView = (TextView) view
                    .findViewById(R.id.priorityView);
            TextView dateText = (TextView) view.findViewById(R.id.dateText);
            TextView monthText = (TextView) view.findViewById(R.id.monthText);
            TextView timeText = (TextView) view.findViewById(R.id.timeText);

            String title = cursor.getString(cursor
                    .getColumnIndex(AppointmentsDB.KEY_TITLE));
            int priority =0;
            String _priority = cursor.getString(cursor
                    .getColumnIndex(AppointmentsDB.KEY_PRIORITY));
            if(_priority != null)
                priority = Integer.parseInt(_priority);


            long dateTime = Long.parseLong(cursor.getString(cursor
                    .getColumnIndex(AppointmentsDB.KEY_DATE_TIME)));

            Calendar calendar = new GregorianCalendar();
            calendar.setTimeInMillis(dateTime);

            SimpleDateFormat timeFormat = new SimpleDateFormat(
                    "dd,MMM,HH:mm aaa");
            String[] tokens = timeFormat.format(calendar.getTime()).split(",");
            dateText.setText(tokens[0]);
            monthText.setText(tokens[1]);
            timeText.setText(tokens[2]);

            titleText.setText(title);
            priorityView.setBackgroundColor(colours[priority]);

        }
    }
}
  • 写回答

2条回答 默认 最新

  • balmy 2013-04-10 14:34
    关注

    是不是每行的复选框checkbox把焦点抢掉了,试试设置下焦点
    checkbox中添加android:focusable="false"

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符
  • ¥15 NX MCD仿真与博途通讯不了啥情况
  • ¥15 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理
  • ¥15 gradio的web端页面格式不对的问题
  • ¥15 求大家看看Nonce如何配置
  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?
  • ¥15 cgictest.cgi文件无法访问