从android中的表格布局中的每一行获取值

时间:2013-04-26 06:18:58

标签: android

我已经创建了一个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

2 个答案:

答案 0 :(得分: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));
        }
    });

答案 1 :(得分: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);
    }