ugurrrrr
ugurrrrr

Reputation: 51

Android - Getting selected row values in a table

I'm getting data from mssql dynamically in my table. I want to get values in selected row as String. For example i want to get "99" , "Yonca" , "Lodi" values for this screen. How can i do this? Here is my creating table code :

public void addData()
{
    for(int i = 0 ; i < protokolNo.length ; i++)
    {
        /**
         * Creating table row here
         */
        tr = new TableRow(this);
        tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));

        tVProtokolNo = new TextView (this);
        tVProtokolNo.setText(protokolNo[i]);
        tVProtokolNo.setTextColor(Color.RED);
        tVProtokolNo.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
        tVProtokolNo.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
        tVProtokolNo.setPadding(5, 5, 5, 5);
        tr.addView(tVProtokolNo);

        TextView tVAdi = new TextView(this);
        tVAdi.setText(adi[i]);
        tVAdi.setTextColor(Color.RED);
        tVAdi.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
        tVAdi.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
        tVAdi.setPadding(5, 5, 5, 5);
        tr.addView(tVAdi);

        TextView tVSoyadi = new TextView(this);
        tVSoyadi.setText(soyadi[i]);
        tVSoyadi.setTextColor(Color.RED);
        tVSoyadi.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
        tVSoyadi.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
        tVSoyadi.setPadding(5, 5, 5, 5);
        tr.addView(tVSoyadi);

        tl.addView(tr, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

        /**
         *  Colouring selected row
         */

        tr.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    v.setBackgroundColor(Color.DKGRAY);
                    v.showContextMenu();


                }
            });
            registerForContextMenu(tr);
    }
}

Upvotes: 0

Views: 6325

Answers (2)

Sergey Benner
Sergey Benner

Reputation: 4431

This is dirty but working. Create a global private variable in your Activity class called TableRow tr1=null; then you can use the following code within your addData() method before registerForContextMenu(tr); call:

   tr.setOnClickListener(new View.OnClickListener() {
    public void onClick(View view) {
        view.setBackgroundColor(Color.DKGRAY);
    }
    });

    tr.setOnLongClickListener(new View.OnLongClickListener() {
            public boolean onLongClick(View v) {
                tr1=(TableRow)v; //assign selected TableRow gobally
                openContextMenu(v);
                return true;
            }
        });

Then use the following in the same Activity to make it work with the context menus as far as I got it from your code:

  @Override
     public void onCreateContextMenu(ContextMenu menu, View v,ContextMenu.ContextMenuInfo menuInfo) {
     super.onCreateContextMenu(menu, v, menuInfo);
         menu.add(0, v.getId(), 0, "Do YourStuff");

     }
    @Override
       public boolean onContextItemSelected(MenuItem item) {
        int ccount= (tr1).getChildCount();
                          String[] str =new String[ccount];
                          for(int i=0;i<ccount;i++)
                          {
                              TextView tv =   (TextView)(((TableRow)tr1)).getChildAt(i);
                              str[i]=tv.getText().toString(); //set selected text data into the String array
                          }
                          Toast.makeText(ListActivity2.this,Arrays.toString(str), 2).show();
        return true;
    }

so basically tr1 variable will host your selected TableRow.

But do as everyone says use the ListView for the tasks like this one and just use RelativeLayouts for the rows or anything.

hope it clarifies it abit.

Upvotes: 0

Senthil Mg
Senthil Mg

Reputation: 3313

It's better use Android ListView with custom adapter,refer the below link.... http://www.vogella.de/articles/AndroidListView/article.html

Upvotes: 1

Related Questions