大家,早安,

在学习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;
                }
            }
        }
    }
}

最佳答案

不要在OnClickListener上设置TableLayout,而是将其设置为在该for循环中创建的每个TableRow

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)

关于android - TableLayout中的单击事件问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12491138/

10-12 02:42