Search code examples
androideclipserowsandroid-tablelayout

How to dynamically add rows and detect and delete clicked?


I'm dynamically adding rows to the table. I have few textViews[] that I add to the row[] and then I add row[] to the table. That's ok, but then I want to detect click on textView[] and delete that row, also I want the numbers of the textViews below deleted ones to decrease.

example:

1 a a a //(I delete this row)
2 b b b
3 c c c

after deleting, I want to have this:

1 b b b
2 c c c

I have tried adding onClickListener to one textView[], but sometimes it deletes row[] and decreases number, sometimes don't.

Can someone write me some example to try out?

EDIT: Here's my code (I think it's all that's needed for this) This is the code on my button for adding rows:

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

EDIT [2]: brpara is my counter so I know which is number of row which is being added. I have a max of 20 rows. Also, my loop is working perfectly for deleting one row, but when I delete multiple rows at once, it only changes my row numbers for the first time and after that only deletes rows.


Solution

  • The only possible solution is to replace deleted rows, and their data, with rows following, and their data and delete last row. So it looks like the deleted row is really deleted.

    row[x] = row[x+1] // want to delete this
    row[x+1] = row[x+2]
    //... and like that until the last row
    row[last] // delete this row