Android - Dynamic Table Rows NullPointerException - android

I am making an android application, which creates a dynamic interface, according to a string read from your preferences:
ScrollView sv = new ScrollView(this);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
TableLayout tl = new TableLayout(this);
TableRow[] tr = null;
SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
String Favs = SP.getString("Favs", "None");
String[] Genveje;
if (!Favs.equals("None"))
{
Genveje = Favs.split(";");
tr = new TableRow[Genveje.length];
for (int i = 0; i < Genveje.length; i++)
{
String BtnName = Genveje[i].split("#")[0];
String BtnPath = Genveje[i].split("#")[1];
Button btn = new Button(this);
btn.setText(BtnName);
btn.setTag(BtnPath);
btn.setOnClickListener(this);
btn.setOnLongClickListener(this);
tr[i].addView(btn);
.........
}
The problem is: when i try to do ANYTHING with tr[i] i get a NullPointerException, and i have no clue why.
Anyone with a suggestion?

tr[i]=new TableRow(this);
tr[i].addView(btn);

Related

Android Studio: getting an input from user

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

How to get ID of multiple EditText dynamically added?

I've added multiple EditText and TextViews dynamically like:
final String[] meses = new String[]{"Janeiro", "Fevereiro", "Março", "Abril", "Maio", "Junho", "Julho", "Agosto", "Setembro", "Outubro", "Novembro", "Dezembro"};
for(int i=0; i<meses.length; i++){
LinearLayout layout = new LinearLayout(getContext());
layout.setOrientation(LinearLayout.HORIZONTAL);
layout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
TextView textview = new TextView(getContext());
ViewGroup.LayoutParams lparams = new TableLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1f);
textview.setLayoutParams(lparams);
textview.setText(meses[i]);
layout.addView(textview);
EditText ed = new EditText(getContext());
ed.setLayoutParams(lparams);
ed.setId(i);
layout.addView(ed);
llMes.addView(layout);
}
But now I need to retrieve the values of each EditText, how can I retrieve it, I have this:
for(int i=0; i < meses.length; i++){
EditText et = null;
et.getId();
pago = quotas - Integer.parseInt(et.getText().toString());
}
And also tried with findViewById(i) with for but also failed.
Thanks for helping me!
Add EditTexts to an ArrayList then retrieve it by index.
//Arraylist that will contain EditTexts
ArrayList<EditText> list = new ArrayList<>();
// for each EditText add it to the ArrayList
for(int i=0; i < 10; i++){
EditText et = new EditText();
list.add(et);
}
//To retrieve EditText
EditText et = list.get(0);

How to get the dynamically created form values in android?

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..

How to display database table records using TableLayout

I followed the "How to get data from database in my android table view as columns" link to resolve my problem
but a blank screen appears without data. On clicking the button I have written code to fetch the records from MS SQL DB.
My XML, for layout purpose, activity_display_result.xml:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/tab" >
</TableLayout>
And the code snippet from MainActivity.java:
#SuppressLint("NewApi") #Override
public void onCreate(Bundle b)
{
super.onCreate(b);
setContentView(R.layout.activity_main);
if( Build.VERSION.SDK_INT >= 9)
{
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
initilize();
button1.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
List <Map<String,String>> dataList = querySQL(e1.getText().toString(),e2.getText().toString(),e.getText().toString());
StringBuilder builder = new StringBuilder();
for (int i=0;i<dataList.size();i++)
{
Log.i("onClick Method()", "UserName : "+dataList.get(i).get("UserName"));
Log.i("onClick Method()", "Password : "+dataList.get(i).get("Password"));
builder.append(dataList.get(i).get("UserName")).append(";").
append(dataList.get(i).get("Password")).append("_");
}
builder.toString();
String st = new String(builder);
String[] rows =st.split("_");
setContentView(R.layout.activity_display_result);
TableLayout tableLayout =(TableLayout) findViewById(R.id.tab);
tableLayout.removeAllViews();
for(int i=0;i<rows.length;i++)
{
String row = rows[i];
TableRow tableRow = new TableRow(getApplicationContext());
tableRow.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
final String[] cols = row.split(";");
Log.i("MainActivity ","Column length :"+cols.length);
//Handler handler = null;
for (int j = 0; j < cols.length; j++)
{
final String col = cols[j];
TextView TextView11 = new TextView(getApplicationContext());
TextView11.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
TextView11.setTextColor(color.black);
//TextView11.setText(String.format("%7s", col));
TextView11.setText(col);
tableRow.addView(TextView11);
}
tableLayout.addView(tableRow);
setContentView(R.layout.activity_display_result);
}
}
});
}
Declaring all
public void declere()
{
e = (EditText)findViewById(R.id.editText3);
e1 = (EditText)findViewById(R.id.editText1);
e2 = (EditText)findViewById(R.id.editText2);
button1=(Button)findViewById(R.id.button1);
}
//initializing
public void initilize()
{
Log.i("initilize() Method", "initilize() Method is called..Now Calling declere() method");
declere();
Log.i("initilize() Method", "declere() method is called ");
Log.i("initilize() Method", "Before Calling CONN() method..");
connect=CONN("sa","C!#C2#!2","RequestCenter");
Log.i("initilize() Method", "CONN() method called Successfully..");
}
I also added one CONN method for getting connection. Can anyone help me to determine why the screen appears blank ?
You are calling setContentView(R.layout.activity_display_result) at the end of your onClick method, which will reset the view to the content of the xml, hence giving you a blank screen as defined in activity_display_result.xml. You need to remove that line and see if it works.
A couple of things:
You continue to reset the setContentView(R.layout.activity_display_result); many times (via the loop). Is there a a reason? You probably only have to set it once at the end once all your controls have been placed in the layout. I've moved it to where I think it should be in the code below. If that doesn't work, reconsider taking it out as mentioned above by 2Dee
A question for my own understanding: Why is col set to Final? Would that not prevent the variable from being assigned the next value in the cols array? Thanks!
for(int i=0;i<rows.length;i++){
String row = rows[i];
TableRow tableRow = new TableRow(getApplicationContext());
tableRow.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
final String[] cols = row.split(";");
Log.i("MainActivity ","Column length :"+cols.length);
//Handler handler = null;
for (int j = 0; j < cols.length; j++)
{
final String col = cols[j];
TextView TextView11 = new TextView(getApplicationContext());
TextView11.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
TextView11.setTextColor(color.black);
//TextView11.setText(String.format("%7s", col));
TextView11.setText(col);
tableRow.addView(TextView11);
}
tableLayout.addView(tableRow);
}
setContentView(R.layout.activity_display_result);

how to generate dynamic textview in android?

something like this i want
for(i=0; i<10; i++) {
Textview tx[i] = new TextView(this);
tx[i].setText("Data")
TableRow tr[i] = new TableRow(this);
tr[i].addView(tx);
// and then adding table row in tablelayout
}
can somebody give me small example
TextView[] tx = new TextView[10];
TableRow[] tr = new TableRow[10];
for(i=0; i<10; i++) {
tx[i] = new TextView(this);
tx[i].setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
tx[i].setText("Data")
tr[i] = new TableRow(this);
tr[i].setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
tr[i].addView(tx[i]);
// and then adding table row in tablelayout
}
TextView[] tx = new TextView[10];
TableRow[] tr = new TableRow[10];
for(i=0; i<10; i++) {
tx[i] = new TextView(this);
tx[i].setText("Data")
tr[i] = new TableRow(this);
tr[i].addView(tx[i]);
// and then adding table row in tablelayout
}

Categories

Resources