2
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++;}

Basically,I'm a beginner in android, and thus I need a help in doing tablelayout program with textview and edittext.I'm not able to store the user entered edittext value in the database .I tried to get the row id of each row but couldn't get that.Can anyone please help me.

This is my layout.

enter image description here

SMR
  • 6,628
  • 2
  • 35
  • 56
Dinsha
  • 41
  • 1
  • 4

2 Answers2

0

You can do it by first reading the data and then dumping it in DB. It can easily be done by knowing the position of the edit text with in a row

Eg:

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

In the above code we consider that edittext is at 0th position in its row and value is read.

frogEye
  • 364
  • 3
  • 14
0

First, you can get the user-entered value in an EditText using the following method.

myEditText.getText().toString() ;

Secondly, for storing the value in a database, you should create a custom class which extends the SQLiteOpenHelper.

public class MyDatabaseHandler extends SQLiteOpenHelper

After overriding the onCreate and onUpgrade methods of this class, create the following method for handling the insert method :

 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();
    }
}

For a complete tutorial on Using SQlite Database in Android , you can study this article.

Farhad
  • 12,178
  • 5
  • 32
  • 60
  • Im not using SQLite database...can you please suggest other than this. – Dinsha Feb 29 '16 at 06:46
  • There are plenty of other options for data storage in android. Try to be more specific about your issue and edit your question – Farhad Feb 29 '16 at 06:48
  • yes,i have added my source code and the screenshot of my output...i need to enter values in price and save in db using JSON.Now can u please help me out. – Dinsha Mar 01 '16 at 06:44