gpt4 book ai didi

Android TableLayout 通过单击按钮添加来自 EditText 的数据的新行

转载 作者:行者123 更新时间:2023-11-29 21:00:06 25 4
gpt4 key购买 nike

我在 MainActivity 上设置了一个空的 tablelayout 并且我有另一个名为 Add_Item 的 Activity ,其中我有一些 EditText 是用户可以提交信息以添加到 tablelayout 的地方。我坚持的部分是每次用户按下保存按钮时如何将插入到 EditText 中的数据实际保存到新行中。是否有关于如何使用示例代码执行此操作的教程?我只找到那些我不需要的 sql。下面是我到目前为止的 XML 和 Add_Item Activity 的代码

//添加项目 Activity

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 但这一切都将是……破解。为什么?因为,Android SDK 附带了更好的选项和功能。

无论哪种方式,您都需要以某种方式保存用户输入的数据。虽然 sqlite 不是唯一的选择,但在我看来,它是最具可扩展性的解决方案。这里有一些想法......

  • 忘掉TableLayout,改用ListView
  • 如果不需要在不同的应用程序 session 中持久化数据并且数据量不大,则将数据使用输入存储在内存中
  • 如果需要持久化且数据量不大,则将数据存储在文件或首选项中
  • 将其存储在 SQLite 数据库中...强烈推荐

现在,继续使用我在上面指出的术语搜索一些教程,但不要在此处索取教程,因为它违反了本网站的使用条款。祝你好运

关于Android TableLayout 通过单击按钮添加来自 EditText 的数据的新行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26374245/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com