我正在向表中动态添加行。我有几个 textViews[] 添加到 row[] 然后我将 row[] 添加到表中。没关系,但是我想检测点击 textView[] 并删除该行,我还希望删除的 textViews 下面的数量减少。
例子:
1 a a a //(I delete this row)
2 b b b
3 c c c
删除后,我想要这个:
1 b b b
2 c c c
我曾尝试将 onClickListener 添加到一个 textView[],但有时它会删除 row[] 并减少数量,有时不会。
有人可以给我写一些例子来试试吗?
编辑:这是我的代码(我认为这就是所需的全部)这是我的按钮上用于添加行的代码:
public TextView textViewKoeficijent[] = new TextView[50];
public TextView textViewBr[] = new TextView[50];
public TextView textViewObrisi[] = new TextView[50];
public TextView textViewTip[] = new TextView[50];
public TextView textViewPar[] = new TextView[50];
fkoeficijent = Double.valueOf(koeficijent);
koefIzracun = koefIzracun * fkoeficijent;
TextView textViewKoeficijentIzracun = (TextView) findViewById(R.id.textViewUkupniKoeficijentIzracun);
koefIzracun = Math.round(koefIzracun*100)/100.0d;
koefIzracunString = String.valueOf(koefIzracun);
textViewKoeficijentIzracun.setText(koefIzracunString);
final TableLayout PopisParova = (TableLayout) findViewById(R.id.TableLayout);
final TableRow noviPar[] = new TableRow[50];
LayoutParams paramsBroj = new LayoutParams(0, LayoutParams.WRAP_CONTENT, 1);
paramsBroj.setMargins(4, 0, 2, 4);
LayoutParams paramsPar = new LayoutParams(0, LayoutParams.WRAP_CONTENT, 2);
paramsPar.setMargins(2, 0, 2, 4);
LayoutParams paramsKoef = new LayoutParams(0, LayoutParams.WRAP_CONTENT, 2);
paramsKoef.setMargins(2, 0, 4, 4);
//onclicklistener:
OnClickListener onClickListener = new View.OnClickListener() {
@Override
public void onClick(View view) {
// TODO Auto-generated method stub
id = view.getId();
brpara --;
TextView textViewKoeficijentIzracun = (TextView) findViewById(R.id.textViewUkupniKoeficijentIzracun);
if(brpara==1){
textViewKoeficijentIzracun.setText("0");
koefIzracun = 1;
}
else{
koeficijent = textViewKoeficijent[id].getText().toString();
fkoeficijent = Double.valueOf(koeficijent);
koefIzracun = koefIzracun / fkoeficijent;
koefIzracun = Math.round(koefIzracun*100)/100.0d;
koefIzracunString = String.valueOf(koefIzracun);
textViewKoeficijentIzracun.setText(koefIzracunString);}
PopisParova.removeViewAt(id);
//PopisParova.removeView(noviPar[id]);
for(i=1; i<=brpara; i++){
if(i>id){
String bri = String.valueOf(i-1);
textViewBr[i].setText(bri);
textViewObrisi[i].setDrawingCacheBackgroundColor(i-1);
}
}
}};
{
textViewBr[brpara] = new TextView(MainActivity.this);
textViewBr[brpara].setLayoutParams(paramsBroj);
textViewBr[brpara].setGravity(Gravity.CENTER_HORIZONTAL);
textViewBr[brpara].setBackgroundColor(0xFFFFFFFF);
brojPara = String.valueOf(brpara);
textViewBr[brpara].setText(brojPara);
textViewBr[brpara].setId(brpara);
}
{
textViewPar[brpara] = new TextView(MainActivity.this);
textViewPar[brpara].setLayoutParams(paramsPar);
textViewPar[brpara].setGravity(Gravity.CENTER_HORIZONTAL);
textViewPar[brpara].setBackgroundColor(0xFFFFFFFF);
textViewPar[brpara].setText(par);
textViewPar[brpara].setId(brpara);
}
{
textViewTip[brpara] = new TextView(MainActivity.this);
textViewTip[brpara].setLayoutParams(paramsPar);
textViewTip[brpara].setGravity(Gravity.CENTER_HORIZONTAL);
textViewTip[brpara].setBackgroundColor(0xFFFFFFFF);
textViewTip[brpara].setText(tip);
textViewTip[brpara].setId(brpara);
}
{
textViewKoeficijent[brpara] = new TextView(MainActivity.this);
textViewKoeficijent[brpara].setLayoutParams(paramsPar);
textViewKoeficijent[brpara].setGravity(Gravity.CENTER_HORIZONTAL);
textViewKoeficijent[brpara].setBackgroundColor(0xFFFFFFFF);
textViewKoeficijent[brpara].setText(koeficijent);
textViewKoeficijent[brpara].setId(brpara);
}
{
textViewObrisi[brpara] = new TextView(MainActivity.this);
textViewObrisi[brpara].setLayoutParams(paramsKoef);
textViewObrisi[brpara].setGravity(Gravity.CENTER_HORIZONTAL);
textViewObrisi[brpara].setBackgroundColor(0xFFFFFFFF);
textViewObrisi[brpara].setText("X");
textViewObrisi[brpara].setId(brpara);
textViewObrisi[brpara].setClickable(true);
textViewObrisi[brpara].setOnClickListener(onClickListener);
}
noviPar[brpara] = new TableRow(MainActivity.this);
noviPar[brpara].addView(textViewBr[brpara]);
noviPar[brpara].addView(textViewPar[brpara]);
noviPar[brpara].addView(textViewTip[brpara]);
noviPar[brpara].addView(textViewKoeficijent[brpara]);
noviPar[brpara].addView(textViewObrisi[brpara]);
PopisParova.addView(noviPar[brpara]);
brpara++;
editTextPar.setText("");
editTextTip.setText("");
editTextKoeficijent.setText("");
编辑 [2]: brpara 是我的计数器,所以我知道要添加的行数。我最多有 20 行。此外,我的循环非常适合删除一行,但是当我一次删除多行时,它只会第一次更改我的行号,之后只会删除行。