gpt4 book ai didi

android - TableLayout 中单击事件的问题

转载 作者:行者123 更新时间:2023-11-29 16:11:51 26 4
gpt4 key购买 nike

大家早上好

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

最佳答案

不要在 TableLayout 上设置 OnClickListener,而是将其设置到您在 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com