【问题标题】:TableLayout with TextView and EditText using android StudioTableLayout 与 TextView 和 EditText 使用 android Studio
【发布时间】:2016-06-12 02:43:01
【问题描述】:
JSONArray json=new JSONArray(data);
for(int i=0;i<json.length(); i++)
{
    k=json.length();
    rowId =  i;
    JSONObject obj=json.getJSONObject(i);

    pname=obj.getString("ProductName");//database values
    quantity= obj.getString("Quantity");
    id = Integer.toString(k);
    TableRow tr = new TableRow(this);
    tr.setId(i);
    tr.setLayoutParams(new TableRow.LayoutParams(
            TableRow.LayoutParams.WRAP_CONTENT,
            TableRow.LayoutParams.WRAP_CONTENT));


    TextView labelID = new TextView(this);//header is SI:NO
    labelID.setText(id);
    labelID.setPadding(2, 5, 5, 0);
    labelID.setLeft(50);
    labelID.setTextColor(Color.BLACK);
    tr.addView(labelID);

    TextView labelNAME = new TextView(this);//header is PRODUCT NAME
    labelNAME.setText(pname);
    labelNAME.setRight(10);
    labelNAME.setPadding(2, 8, 5, 0);
    labelNAME.setTextColor(Color.BLACK);
    tr.addView(labelNAME);

    labelWEIGHT = new TextView(this);//header is QUANTITY
    //  labelWEIGHT.setId(200 + count);
    labelWEIGHT.setPadding(2, 6, 5, 0);
    labelWEIGHT.setText(quantity.toString());
    labelWEIGHT.setTextColor(Color.BLACK);
    tr.addView(labelWEIGHT);

    labelPRICE = new EditText(this);//header is PRICE...here i need to enter values and save in db
    labelPRICE.setTextColor(Color.BLACK);
    labelPRICE.setPadding(2, 6, 5, 0);
    labelPRICE.setId(Integer.valueOf(rowId));
    ar1[i]=rowId;//row id
    tr.addView(labelPRICE);

    tl.addView(tr, new TableLayout.LayoutParams(
            TableRow.LayoutParams.WRAP_CONTENT,
            TableRow.LayoutParams.WRAP_CONTENT));
    count++;}

基本上,我是 android 的初学者,因此在使用 textview 和 edittext 进行 tablelayout 程序时我需要帮助。我无法将用户输入的 edittext 值存储在数据库中。我试图获取行每行的 id,但无法得到。谁能帮助我。

这是我的布局。

【问题讨论】:

  • 贴出你试过的代码。
  • 这是代码...我需要在 EditText(in Price) 中输入值并使用 JSON 保存在数据库中

标签: android android-layout android-studio android-tablelayout android-database


【解决方案1】:

您可以先读取数据,然后将其转储到数据库中。可以通过连续知道编辑文本的位置来轻松完成

例如:

TableRow row = (TableRow)tableLayout.getChildAt(rowPosition);
EditText edit = (EditText) row.getChildAt(0);
String text = edit.getText().toString();

在上面的代码中,我们认为edittext在其行中的第0个位置并且读取了值。

【讨论】:

    【解决方案2】:

    首先,您可以使用以下方法在 EditText 中获取用户输入的值。

    myEditText.getText().toString() ;
    

    其次,为了将值存储在数据库中,您应该创建一个扩展 SQLiteOpenHelper 的自定义类。

    public class MyDatabaseHandler extends SQLiteOpenHelper
    

    重写该类的onCreate和onUpgrade方法后,创建如下方法处理插入方法:

     public void addNewData(String value)
    {
        try
        {
            ContentValues values = new ContentValues();
    
            values.put([name of the column in your database for this value],value);
    
            getWritableDatabase().insertOrThrow([name of your table], null, values);
        }
    
        catch (SQLiteException sqlExc)
        {
            sqlExc.printStackTrace();
        }
    
        catch (Exception exc)
        {
            exc.printStackTrace();
        }
    }
    

    如需在 Android 中使用 SQlite 数据库的完整教程you can study this article.

    【讨论】:

    • 我没有使用 SQLite 数据库...请您提出其他建议。
    • 在 android 中还有很多其他的数据存储选项。尝试更具体地说明您的问题并编辑您的问题
    • 是的,我已经添加了我的源代码和我的输出截图...我需要输入价格值并使用 JSON 保存在数据库中。现在你能帮我吗。
    猜你喜欢
    • 2020-08-12
    • 2015-05-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-01
    • 1970-01-01
    • 2017-05-04
    • 1970-01-01
    • 2011-02-10
    相关资源
    最近更新 更多