1

我创建了一个TableLayout我在硬代码中存储值的位置。现在我想查看我在另一个页面中单击的行的数据。我尝试了很多方法,但实际上我对实现正确的方法感到困惑。
请提出正确的方法来获取行的值并将它们显示在不同的页面中。

这是我的 xml 表格布局..

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" 
android:background="@drawable/lightbackground"
android:id="@+id/tablepickup"
android:clickable="true"
 android:shrinkColumns="*"
android:stretchColumns="*">

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:scaleType="fitXY"
    android:src="@drawable/d" />

 <TableRow
    android:id="@+id/tableRow1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal" >
</TableRow>

<TableRow
    android:id="@+id/tableRow2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/TextView1"
        android:gravity="center"
        android:text="Items"
        android:textSize="20dp"
        android:textStyle="bold"
        android:typeface="normal" >

    </TextView>

    <TextView
        android:id="@+id/TextView2"
        android:gravity="center"
        android:text="Address"
        android:textSize="18dp"
        android:textStyle="bold"
        android:typeface="normal" >

    </TextView>

    <TextView
        android:id="@+id/TextView3"
        android:gravity="center"
        android:text="PickBy"
        android:textSize="18dp"
        android:textStyle="bold"
        android:typeface="normal" >

    </TextView>

    <TextView
        android:id="@+id/TextView4"
        android:gravity="center"
        android:text="Status"
        android:textSize="18dp"
        android:textStyle="bold"
        android:typeface="normal" >

    </TextView>
</TableRow>

<TableRow
    android:id="@+id/tableRow3"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >



    <TextView
        android:id="@+id/textView6"
        android:gravity="center_horizontal"
        android:text="Dairy Milk" >
    </TextView>

    <TextView
        android:id="@+id/textView7"
        android:gravity="center_horizontal"
        android:text="18,D.H.Rd" >
    </TextView>

    <TextView
        android:id="@+id/textView8"
        android:gravity="center_horizontal"
        android:text="Sushil" >
    </TextView>

    <TextView
        android:id="@+id/textView9"
        android:gravity="center_horizontal"
        android:text="Picked" >
    </TextView>
</TableRow>

<TableRow
    android:id="@+id/tableRow4"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >



    <TextView
        android:id="@+id/textView11"
        android:gravity="center_horizontal"
        android:text="Bournville" >
    </TextView>

    <TextView
        android:id="@+id/textView12"
        android:gravity="center_horizontal"
        android:text="EC5,Sec1" >
    </TextView>

    <TextView
        android:id="@+id/textView13"
        android:gravity="center_horizontal"
        android:text="Hemant" >
    </TextView>

    <TextView
        android:id="@+id/textView14"
        android:gravity="center_horizontal"
        android:text="Picked" >
    </TextView>
</TableRow>
<TableRow
    android:id="@+id/tableRow5"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >



    <TextView
        android:id="@+id/textView15"
        android:gravity="center_horizontal"
        android:text="Gems" >
    </TextView>

    <TextView
        android:id="@+id/textView16"
        android:gravity="center_horizontal"
        android:text="AP5,Sec5" >
    </TextView>

    <TextView
        android:id="@+id/textView17"
        android:gravity="center_horizontal"
        android:text="Suresh" >
    </TextView>

    <TextView
        android:id="@+id/textView18"
        android:gravity="center_horizontal"
        android:text="Pending" >
    </TextView>
</TableRow>

这是我的 .java 页面...

package com.android.pickupdelivery;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TableLayout;
import android.widget.TableRow;

public class Pickuplist extends Activity{

//private static final int i = 0;
TableLayout mtablelayout;
TableRow mtablerow5;
private OnClickListener mListener;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.pickuplist);

    mtablerow5=(TableRow)findViewById(R.id.tableRow5);
    mtablelayout =(TableLayout)findViewById(R.id.tablepickup);
   mtablerow5.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            startActivity(new Intent(Pickuplist.this,Showdetails.class));
        }
    });

我留下了我的 .java 页面不完整,以便您可以自由提出您的宝贵建议...请帮助我...这是我的第一个应用程序涉及TableLayout

4

2 回答 2

1

通过此获取子视图tableRow.getChildAt(int index);将以View形式返回。将其类型转换为TextView. 然后通过获取文本textView.getText()然后将其发送Intent到下一个活动startActivity(new Intent(CurrentActivity,this, NextActivity.class).putExtra("key", "value"));

- - - - - 编辑 - - - - - -

mtablerow5.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

textview = (TextView)mtablerow5.getChildAt(1);
String text = textview.getText().toString;

            startActivity(new Intent(Pickuplist.this,Showdetails.class).putExtra("text1", text));
        }
    });
于 2013-04-26T06:34:16.287 回答
0

在构建表格时,您应该为每列添加偶数 OnClickListener()。

for (int i = 0; i < rows; i++) {

        final TableRow row = new TableRow(this);
        row.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.WRAP_CONTENT));
        // inner for loop
        for (int j = 0; j < cols; j++) {
            final LinearLayout col = (LinearLayout) LayoutInflater.from(
                    DanhSachTuongActivity.this).inflate(
                    R.layout.tuong_attrib_row, null);

            row.addView(col);
            row.setClickable(true);
            col.setClickable(true);
            col.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    int col_id = row.indexOfChild(col);
                    int row_id = tableLayout.indexOfChild(row);
                    Toast.makeText(getApplicationContext(), " "+col_id + " "+row_id, Toast.LENGTH_SHORT).show();

                }
            });

        }
        tableLayout.addView(row);
    }
于 2014-07-28T07:38:03.440 回答