Icreate this post, because i am new at this, and i need a little help. I am doing a little exercise about a application you that with some values you can make a tablelayout from class. But after i push the button i get the error "The specified child already has a parent. You must call removeView() on the child's parent first"
public class TabelFlat extends Activity{
//deklarasi variabel
String jumlah,jasa,jangkawaktu;
int jum = 0, jsa = 0, jkwaktu = 0;
int a = 0, b = 0, c = 0, d = 0, i = 0;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle extras = getIntent().getExtras();
//mengambil value yg diimport dari class lain
if (extras != null) {
jumlah = extras.getString("jumlah");
jasa = extras.getString("jasa");
jangkawaktu = extras.getString("jangkawaktu");
}
//konversi string menjadi int
jum = Integer.parseInt(jumlah);
jsa = Integer.parseInt(jasa);
jkwaktu = Integer.parseInt(jangkawaktu);
//membuat tabel
TableLayout MainLayout = new TableLayout(this);
MainLayout.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.FILL_PARENT));
MainLayout.setStretchAllColumns(true);
//Baris Judul
TableRow row1 = new TableRow(this);
TextView text1 = new TextView(this);
text1.setText("Bulan");
TextView text2 = new TextView(this);
text2.setText("Total Angsuran");
TextView text3 = new TextView(this);
text2.setText("Angsuran");
TextView text4 = new TextView(this);
text2.setText("Bunga");
TextView text5 = new TextView(this);
text2.setText("Sisa Saldo");
text2.setGravity(android.view.Gravity.RIGHT);
//memasukkan nilai text ke row
row1.addView(text1);
row1.addView(text2);
row1.addView(text3);
row1.addView(text4);
row1.addView(text5);
//menambah row ke tabel
MainLayout.addView(row1);
//kalkulasi
TextView tangsuran = new TextView(this);
a = (jum * 120/100);
tangsuran.setText(""+String.valueOf(a));
TextView angsuran = new TextView(this);
b = jum / jkwaktu;
angsuran.setText(""+String.valueOf(b));
TextView bunga = new TextView(this);
c = jum * 20 / 100 / jkwaktu;
bunga.setText(""+String.valueOf(c));
//loop
d = jum;
for (i = 1;i<=jkwaktu;i++){
TableRow barisloop = new TableRow(this);
//membuat string kosong untuk setiap kolom
TextView bulan = new TextView(this);
bulan.setText(""+i);
TextView sisa = new TextView(this);
d = d - b;
sisa.setText(""+String.valueOf(d));
barisloop.addView(bulan);
barisloop.addView(tangsuran);
barisloop.addView(angsuran);
barisloop.addView(bunga);
barisloop.addView(sisa);
MainLayout.addView(barisloop);
}
setContentView(MainLayout);
}
}
here you add the tangsuran, angsuran and bunga text view to new row, but they are already added to another one.You must create new objects for the rows in the loop.
for (i = 1;i<=jkwaktu;i++){
TableRow barisloop = new TableRow(this);
//membuat string kosong untuk setiap kolom
TextView bulan = new TextView(this);
bulan.setText(""+i);
TextView sisa = new TextView(this);
d = d - b;
sisa.setText(""+String.valueOf(d));
barisloop.addView(bulan);
//barisloop.addView(tangsuran);
//barisloop.addView(angsuran); CREATE NEW TEXTVIEWS
//barisloop.addView(bunga);
barisloop.addView(sisa);
MainLayout.addView(barisloop);
}
Related
I'm new to Android Studio. I would like to read input from user from a dynamically created elements. I've already managed to create the elements. Now I do not know at which place should I place the reading element. Can someone give me a MWE here ? I do not know also if I should use getText(this) or editText(this) or something else.
public void click(View view) {
String col1;
String col2;
TableLayout tl = (TableLayout)findViewById(R.id.tableLayout1);
TableRow row = new TableRow(this);
TextView tv = new TextView(this);
TextView c = new TextView(this);
EditText etUserInfoNewValue = (EditText)findViewById(R.id.simpleEditText);
tv.setText("This is text");
tl.addView(row);
row.addView(tv);
for (int x = 0; x < 5; x++) {
col1 = "(" + x + ")"+Integer.toBinaryString(x);
col2 = "1";
TableRow newRow = new TableRow(this);
TextView column1 = new TextView(this);
TextView column2 = new TextView(this);
EditText editText1 = new EditText(this);
TextView column3 = new TextView(this);
column1.setText(col1);
column1.setText(col1);
column2.setText(col2);
newRow.addView(column1);
newRow.addView(editText1);
newRow.addView(column3);
tl.addView(newRow, new TableLayout.LayoutParams());
}
}
i create two edittext programmatically and i need to get text from him
this is my code for create edittext
layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(10, 10, 10, 10);
//First EditText
edText = new EditText(getActivity());
edText.setId(0);
edText.setLayoutParams(new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT,
1f));
edText.setBackgroundResource(R.drawable.edittext);
edText.setPadding(5, 5, 5, 5);
linear=(LinearLayout)rootView.findViewById(R.id.edittextpanel);
linear.addView(edText ,layoutParams);
nb++;
//Second EditText
edText = new EditText(getActivity());
edText.setId(1);
edText.setLayoutParams(new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT,
1f));
edText.setPadding(5, 5, 5, 5);
edText.setBackgroundResource(R.drawable.edittext);
linear.addView(edText ,layoutParams);
Help please i'm blocked
edText = new EditText(getActivity());
edText.setId(id);
String stringAnswer = edText.getText().toString();
If you want to create and reference more than one edit text you'll have to create an EditText array
List<EditText> allEDs = new ArrayList<EditText>();
Then create the Edit Texts
edText = new EditText(getActivity());
edText.setId(id);
Push them into the array
allEDs.add(edText);
Create a string array same size as the EditText array
String[] ETResults = new String[allEDs.size()];
Loop through the EditText array and put converted results into String array
for(int i=0; i < allEDs.size(); i++){
ETResults[i] = allEDs.get(i).getText().toString();
}
And then assign the desired string to whatever you want
String stringAnswer = ETResults[i];
try {
final ViewGroup viewGroup = (ViewGroup) ((ViewGroup) context.findViewById(android.R.id.content)).getChildAt(0);
for(int i = 0; i <viewGroup.getChildCount(); i++){
if(viewGroup.getChildAt(i) instanceof EditText) {
EditText view = (EditText)viewGroup.getChildAt(i);
Log.i("val " , " val " + ((EditText) viewGroup.getChildAt(i)).getText() + " view "+ view.getText() );
}
}
}catch (Exception ex){
Log.e("Error in Bind layout", ex.toString());
}
Maintain a reference to both EditTexts. Put the following in the main body of your Fragment to make them globally accessible..
EditText edText1;
EditText edText2;
Then in the method you've shown you can use...
// Do some stuff
edText1 = new EditText(getActivity());
// Do some more stuff
edText2 = new EditText(getActivity());
...
Later when you need to get the text you can use...
String text1 = edText1.getText().toString();
String text2 = edText2.getText().toString();
Alternatively you can find them later in your Fragment using their ids...
LinearLayout linear = (LinearLayout) findViewById(R.id.edittextpanel);
EditText et = (EditText) linear.findViewById(0);
String text1 = et.getText().toString();
et = (EditText) linear.findViewById(1);
String text2 = et.getText().toString();
I have developed a form in dynamically using some edit text,check boxes,radio buttons and buttons.I have created form successfully ,but how i can get the values from that and stored in database.Please can any one help me.
Thanking in Advance.
public void textView() {
idText++;
i++;
LinearLayout linearLayoutHorizantal1 = new LinearLayout(
getApplicationContext());
linearLayoutHorizantal1.setOrientation(LinearLayout.HORIZONTAL);
/*
* LinearLayout.LayoutParams params = new
* LinearLayout.LayoutParams(LayoutParams
* .WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
* linearLayoutHorizantal1.setGravity(Gravity.RIGHT);
*/
txtQuetion = new TextView(getApplicationContext());
txtQuetion.setText(i + ". " + strQuestionText);
txtQuetion.setTextColor(Color.BLACK);
txtQuetion.setTextSize(20);
txtQuetion.setId(idText);
linearLayoutHorizantal1.addView(txtQuetion);
linearLayoutDynamicAdd.addView(linearLayoutHorizantal1);
}
public void RadioButtons() {
radiogroup = new RadioGroup(getApplicationContext());
radiogroup.setOrientation(RadioGroup.HORIZONTAL);
RadioGroup.LayoutParams params = new RadioGroup.LayoutParams(
SurveyFillActivity.this, null);
params.setMargins(10, 10, 10, 0);
linearLayoutDynamicAdd.addView(radiogroup);
for (int j = 0; j < answersList.length; j++) {
if (answersList[j] != null) {
idRadio++;
rb = new RadioButton(getApplicationContext());
rb.setId(idRadio);
rb.setText(answersList[j]);
rb.setTextColor(Color.BLACK);
rb.setButtonDrawable(R.drawable.radio_custom);
// rb.setChecked(true);
rb.setLayoutParams(params);
radiogroup.addView(rb);
}
}
}
private EditText editText(int _intID) {
idEditBox++;
final LayoutParams lparams = new LayoutParams(350, 50);
final EditText et = new EditText(this);
lparams.leftMargin = 20;
lparams.topMargin = 10;
et.setLayoutParams(lparams);
et.setWidth(32);
et.setEms(50);
et.setBackgroundResource(R.drawable.customborder_backbutton);
/*
* EditText et = new EditText(getApplicationContext());
* et.setId(idEditBox); et.setHeight(60); et.setWidth(50);
*/
//et.setBackgroundColor(Color.WHITE);
String s1 = et.getText().toString();
linearLayoutDynamicAdd.addView(et, lparams);
return et;
}
The above code for creating forms and i need to get the values form it and store in Database.
Create the arrays for texviews and radiobuttons ...what ever you need in your form.
take this as reference
TextView tv[] = new TextView[HowManyYouNeed];
TextView tvsa = new TextView(YourContax);
tv[position]=tvsa;//position like o ..1..2
tv[0].getText();
May be help full..
I have been working on saving 4 String arrays to sharedpreference. Currently I am breaking the array down in to a string separating the pieces by a comma. When I go to the string, it does save the pieces accurately as it should; however, when I "load" the data and convert it back to an array, it's coming up empty (not null). I am unable to determine how I should fix this to pull my array back. Below is my Activity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.debtlist);
String[] debtName = new String[10];
String[] debtAmount = new String[10];
String[] debtRate = new String[10];
String[] debtPayment = new String[10];
int counter = 0;
SharedPreferences sharedPref= getSharedPreferences("chaosdatasnowball", 0);
String tempDebtNames = sharedPref.getString("debtNames", "");
debtName = convertStringToArray(tempDebtNames);
Bundle extras = getIntent().getExtras();
int trigger = 0;
for (int i=0;i<counter || i==counter;i++)
{
if (debtName[i] == null && extras != null && trigger == 0)
{
debtName[i] = extras.getString("debtName");
debtAmount[i] = extras.getString("debtAmount");
debtRate[i] = extras.getString("debtRate");
debtPayment[i] = extras.getString("debtPayment");
trigger = 1;
counter++ ;
}
}
TableLayout tl = (TableLayout) findViewById(R.id.debtListTableView);
for (int i=0;i<counter || i==counter;i++)
{
if (debtName[i] != null)
{
TableRow tr = new TableRow(this);
TextView tv0 = new TextView(this);
TextView tv1 = new TextView(this);
TextView tv2 = new TextView(this);
TextView tv3 = new TextView(this);
TableRow.LayoutParams trlp = new TableRow.LayoutParams();
tv0.setLayoutParams(new LayoutParams(0, LayoutParams.MATCH_PARENT, 0.25f));
tv1.setLayoutParams(new LayoutParams(0, LayoutParams.MATCH_PARENT, 0.25f));
tv2.setLayoutParams(new LayoutParams(0, LayoutParams.MATCH_PARENT, 0.25f));
tv3.setLayoutParams(new LayoutParams(0, LayoutParams.MATCH_PARENT, 0.25f));
trlp.span = 3;
tr.setLayoutParams(trlp);
tv0.setText("" + debtName[i]);
tv1.setText("" + debtAmount[i]);
tv2.setText("" + debtPayment[i]);
tv3.setText("" + i);
tr.addView(tv0);
tr.addView(tv1);
tr.addView(tv2);
tr.addView(tv3);
tl.addView(tr);
}
}
SharedPreferences.Editor editor= sharedPref.edit();
String debtNames = convertArrayToString(debtName, counter);
editor.putString("debtNames", debtNames).commit();
TextView disp = (TextView) findViewById(R.id.dispAdditionalAmount);
disp.setText("" + debtNames); //This is how I confirm that the convertArrayToString is functioning properly
}
public static String convertArrayToString(String[] array, int stop){
String str = "";
for (int i = 0;i<stop; i++) {
str = str+array[i];
// Do not append comma at the end of last element
if(i<stop-1){
str = str+",";
}
}
return str;
}
public static String[] convertStringToArray(String str){
String[] arr = str.split(",");
return arr;
}
You do:
String tempDebtNames = sharedPref.getString("debtNames", "");
debtName = convertStringToArray(tempDebtNames);
It is possible that "debtNames" key does not exist. (It returns "" in this case).
Think about first time the app is run. The key will not exist. Therefore, you should convertStringToArray only if !tempDebtNames.equals(""). If key doesn't exist then proceed with empty values..
I am iterating through the results of a query creating RelativeLayouts in TableRows. If I try to set the LayoutParams for the RelativeLayout then nothing appears and get no errors. If I don't then everything appears but with the layout being set to WRAP_CONTENT. I have tried several example and can not get it to work. Below is my code:
TableLayout tblItems = (TableLayout) findViewById(R.id.tblItems);
// Fields from the database (projection)
String[] projection = new String[] { ItemsTable.ITEM_ID,
ItemsTable.ITEM_NAME,
ItemsTable.ITEM_PRICE,
ItemsTable.ITEM_PICTURE };
Cursor cursor = getContentResolver().query(ItemsTable.CONTENT_URI, projection, null, null, null);
startManagingCursor(cursor);
// Establish the item mapping
// String[] columns = new String[] {"name", "price"};
// int[] to = new int[] {R.id.tv_description, R.id.tv_price};
// adapter = new SimpleCursorAdapter(this, R.layout.item_list_select, cursor, columns, to, 0);
// lvSelectItems.setAdapter(adapter);
/*
adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
if(columnIndex == cursor.getColumnIndexOrThrow(OrderDetailTable.ORDERDETAIL_PRICE)) {
// String createDate = aCursor.getString(aColumnIndex);
// TextView textView = (TextView) aView;
// textView.setText("Create date: " + MyFormatterHelper.formatDate(getApplicationContext(), createDate));
double total = cursor.getDouble(columnIndex);
TextView textView = (TextView) view;
textView.setText("$" + String.format("%.2f",dRound(total, 2)));
return true;
}
return false;
}
});
*/
// tblItems
int totalRows = 0;
int tcolumn = 1;
int numItems = cursor.getCount();
double tempRow = numItems / 2;
if (numItems > 0) {
itemsFound = true;
} else {
itemsFound = false;
}
long iPart; // Integer part
double fPart; // Fractional part
boolean ifFirst = true;
// Get user input
iPart = (long) tempRow;
fPart = tempRow - iPart;
if (fPart > 0) {
totalRows = (int) (iPart + 1);
} else {
totalRows = (int) iPart;
}
TableRow row = null;
cursor.moveToFirst();
while (cursor.isAfterLast() == false)
{
// tblItems // TableLayout
// TableRow
// RelativeLayout
// ImageView
// TextView
// TextView
if (ifFirst) {
// create a new TableRow
row = new TableRow(this);
ifFirst = false;
}
// Creating a new RelativeLayout
RelativeLayout relativeLayout = new RelativeLayout(this);
// Defining the RelativeLayout layout parameters.
// In this case I want to fill its parent
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
rlp.setMargins(5, 5, 5, 5);
relativeLayout.setPadding(5, 5, 5, 5);
relativeLayout.setBackgroundResource(R.drawable.grpbx_item);
row.addView(relativeLayout, rlp);
// add text view
ImageView imgPic = new ImageView(this);
imgPic.setBackgroundResource(R.drawable.takepic);
imgPic.setPadding(0, 0, 0, 0);
relativeLayout.addView(imgPic);
// add text view
TextView tvName = new TextView(this);
String name = cursor.getString(cursor.getColumnIndexOrThrow(ItemsTable.ITEM_NAME));
tvName.setText("" + name);
// Defining the layout parameters of the TextView
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.CENTER_IN_PARENT);
// RelativeLayout.
// Setting the parameters on the TextView
tvName.setLayoutParams(lp);
relativeLayout.addView(tvName);
// add text view
double iPrice = cursor.getDouble(cursor.getColumnIndexOrThrow(ItemsTable.ITEM_PRICE));
TextView tvPrice = new TextView(this);
tvPrice.setText("$" + String.format("%.2f",dRound(iPrice, 2)));
relativeLayout.addView(tvPrice);
tcolumn++;
numItems--;
if (tcolumn == 3) {
// add the TableRow to the TableLayout
tblItems.addView(row,new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
// create a new TableRow
row = new TableRow(this);
tcolumn = 1;
} else {
if (numItems == 0) {
// add the TableRow to the TableLayout
tblItems.addView(row,new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
}
}
// Move to the next record
cursor.moveToNext();
}
}
If I try to set the layout params for the relativeLayout then nothing
appears and get no errors. If I don't then everything appears but with
the layout being set to wrapcontent.
You use the wrong LayoutParams for the RelativeLayout. You use:
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
row.addView(relativeLayout, rlp);
but as the parent of that RelativeLayout is a TableRow you must use TableRow.LayoutParams:
TableRow.LayoutParams rlp = new TableRow.LayoutParams(
TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT);
row.addView(relativeLayout, rlp);