0

嘿,在我的代码中,我有一个由我调用的方法动态填充的表。添加时的表格行有一个不可见的视图,我想在单击时为每个特定行显示它,而所有其他保持隐藏的行是我的代码

public void createTable(String itemName,String itemQuantity,String itemPrice,String itemAmount){
    final TableRow tr1 = new TableRow(getApplicationContext());
    tr1.setId(id);
    tr1.setClickable(true);

    tr1.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
        for(int i = 0;i<lytTableData.getChildCount();i++){
        tr1.getChildAt(4).setVisibility(View.INVISIBLE);
        }
        tr1.getChildAt(4).setVisibility(View.VISIBLE);
        }

    });

    tr1.setLayoutParams(new LayoutParams( LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
    EditText textViewItemQuantity;
    TextView textViewItemName,textViewItemPrice,textViewItemAmount;

    textViewItemName= new TextView(this);textViewItemQuantity= new EditText(this);textViewItemPrice= new TextView(this);textViewItemAmount= new TextView(this);
    iv2 = new ImageView(this);
    textViewItemName.setWidth(500);textViewItemQuantity.setWidth(100);textViewItemPrice.setWidth(100);textViewItemAmount.setWidth(100);
    textViewItemName.setLayoutParams(new TableRow.LayoutParams((int) getResources().getDimension(R.dimen.textsize), LayoutParams.WRAP_CONTENT, 6f));
    textViewItemQuantity.setLayoutParams(new TableRow.LayoutParams((int) getResources().getDimension(R.dimen.textsize2), LayoutParams.WRAP_CONTENT, 1f));
    textViewItemPrice.setLayoutParams(new TableRow.LayoutParams((int) getResources().getDimension(R.dimen.textsize2), LayoutParams.WRAP_CONTENT, 1f));
    textViewItemAmount.setLayoutParams(new TableRow.LayoutParams((int) getResources().getDimension(R.dimen.textsize2), LayoutParams.WRAP_CONTENT, 1f));
    textViewItemName.setText(itemName);textViewItemQuantity.setText(itemQuantity);textViewItemPrice.setText(itemPrice);textViewItemAmount.setText(itemAmount);  
    textViewItemQuantity.setTextAppearance(getApplicationContext(), android.R.style.TextAppearance_Large); textViewItemPrice.setTextAppearance(getApplicationContext(), android.R.style.TextAppearance_Large);
    textViewItemName.setTextAppearance(getApplicationContext(), android.R.style.TextAppearance_Large); textViewItemAmount.setTextAppearance(getApplicationContext(), android.R.style.TextAppearance_Large);
    textViewItemName.setTextColor(0xFF121212);textViewItemQuantity.setTextColor(0xFF121212);textViewItemPrice.setTextColor(0xFF121212);textViewItemAmount.setTextColor(0xFF121212);
    textViewItemName.setPadding(0, 15, 5, 0);textViewItemQuantity.setPadding(0, 15, 5, 0);textViewItemPrice.setPadding(0, 15, 5, 0);textViewItemAmount.setPadding(0, 15, 5, 0);
    textViewItemQuantity.setBackgroundColor(android.R.color.transparent);
    iv2.setLayoutParams(new TableRow.LayoutParams((int) LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));
    iv2.setImageResource(R.drawable.delete);
    iv2.setVisibility(View.INVISIBLE);
    textViewItemQuantity.setOnClickListener(editTextRows);
    tr1.addView(textViewItemName);tr1.addView(textViewItemQuantity);tr1.addView(textViewItemPrice);tr1.addView(textViewItemAmount);tr1.addView(iv2);
    lytTableData.addView(tr1, new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
    Double intItemAmount = Double.parseDouble(itemAmount);
    intTotalAmount = intTotalAmount + intItemAmount;
    intTotalAmount2 = intTotalAmount2 + intItemAmount;
    txtTotal.setText(String.valueOf(intTotalAmount));
    //txtDiscount.setText("0");
    txtAmount.setText(String.valueOf(intTotalAmount));
    id = id + 1;
        System.out.println(tr1.getId());
}

使用这种方法,我每次调用它时都可以创建一个新行,我可能缺少什么来使它工作只是为了清楚什么不起作用是它显示的不可见视图的显示,但不是我想要的方式. 我希望它显示在单击的行上,其余所有保持不可见,当单击另一行时,它会显示,其余所有保持不可见

4

2 回答 2

0

You can do this by defining your dynamic layout (which you want to be displayed on click) in your list view's row layout file, keep it invisible / gone by-default and mark it as visible for clicked row on onItemClickListener event of list view. Hope this help you.

If you are using base adapter then it would be better to solve this issue.

于 2013-09-30T12:40:02.950 回答
0

好吧,感谢所有尝试过的人,我终于弄明白了,这就是答案

public void createTable(String itemName,String itemQuantity,String itemPrice,String itemAmount){
    final TableRow tr1 = new TableRow(getApplicationContext());
    tr1.setId(id);
    tr1.setClickable(true);

    tr1.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub


        TableRow vi = (TableRow) lytTableData.getChildAt(lytTableData.indexOfChild(tr1));
        for(int i = 0;i<lytTableData.getChildCount();i++){
            TableRow vi2 = (TableRow) lytTableData.getChildAt(i);
            vi2.getChildAt(4).setVisibility(View.INVISIBLE);
            }       
        vi.getChildAt(4).setVisibility(View.VISIBLE);
        }

    });

    tr1.setLayoutParams(new LayoutParams( LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
    TextView textViewItemName,textViewItemPrice;

    textViewItemName= new TextView(this);textViewItemQuantity= new EditText(this);textViewItemPrice= new TextView(this);textViewItemAmount= new TextView(this);
    iv2 = new ImageView(this);
    textViewItemName.setWidth(500);textViewItemQuantity.setWidth(100);textViewItemPrice.setWidth(100);textViewItemAmount.setWidth(100);
    textViewItemName.setLayoutParams(new TableRow.LayoutParams((int) getResources().getDimension(R.dimen.textsize), LayoutParams.WRAP_CONTENT, 6f));
    textViewItemQuantity.setLayoutParams(new TableRow.LayoutParams((int) getResources().getDimension(R.dimen.textsize2), LayoutParams.WRAP_CONTENT, 1f));
    textViewItemPrice.setLayoutParams(new TableRow.LayoutParams((int) getResources().getDimension(R.dimen.textsize2), LayoutParams.WRAP_CONTENT, 1f));
    textViewItemAmount.setLayoutParams(new TableRow.LayoutParams((int) getResources().getDimension(R.dimen.textsize2), LayoutParams.WRAP_CONTENT, 1f));
    textViewItemName.setText(itemName);textViewItemQuantity.setText(itemQuantity);textViewItemPrice.setText(itemPrice);textViewItemAmount.setText(itemAmount);  
    textViewItemQuantity.setTextAppearance(getApplicationContext(), android.R.style.TextAppearance_Large); textViewItemPrice.setTextAppearance(getApplicationContext(), android.R.style.TextAppearance_Large);
    textViewItemQuantity.setInputType(InputType.TYPE_CLASS_NUMBER);
    textViewItemName.setTextAppearance(getApplicationContext(), android.R.style.TextAppearance_Large); textViewItemAmount.setTextAppearance(getApplicationContext(), android.R.style.TextAppearance_Large);
    textViewItemName.setTextColor(0xFF121212);textViewItemQuantity.setTextColor(0xFF121212);textViewItemPrice.setTextColor(0xFF121212);textViewItemAmount.setTextColor(0xFF121212);
    textViewItemName.setPadding(0, 15, 5, 0);textViewItemQuantity.setPadding(0, 15, 5, 0);textViewItemPrice.setPadding(0, 15, 5, 0);textViewItemAmount.setPadding(0, 15, 5, 0);
    textViewItemQuantity.setBackgroundColor(0x0000000000);
    iv2.setLayoutParams(new TableRow.LayoutParams((int) LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));
    iv2.setImageResource(R.drawable.delete);
    iv2.setVisibility(View.INVISIBLE);
    iv2.setOnClickListener(new OnClickListener(){

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

    });
    tr1.addView(textViewItemName);tr1.addView(textViewItemQuantity);tr1.addView(textViewItemPrice);tr1.addView(textViewItemAmount);tr1.addView(iv2);
    lytTableData.addView(tr1, new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

    Double intItemAmount = Double.parseDouble(itemAmount);
    intTotalAmount = intTotalAmount + intItemAmount;
    //intTotalAmount2 = intTotalAmount2 + intItemAmount;
    txtTotal.setText(String.valueOf(intTotalAmount));
    //txtDiscount.setText("0");
    txtAmount.setText(String.valueOf(intTotalAmount));
    id = id + 1;
        System.out.println(tr1.getId());
}
于 2013-10-02T14:33:59.203 回答