可扩展的listview子点击事件

时间:2014-11-10 15:11:28

标签: android android-listview

我对可扩展listview的子行使用以下布局

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

    <RelativeLayout
        android:id="@+id/relativeItem"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/labourheader"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:layout_marginLeft="10dp"
            android:paddingBottom="5dp"
            android:textColor="#000000"
            android:textSize="17dip" />

        <TextView
            android:id="@+id/labourrating"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:layout_marginLeft="10dp"
            android:paddingBottom="5dp"
            android:textColor="#000000"
            android:textSize="17dip" />

        <Button
            android:id="@+id/addtasks"
            android:layout_width="30dp"
            android:layout_height="20dp"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="5dp"
            android:background="@drawable/plussquare"
            android:textColor="#000000"
            android:focusable="false"
            android:textSize="17dip" />

        <Button
            android:id="@+id/showtaskbtn"
            android:layout_width="90dp"
            android:layout_height="30dp"
            android:layout_centerVertical="true"
            android:layout_toLeftOf="@id/addtasks"
            android:textColor="#000000"
            android:textSize="17dip" />
    </RelativeLayout>

</LinearLayout>

我想为ADDTASK和showtask按钮添加点击监听器。想要获取标题和子位置的位置。之后我想将此值传递给另一个活动。 我试过但它没有给我点击按钮的正确位置

以下是ADAPTER的代码

public class ListAdapterforTask extends BaseExpandableListAdapter {

    private Context _context;
    private List<CategoryDetail> _listDataHeader;
    private HashMap<CategoryDetail, List<LabourDetail>> _listDataChild;
    LabourDetail b = new LabourDetail();

    public ListAdapterforTask(Context context,
            List<CategoryDetail> listDataHeader,
            HashMap<CategoryDetail, List<LabourDetail>> listChildData) {
        this._context = context;
        this._listDataHeader = listDataHeader;// category name
        this._listDataChild = listChildData;// labor name

    }

    @Override
    public Object getChild(int groupPosition, int childPosititon) {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                .get(childPosititon);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public View getChildView(final int groupPosition, final int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {
        try {

            if (convertView == null) {
                LayoutInflater infalInflater = (LayoutInflater) this._context
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = infalInflater.inflate(R.layout.task_child, null);
            }

            b = _listDataChild.get(_listDataHeader.get(groupPosition)).get(
                    childPosition);

            TextView txtLabourname = (TextView) convertView
                    .findViewById(R.id.labourheader);
            TextView txtLaborrate = (TextView) convertView
                    .findViewById(R.id.labourrating);
            Button buttonAdd = (Button) convertView.findViewById(R.id.addtasks);

            Button btnshowtask = (Button) convertView
                    .findViewById(R.id.showtaskbtn);

            btnshowtask.setVisibility(View.GONE);
            txtLabourname.setText(b.getcFirst_name() + "(" + b.getcLabor_id()
                    + ")");

            AddTaskToDb add = new AddTaskToDbImpl();

            List<TaskDetail> listtask = add.findByLabourid(Integer.parseInt(b
                    .getcLabor_id()));

            for (int i = 0; i < listtask.size(); i++) {
                btnshowtask.setVisibility(View.VISIBLE);

                String firstTask = listtask.get(0).getcTask_Name();

                btnshowtask.setText(firstTask);

            }

            btnshowtask.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
//                  LabourDetail b = listDataChild.get(
//                          categoryList.get(groupPosition)).get(childPosition);
//
//                  Intent i = new Intent(TaskAssigmentActivity.this,
//                          TaskActivity.class);
//                  i.putExtra("catid", b.getcCategory_id());
//                  i.putExtra("labid", b.getcLabor_id());
//                  startActivity(i);
                }
            });

            // String ratingstr=String.valueOf(b.getcRating());
            //
            // int rating=Integer.valueOf(ratingstr);

            txtLaborrate.setText(b.getcRating() + "");

            buttonAdd.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {




                //  int position=(Integer)v.getTag();
                    //b = _listDataChild.get(_listDataHeader.get(groupPosition)).get(
                    //      position);

                    Intent i = new Intent(_context, TaskActivity.class);
                    i.putExtra("catid", b.getcCategory_id());
                    i.putExtra("labid", b.getcLabor_id());
                    _context.startActivity(i);
                }
            });

        } catch (Exception e) {
        }

        return convertView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                .size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return this._listDataHeader.get(groupPosition);
    }

    @Override
    public int getGroupCount() {
        return this._listDataHeader.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.task_assign_group,
                    null);
        }

        TextView lblListHeader = (TextView) convertView
                .findViewById(R.id.lblListHeader);
        TextView lbllabournum = (TextView) convertView
                .findViewById(R.id.labourNumberabsent);

        List<LabourDetail> list = _listDataChild.get(_listDataHeader
                .get(groupPosition));

        int count = list.size();
        String counter = String.valueOf(count);
        lbllabournum.setText(counter);

        lblListHeader.setTypeface(null, Typeface.BOLD);

        lblListHeader.setText(_listDataHeader.get(groupPosition)
                .getcCategory_Name());

        return convertView;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

}

1 个答案:

答案 0 :(得分:1)

@Override
    public View getChildView(final int groupPosition, final int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {
        try {
            b = (LabourDetail) getChild(groupPosition, childPosition);

            Button buttonAdd = (Button) convertView.findViewById(R.id.addtasks);

            AddTaskToDb add = new AddTaskToDbImpl();

            List<TaskDetail> listtask = add.findByLabourid(Integer.parseInt(b
                    .getcLabor_id()));

            buttonAdd.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                   //HERE YOU HAVE ACCESS TO
                   //GROUPPOSITION AND CHILDPOSITION
                   Intent intent = new Intent(_context, TaskActivity.class);
                   intent.putExtra("GroupPosition", groupPosition);
                   intent.putExtra("ChildPosition", childPosition);
                   _context.startActivity(intent);
                }
            });

        } catch (Exception e) {
        }

        return convertView;
    }

我只关注你想要的东西:在这里,每次点击一个孩子的buttonAdd时,它会启动TaskActivity,其中包含点击的组和子位置。

希望这有帮助。