在活动中设置表数据 - 这种方法有效吗

发布于 2024-10-20 19:31:53 字数 4625 浏览 0 评论 0原文

我有一个简单的表格布局,我想通过初始化特定类型来填充它 我的 onCreate 方法中的对象。 布局

首先,这是我的jobs_table.xml

<?xml version="1.0" encoding="utf-8"?>
<TableLayout android:id="@+id/Jobs"
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"
             android:stretchColumns="*"
             xmlns:android="http://schemas.android.com/apk/res/android">
    <TableRow android:id="@+id/JobRow" android:layout_width="wrap_content" android:layout_height="wrap_content">
        <TextView android:id="@+id/JobID" android:layout_column="1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textColor="@color/black"></TextView>
        <TextView android:id="@+id/JobStatus" android:layout_column="2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textColor="@color/black"></TextView>
        <TextView android:id="@+id/Customer" android:layout_column="3" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textColor="@color/black"></TextView>
        <TextView android:id="@+id/Department" android:layout_column="4" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textColor="@color/black"></TextView>
        <TextView android:id="@+id/DocType" android:layout_column="5" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textColor="@color/black"></TextView>
    </TableRow> 
</TableLayout>

和活动

package com.producermobile;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TableLayout;

import com.producermobile.jobs.ProducerJobRows;

public class JobRowsActivity extends Activity { 

    private BatchingJobRows jobRows;    

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.jobs_table);
        jobRows = new BatchingJobRows(this);        
    }
}

现在的想法是,当我创建 jobRows 时,表的数据将由 BatchingJobRows 对象加载,希望该对象可以用于我的活动。不过,我没有办法在 mo 上检查这一点,所以我不确定这个概念是否正确(我主要担心的是向表中添加行的 while 循环)。

无论如何,这是 BatchingJobRows 中发生的事情的简化版本。

package com.producermobile.jobs;

import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import com.producermobile.R;

public class BatchingJobRows  {

    private TableLayout table;
    private View tableView;
    private Context context;
    private HashMap<String,ArrayList<String>> jobs; 
    private ArrayList<String> jobsVals;

    public BatchingJobRows(Context context) {
        this.context = context;
        LayoutInflater inflater = (LayoutInflater) 
            context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        tableView = inflater.inflate(R.layout.jobs_table, null);
        table = (TableLayout) tableView.findViewById(R.id.Jobs);
        jobVals = new ArrayList<String>();
        jobVals.add("HELD");
        jobVals.add("DELL");
        jobVals.add("PRINT");
        jobVals.add("STATEMENT");
        jobs = new HashMap<String,ArrayList<String>>();
        jobs.put("012345",jobVals);
        jobs.put("123456",jobVals);
        this.setRows();
    }

    public void setRows() {
        Iterator<String> keys = jobs.keySet().iterator();
        while(keys.hasNext()) {
            //Init Objects
            TableRow row = (TableRow) TableView.findViewById(R.id.JobRow);
            TextView jobid = (TextView) tableView.findViewById(R.id.JobID);
            TextView jobstatus = (TextView) tableView.findViewById(R.id.JobStatus);
            TextView customer = (TextView) tableView.findViewById(R.id.Customer);
            TextView department = (TextView) tableView.findViewById(R.id.Department);
            TextView doctype = (TextView) tableView.findViewById(R.id.DocType);
            String key = keys.next();
            jobid.setText(key);
            jobstatus.setText(jobs.get(key).get(0));
            customer.setText(jobs.get(key).get(1));
            department.setText(jobs.get(key).get(2));
            doctype.setText(jobs.get(key).get(3));
            row.addView(jobid);
            row.addView(jobstatus);
            row.addView(customer);
            row.addView(department);
            row.addView(doctype);
            table.addView(row);
        }
    }
}

I have a simple table layout that I want to populate just by initialising a specific type
of object in my onCreate method. First off this is the layout I have

jobs_table.xml

<?xml version="1.0" encoding="utf-8"?>
<TableLayout android:id="@+id/Jobs"
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"
             android:stretchColumns="*"
             xmlns:android="http://schemas.android.com/apk/res/android">
    <TableRow android:id="@+id/JobRow" android:layout_width="wrap_content" android:layout_height="wrap_content">
        <TextView android:id="@+id/JobID" android:layout_column="1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textColor="@color/black"></TextView>
        <TextView android:id="@+id/JobStatus" android:layout_column="2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textColor="@color/black"></TextView>
        <TextView android:id="@+id/Customer" android:layout_column="3" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textColor="@color/black"></TextView>
        <TextView android:id="@+id/Department" android:layout_column="4" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textColor="@color/black"></TextView>
        <TextView android:id="@+id/DocType" android:layout_column="5" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textColor="@color/black"></TextView>
    </TableRow> 
</TableLayout>

And the activity

package com.producermobile;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TableLayout;

import com.producermobile.jobs.ProducerJobRows;

public class JobRowsActivity extends Activity { 

    private BatchingJobRows jobRows;    

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.jobs_table);
        jobRows = new BatchingJobRows(this);        
    }
}

Now the idea will be when I create my jobRows the data for the table will be loaded by the BatchingJobRows object, which should hopefully then be available to my activity. I don't have a way of checking this at the mo though so I'm not sure if the concept is right (my main worry is the while loop which is adding rows to the table).

Anyway here's a simplified version of what's happening in the BatchingJobRows.

package com.producermobile.jobs;

import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import com.producermobile.R;

public class BatchingJobRows  {

    private TableLayout table;
    private View tableView;
    private Context context;
    private HashMap<String,ArrayList<String>> jobs; 
    private ArrayList<String> jobsVals;

    public BatchingJobRows(Context context) {
        this.context = context;
        LayoutInflater inflater = (LayoutInflater) 
            context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        tableView = inflater.inflate(R.layout.jobs_table, null);
        table = (TableLayout) tableView.findViewById(R.id.Jobs);
        jobVals = new ArrayList<String>();
        jobVals.add("HELD");
        jobVals.add("DELL");
        jobVals.add("PRINT");
        jobVals.add("STATEMENT");
        jobs = new HashMap<String,ArrayList<String>>();
        jobs.put("012345",jobVals);
        jobs.put("123456",jobVals);
        this.setRows();
    }

    public void setRows() {
        Iterator<String> keys = jobs.keySet().iterator();
        while(keys.hasNext()) {
            //Init Objects
            TableRow row = (TableRow) TableView.findViewById(R.id.JobRow);
            TextView jobid = (TextView) tableView.findViewById(R.id.JobID);
            TextView jobstatus = (TextView) tableView.findViewById(R.id.JobStatus);
            TextView customer = (TextView) tableView.findViewById(R.id.Customer);
            TextView department = (TextView) tableView.findViewById(R.id.Department);
            TextView doctype = (TextView) tableView.findViewById(R.id.DocType);
            String key = keys.next();
            jobid.setText(key);
            jobstatus.setText(jobs.get(key).get(0));
            customer.setText(jobs.get(key).get(1));
            department.setText(jobs.get(key).get(2));
            doctype.setText(jobs.get(key).get(3));
            row.addView(jobid);
            row.addView(jobstatus);
            row.addView(customer);
            row.addView(department);
            row.addView(doctype);
            table.addView(row);
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

莳間冲淡了誓言ζ 2024-10-27 19:31:53

好吧,这不是对您问题的直接答案,但我认为您应该做的是创建一个列表活动,并将此功能移至某种类型的 ListAdapter 中。我们可以帮助您解决如何设置的问题。它可能比您要走的路线更容易,并且对这个板上的人来说更熟悉。所以我建议对此采取传统方法。根据您的需要使用 ListActivity、ListAdapter Base 或 Cursor。

Well this is not a direct answer to your question but what I think you should do is create a list activity, and move this functionality down into a ListAdapter of some type. We can help you with question on how to set that up. Its probably easier than the route you are going, and more familiar to people on this board. So i would recommend taking the conventional approach on this. Use ListActivity, and ListAdapter Base or Cursor depending on your needs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文