如何通过单击子列表项来调用父列表项

时间:2016-11-10 11:31:42

标签: android android-layout listview

通过单击列表视图中的项目,我有一个列表视图,我将获得另一个子列表视图。如何通过单击子项

获取父列表视图的位置

主要活动:

public boolean onItemLongClick(AdapterView parent, View view, int position, long id) {
    newListitems2.clear();

    newListitems2.addAll(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 false;
}

家长名单:

public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub

    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"));
    }

    return convertView;
}

Child ListAdapter:

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) {
                // TODO Auto-generated method stub
                int pos = (Integer) v.getTag();//Cast object to integer
                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)

创建两个列表对象,如

List<String> list_parent;
List<String> list_child;

在父项上点击更新list_child并分配给listview Adapter并设置一个flag列表视图正在显示(如设置flag 0 if {{ 1}}分配给适配器,1分配给孩子)

list_parent检查标志是否为1然后将onBackPressed分配给适配器,如果标志为0,则list_parent

但请记住每次将列表对象分配给适配器调用finish();

这是简单的Listview代码:

notifyDataSetChanged