单击复选框时从ListView项获取ID

时间:2012-08-13 02:41:42

标签: android listview checkbox

我使用带有自定义适配器的ListView来构建包含ckeckboxes的列表。现在我不知道如何从所选框中获取“ID”。

项目的xml文件如下所示:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="5dp">
    <TextView android:id="@+id/class_open"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:visibility="gone" />
    <CheckBox   
        android:id="@+id/checkbox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true" 
        android:onClick="clickHandler" />
    <TextView
        android:id="@+id/medi_name"
        android:text="Test"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_toLeftOf="@id/checkbox" />
</RelativeLayout>

我需要以下值:“class_open”

这是我的活动:

package de.bodprod.dkr;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class MediNotmediUserListNew extends Activity{
    LayoutInflater inflater;
    ArrayList<HashMap<String, Object>> medi_notmedi_items;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.medi_notmedi_user_list_new);

        ListView mediListCheck = (ListView) findViewById(android.R.id.list);
        inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        medi_notmedi_items = new ArrayList<HashMap<String,Object>>();
        SystemDatabaseHandler db = new SystemDatabaseHandler(getApplicationContext());
        medi_notmedi_items = db.getAllNotmedis(); 

        final CustomAdapter adapter = new CustomAdapter(this, R.layout.medi_notmedi_user_list_new, medi_notmedi_items);
        mediListCheck.setAdapter(adapter);

    }

    public void clickHandler(View view) {

    }


    private class CustomAdapter extends ArrayAdapter<HashMap<String, Object>>{
        public CustomAdapter(Context context, int textViewResourceId, ArrayList<HashMap<String, Object>> Strings) {
            super(context, textViewResourceId, Strings);
        }
        private class ViewHolder{
            TextView class_open, title;
        }
        ViewHolder viewHolder;

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if(convertView==null){        
                convertView=inflater.inflate(R.layout.medi_notmedi_user_list_new_item, null);
                viewHolder=new ViewHolder();
                viewHolder.class_open=(TextView) convertView.findViewById(R.id.class_open);
                viewHolder.title=(TextView) convertView.findViewById(R.id.medi_name);
                convertView.setTag(viewHolder);
            }else{
                viewHolder=(ViewHolder) convertView.getTag();
            }
            viewHolder.class_open.setText(medi_notmedi_items.get(position).get("sql_id").toString());
            viewHolder.title.setText(medi_notmedi_items.get(position).get("wirk").toString());
            return convertView;
        }
    }       
}

如何从clickHandler中的class_open字段中获取值?

  1. 是否可以直接将值分配给复选框并请求它?
  2. 是否可以从class_open字段请求值?
  3. 请原谅我的坏英语

    马库斯

0 个答案:

没有答案