TableLayout中的单击事件存在问题



大家早上好,

我在学习安卓的道路上面临着另一个问题。我用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)

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

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.
}
});
}
}

最新更新