如何将父列表项标记到子列表

时间:2016-11-11 05:59:11

标签: android listview

如果我选择子项必须禁用父列表项,如何将父列表项标记为子列表。请帮助我是android的新手。

活动:

这是我的活动,我点击父列表项我会得到子列表对话框。

listView1.setAdapter(adapter);
    adapter.notifyDataSetChanged();
    listView1.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view,
                                       int position, long id) {
            // TODO Auto-generated method stub
            newListitems2.clear();
            newListitems2.addAll(itemsList1);

            // ArrayList newListitems2 = new ArrayList();
            //newListitems2.add(itemsList1);
            dialog = new Dialog(PendingOrdersActitvity.this);
            dialog.setContentView(R.layout.itembumping);
            dialog.show();

            listView1.setTag(position);
            list1 = (ListView) dialog.findViewById(R.id.list1);

            ItemBumpingAdapter adapter2 = new ItemBumpingAdapter(PendingOrdersActitvity.this, newListitems2);
            list1.setAdapter(adapter2);


            Button okButton = (Button) dialog.findViewById(R.id.ok1);
            okButton.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {

                    dialog.dismiss();

                }
            });
            Button cancelButton = (Button) dialog.findViewById(R.id.Cancel1);
            cancelButton.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    dialog.dismiss();
                }


            });
            return true;

        }
    });

父列表适配器:

父列表加载

子适配器:

public View getView(int position, View convertView, ViewGroup parent) {

    ViewHolder holder;
    String item = null, qty = null;
    if (convertView == null) {
        holder = new ViewHolder();
        convertView = inflator.inflate(R.layout.itembumpingadapter, null);
        holder.qty = (TextView) convertView.findViewById(R.id.qty);
        holder.name = (TextView) convertView.findViewById(R.id.item);
        holder.childText = (TextView) convertView
                .findViewById(R.id.childitem);
        holder.qtyChild = (TextView) convertView
                .findViewById(R.id.qtychild);
        holder.checkbox = (CheckBox) convertView.findViewById(R.id.chckbox1);
        convertView.setTag(holder);


    } else {

        holder = (ViewHolder) convertView.getTag();
    }
    parentobjid = newListitems.get(position).getParentobjectid();
    if (!parentobjid.isEmpty()) {
        holder.name.setText("   " + newListitems.get(position).getItemnNameDisplay());
        holder.name.setTextColor(Color.parseColor("#CC0000"));
        holder.qty.setText("      " + String.valueOf(newListitems.get(position)
                .getQuantityDisplay()));
        holder.qty.setTextColor(Color.parseColor("#CC0000"));
    } else {

        holder.name.setText(newListitems.get(position).getItemnNameDisplay());
        holder.qty.setText(String.valueOf(newListitems.get(position)
                .getQuantityDisplay()));
        holder.name.setTextColor(Color.parseColor("#FFFFFF"));
        holder.qty.setTextColor(Color.parseColor("#FFFFFF"));
        holder.checkbox.setChecked(false);

        holder.checkbox.setTag(position);

        holder.checkbox.setOnClickListener(new OnClickListener() {


            @Override
            public void onClick(View v) {

                int pos = (Integer) v.getTag();
                newListitems.get(pos).setChecked(!newListitems.get(pos).isChecked());

            }

        });
        if (newListitems.get(position).isChecked()) {
            holder.name.setEnabled(true);
            holder.name.setBackgroundColor(Color.parseColor("#DCDBDB"));


        } else {
            holder.name.setEnabled(false);
        }


    }
    return convertView;
}

1 个答案:

答案 0 :(得分:0)

如果您有父列表的子列表,则应使用ExpandableListView。如果您不熟悉它,请查看this link以了解更多信息。

OnChildClickListener提供ExpandableListView,点击后会显示孩子的位置和父母的位置:

// Listview on child click listener
expListView.setOnChildClickListener(new OnChildClickListener() {

        @Override
        public boolean onChildClick(ExpandableListView parent, View v,
                int groupPosition, int childPosition, long id) {
            // TODO Auto-generated method stub
            Toast.makeText(
                    getApplicationContext(),
                    listDataHeader.get(groupPosition)
                            + " : "
                            + listDataChild.get(
                                    listDataHeader.get(groupPosition)).get(
                                    childPosition), Toast.LENGTH_SHORT)
                    .show();
            return false;
        }
    });

希望这会有所帮助!!