簡體   English   中英

Android TableLayout通過單擊按鈕,使用EditText中的數據添加新行

[英]Android TableLayout Add new Row with data from EditText by clicking a button

我在MainActivity上有一個空的tablelayout設置,還有另一個名為Add_Item活動,其中有一些EditText ,用戶可以在其中提交信息以添加到tablelayout 卡住的部分是如何在用戶每次按下保存按鈕時將插入到EditText的數據實際保存到新行中。 是否有教程提供有關如何使用示例代碼執行此操作的信息? 我只用不需要的sql查找那些。 下面是到目前為止我關於XML和Add_Item活動的代碼

// Add_Item活動

package com.example.moduleonesimpleapplication;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

public class Add_Item extends ActionBarActivity {
    //variables
    EditText TaskNameET;
    Spinner SpinType;
    Button SaveTodo;
    String SpinnerOptions[] = {"OptionOne", "OptionTwo", "OptionThree"};


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add__item);

        //define vars
        TaskNameET = (EditText) findViewById(R.id.TaskNameET);
        SpinType = (Spinner) findViewById(R.id.spinner1);
        SaveTodo = (Button) findViewById(R.id.button1);

        //adapter for spinner
        ArrayAdapter<String> ard=new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line, SpinnerOptions);
        SpinType.setAdapter(ard);

        //onclick todobutton
        SaveTodo.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                //here is the save button, I would like to save the information inputted from the TaskNameET EditText Into the TableLayout
                // I have the tablelayout defined as TableRow
                //MainActivity.this.TableRow.setTextAlignment();
                Add_Item.this.TaskNameET.getText().toString();
            }
        });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.add__item, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

//用於表格布局的XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.moduleonesimpleapplication.MainActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:text="@string/AddItemButton" />

    <TableLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" >

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

        </TableRow>

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

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

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

</RelativeLayout>

你的直覺告訴我嗎? 現在應該告訴您,您采用的這種方法是死路一條,是一個障礙。 嗯,從技術上講,您可以逐步破解並向TableLayout動態添加行,但這僅是... hack。 為什么? 因為,Android SDK附帶了更好的選項和功能。

無論哪種方式,您都需要以某種方式保留用戶輸入的數據。 盡管sqlite不是唯一的選擇,但IMO是最具擴展性的解決方案。 這里有一些想法...

  • 忘記TableLayout使用ListView代替
  • 如果不需要在不同的應用程序會話之間持久存儲數據,則將使用的數據存儲在內存中
  • 將數據存儲在文件或首選項中(如果需要持久存儲並且不大)
  • 將儲存在SQLite資料庫中...強烈建議

現在,繼續使用我上面指出的條款搜索一些教程,但由於此處有違本網站的使用條款,因此請不要在此處索取教程。 祝好運

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM