Functional Clickable TableRows that are created dy

2019-08-01 07:44发布

问题:

I saw this link (http://stackoverflow.com/questions/6603868/android-onclicklistener-and-table-layout) which seems like it works for the person asking the question. That being said, here's my situation.

I have a TableLayout, dynamically filled with four columns of data. The row as a whole needs to be clickable, since there are no buttons like the example linked above.

A clicked row needs to pass its first column's(column 0) data, which is just a string. Here is the function that's called to create a new row.

private void addLotTableRow(CharSequence [] row, int count) {
    final TableLayout table = (TableLayout) findViewById(R.id.lotsTableList);
    final TableRow tr = (TableRow) getLayoutInflater().inflate(R.layout.lotsrow, null);
    TextView tv;
    // Fill Cells
        tv = (TextView) tr.findViewById(R.id.lotCell1);//Cell 1: Lot Number
        tv.setText(row[0]);

        tv = (TextView) tr.findViewById(R.id.lotCell2);//Cell 2: Sample
        tv.setText(row[1]);

        tv = (TextView) tr.findViewById(R.id.lotCell3);//Cell 3: Inspected
        tv.setText(row[3]);

        tv = (TextView) tr.findViewById(R.id.lotCell4);//Cell 4: Total
        tv.setText(row[2]);

        table.addView(tr);
}

So I originally tried to make a tr.setOnClickListener(new View.OnclickListener() {blahblah}); in this function, right before the table.addView(tr) line. The "blah blah" was of course an onClick(View v) function. I originally would only get the last row's data, no matter which row I clicked. Now, I'm trying to do like the link above, and produce the onclicks from a higher up function, after all the table rows have been created. I probably can give more info, but I think people can get the idea for now from this! Thanks for any help!

回答1:

Bottom Line: I figured it out!

Basing my solution from the link I posted in the question, I realized that I needed to make a textview object in order to see a cell's data! So here is my code, in connection to the code above that has remained unchanged!

    int rowNumCount = table.getChildCount();
    for(int count = 1; count < rowNumCount; count++) {
        View v = table.getChildAt(count);
        if(v instanceof TableRow) {
            final TableRow clickRow = (TableRow)v;
            int rowCount = clickRow.getChildCount();
            v.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    Context context = getTabHost().getContext();
                    TableRow row = (TableRow)v;
                    TextView tv = (TextView)row.getChildAt(0);
                    CharSequence text = "Lot VALUE Selected: " + tv.getText();
                    int duration = Toast.LENGTH_SHORT;
                    Toast.makeText(context, text, duration).show();
                }
            });
        }
    }

Like I said, I only needed to grab the first columns data, thus the row.getChildAt(0); line! So I know nobody probably even had the chance to answer this question yet, but I hope my answer can help others in the future!

Rationale to the question, "Why not just use a listview?" Answer: I think for what I'm making, a tabular format looks much better.

While I may not make a huge change to my code's setup, I am ALWAYS open to hear changes that could help improve my code! I love this community!