1

大家,早安,

我在学习 Android 的道路上面临着另一个问题。我使用 CSV 文件的内容制作了一个动态 TableLayout。我需要当我单击/触摸表格中的一行时,颜色应该会改变,然后单击按钮获取同一行的内容。现在我被第一部分困住了,当然我不知道如何获取该行的数据。

我在 LinearLayout 中声明了表格,该表格也在我的布局中的 ScrollView 内部,具有以下属性:

    <ScrollView
    android:id="@+id/scrollMotors"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:layout_alignParentBottom="true"
    android:layout_marginLeft="50dp"
    android:layout_marginRight="50dp"
    android:layout_marginTop="90dp"
    android:layout_marginBottom="50dp" >
    <LinearLayout
        android:id="@+id/layoutMotors"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
        <TableLayout
            android:id="@+id/tableMotors"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:clickable="true"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:gravity="center_vertical"
            android:stretchColumns="*" >

        </TableLayout>
    </LinearLayout>
</ScrollView>

在我的 java 代码中,我声明了该行的创建:

        //Initialization of my table
    my_tableMotors = (TableLayout) findViewById(R.id.tableMotors);
    //This is an ArrayList<ArrayList<String>> that contains the lines of the CSV file,
    //I use this variable as a dynamic Matrix because my CSV file can change its dimensions.
    m = valuesFile.size(); 
    for (n = 0 ; n < m ; n++)
    {
        //Declaration and initialization of my rows
        final TableRow line = new TableRow(MotorActivity.this);
        //Setting the parameters of my row
        line.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
        line.setFocusable(true);
        line.setFocusableInTouchMode(true);
        line.setClickable(true);
        //Initialization of my TextViews that are gonna be the content of each one of the rows in the dynamic TableLayout
        myCol1 = new TextView(MotorActivity.this);
        myCol2 = new TextView(MotorActivity.this);
        myCol3 = new TextView(MotorActivity.this);
        myCol4 = new TextView(MotorActivity.this);
        j = valuesFile.get(n).size();
        for (i = 0 ; i < j ; i++)
        {
            switch(i)
            {
            case 0: 
                if (n == 0)
                {
                    myCol1.setText("Line");
                }
                else
                {
                    myCol1.setText(valuesFile.get(n).get(i)); //Sets value for the column
                }
                line.addView(myCol1);
                break;
            case 1:
                myCol2.setText(valuesFile.get(n).get(i)); //Sets value for the column
                line.addView(myCol2);
                break;
            case 2:
                myCol3.setText(valuesFile.get(n).get(i)); //Sets value for the column
                line.addView(myCol3);
                break;
            case 3:
                myCol4.setText(valuesFile.get(n).get(i)); //I use this variable for some other purpose
                break;
            }
        }
        my_tableMotors.addView(line);
    }
    my_tableMotors.setOnClickListener(new View.OnClickListener() {public void onClick(View v) {onClickedRow(); }});
}

从我在这里看到和阅读的内容来看,最好的方法是使用 setOnClickListener ,这就是我使用在这里找到的两个不同答案所做的事情:

    public void onClickedRow()
{
    m = my_tableMotors.getChildCount();
    for (n = 0 ; n < m ; n++)
    {
        if (my_tableMotors.getChildAt(n).hasFocus())
        {
            my_tableMotors.setBackgroundColor(myColor);
        }
    }
}

现在我根本无法关注 tableLayout,所以如果您在我的代码中发现有问题,或者如果您知道如何帮助我,我将不胜感激!!!!

提前谢谢了 :)。

编辑 1

我找到了获得焦点的方法。我将方法更改为不是整个 TableLayout 而是仅更改为 TableRow,所以最终是这样的:

*之前*

        my_tableMotors = (TableLayout) findViewById(R.id.tableMotors);
    m = valuesFile.size(); 
    for (n = 0 ; n < m ; n++)
    {
        //Declaration and initialization of my rows
        final TableRow line = new TableRow(MotorActivity.this);
        /*Other declarations*/
        j = valuesFile.get(n).size();
        for (i = 0 ; i < j ; i++)
        {
            /*Code*/
        }
        my_tableMotors.addView(line);
    }
    my_tableMotors.setOnClickListener(new View.OnClickListener() {public void onClick(View v) {onClickedRow(); }});
}

*之后*

    my_tableMotors = (TableLayout) findViewById(R.id.tableMotors);
m = valuesFile.size();
for (n = 0 ; n < m ; n++)
{
    //Declaration and initialization of my rows
        final TableRow line = new TableRow(MotorActivity.this);
    /*Other declarations*/
    j = valuesFile.get(n).size();
    for (i = 0 ; i < j ; i++)
    {
        /*Code*/
    }
    line.setOnClickListener(new View.OnClickListener() {public void onClick(View v) {onClickedRow(); }});
    my_tableMotors.addView(line);
}

我还对如何设置线条的颜色进行了更改:

*之前*

my_tableMotors.setBackgroundColor(myColor);

*之后*

my_tableMotors.getChildAt(n).setBackgroundResource(R.drawable.myColor);

现在我正忙于找出如何从 TableRow 中获取数据。一旦我得到那个解决方案或你的答案,我想我的问题已经解决了!!!

编辑 2

在@Luksprog 的帮助下,我可以找到检索内容问题的答案!!!我确实使用他的解决方案使用了下一个代码:

    public void onClickedRow()
{
    TableRow clickedRow = new TableRow(MotorActivity.this);
    m = my_tableMotors.getChildCount();
    for (n = 1 ; n < m ; n++)
    {
        if (my_tableMotors.getChildAt(n).isFocused())
        {
            my_tableMotors.getChildAt(n).setBackgroundResource(R.drawable.highlightTableRow);
            clickedRow = (TableRow) my_tableMotors.getChildAt(n);
            j = clickedRow.getChildCount();
            for (i = 0; i < j ; i++)
            {
                switch(i)
                {
                case 0:
                    myField1 = (TextView) clickedRow.getChildAt(i);
                    break;
                case 1:
                    myField2 = (TextView) clickedRow.getChildAt(i);
                    break;
                case 2:
                    myField3 = (TextView) clickedRow.getChildAt(i);
                    break;
                }
            }
        }
    }
}
4

2 回答 2

4

不要设置OnClickListeneronTableLayout而是将其设置为TableRow您在该 for 循环中创建的每个:

for (n = 0 ; n < m ; n++) {
    //Declaration and initialization of my rows
    final TableRow line = new TableRow(MotorActivity.this);
    line.setOnClickListener(mListener);
    line.setId(1000 + n);
    // ...

哪里mListener是:

OnClickListener mListener = new OnClickListener() {

     public void onClick(View v) {
         // v is the TableRow that was clicked
         v.setBackgroundColor(Color.RED);
         // mClickedPosition is a int field representing the clicked row(to know the position later)
         // if you allow more than one row to be clicked at one time, use a list of ints
         // or something like this
         mClickedPosition = v.getId() - 1000; 
     }
}

要稍后检索该行的内容,您将使用该mClickedPosition变量:

TableRow clickedRow = (TableRow) my_tableMotors.getChildAt(mClickedPosition);
// having the child TableRow that was clicked you could extract any data you want from it
// of course you could simply use the mClickedPosition to extract the data from whatever data structure you have(I'm looking at valuesFile)
于 2012-09-19T09:42:42.213 回答
0

您可以根据需要修改以下代码。给你:-

public void createTable() {
        TableLayout table = (TableLayout) findViewById(R.id.tableMotors);
        table.removeAllViewsInLayout();

        List<String> namesList = new ArrayList<String>();
        namesList.add("name");
        namesList.add("name2");
        namesList.add("name3");

        // display dynamic table rows
        for (int counter = 0; counter < namesList.size(); counter++) {
            TableRow row = new TableRow(this);

            // add the index view to the row
            TextView index = new TextView(this);
            index.setLayoutParams(new LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT));
            index.setId(100);
            index.setTextColor(Color.RED);
            String indexString = String.valueOf(counter + 1);
            index.setText(indexString);

            // textview to display names
            final TextView nameView = new TextView(this);
            nameView.setLayoutParams(new ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT));
            nameView.setId(0);
            nameView.setText(namesList.get(counter));
            nameView.setTextColor(Color.RED);

            RelativeLayout relativeRowContent = new RelativeLayout(this);

            // set the layout params for the control to be added
            RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
                    android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT,
                    android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT);
            rlp.setMargins(10, 0, 0, 0);
            rlp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
            relativeRowContent.addView(index, rlp);

            // add the divider after index
            final TextView indexDivider = new TextView(this);
            indexDivider.setWidth(1);
            indexDivider.setId(101);
            indexDivider.setHeight(80);
            indexDivider.setBackgroundColor(Color.GRAY);

            // display the index
            rlp = new RelativeLayout.LayoutParams(
                    android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT,
                    android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT);
            rlp.setMargins(80, 0, 0, 0);
            rlp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
            relativeRowContent.addView(indexDivider, rlp);

            // set the layout params for the control to be added
            rlp = new RelativeLayout.LayoutParams(
                    android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT,
                    android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT);
            rlp.setMargins(10, 0, 0, 0);
            rlp.addRule(RelativeLayout.RIGHT_OF, indexDivider.getId());
            relativeRowContent.addView(nameView, rlp);

            // finally add the relative row content layout in the table row.
            row.addView(relativeRowContent);
            /* row.setBackgroundResource(R.drawable.row_border_light); */

            // add the row to the table.
            table.addView(row, new TableLayout.LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            row.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    Toast.makeText(TestActivity.this,
                            "" + nameView.getText().toString(),
                            Toast.LENGTH_SHORT).show();

                    // or do something more use full here.

                }
            });

        }
    }
于 2012-09-19T11:38:25.253 回答