Dynamic rows don't appear - android

I have a class Store.java where I simply store 2 strings and an integer. Then I create an ArrayList object of the same. I do this so that I can populate the values from 2 spinners and an edit text into the ArrayList dynamically. I want to get these values displayed in a tabular format. Also I've created a subclass of Application so that the ArrayList can be easily updated and retrieved in other Activities. The problem is that when I click on the 'Bill' button the dynamic rows are not added to the table layout.
Here is the relevant code:
(MyApp.java)
public class MyApp extends Application {
ArrayList<store> B = null;
public ArrayList<store> getState(){
return B;
}
public void setState(ArrayList<store> B){
this.B = B;
}
}
(Bill.java)
public class Bill extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.calc);
ArrayList<store> B = new ArrayList<store>();
store temp1=null;
MyApp appState = ((MyApp)getApplicationContext());
appState.setState(B);
B = appState.getState();
TableLayout tl = (TableLayout)findViewById(R.id.myTable);
for(int i=0;i<B.size();i++)
{
temp1=new store();
TableRow tr = new TableRow(this);
tr.setId(100+i);
temp1=B.get(i);
tr.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
TextView b = new TextView(this);
b.setId(200+i);
b.setText(temp1.getPizzaName());
TextView b1 = new TextView(this);
b1.setText(temp1.getPizzaSize());
b1.setId(300+i);
TextView b2 = new TextView(this);
b2.setText(String.valueOf(temp1.getQuantity()));
b2.setId(400+i);
b.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
tr.addView(b);
b1.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
tr.addView(b1);
b2.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
tr.addView(b2);
tl.addView(tr,new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
}
}
}

Take a look to your code:
ArrayList<store> B = new ArrayList<store>();
store temp1=null;
MyApp appState = ((MyApp)getApplicationContext());
appState.setState(B);
B = appState.getState();
You create a List, than set it as your state, then retrieve it again.
How do you want to has some data on it?
PS: Please follow the code conventions, you code is hard to read.
Your variable 'temp' is initialized once. You should make a new instance every time before add it to the List.

Related

ArrayList data passed to a second Activity is not showing in a TableLayout there

I'm trying to send a sample ArrayList to a second Activity so that it can be displayed in a TableLayout.
My MainActivity class:
public class MainActivity extends AppCompatActivity {
public ArrayList<String> al = new ArrayList<>();
Button b;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b = (Button) findViewById(R.id.b);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
al.add("Add");
al.add("Sub");
Intent i = new Intent(view.getContext(),two.class);
i.putStringArrayListExtra("al", al);
startActivity(i);
}
});
}
}
The activity_main layout contains a Button that opens the next Activity, which contains a TableLayout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:stretchColumns="0,1"
android:id="#+id/main_table" android:layout_weight="1"
android:layout_height="wrap_content" android:layout_width="match_parent">
</TableLayout>
</LinearLayout>
In the second Activity, the ArrayList is being traversed to display the data in the TableLayout.
I guess the ArrayList is not being transfered properly to the next Activity.
public class two extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_two);
Intent i = getIntent();
ArrayList<String> al = i.getStringArrayListExtra("al");
TableLayout tl = (TableLayout) findViewById(R.id.main_table);
TableRow tr_head = new TableRow(this);
//tr_head.setId(11);
// tr_head.setBackgroundColor(Color.GRAY);
int co=0;
for(String k:al)
{
co++;
TableRow tr = new TableRow(this);
tr.setId(co);
tr.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
TextView tv = new TextView(this);
tv.setText(k);
tv.setPadding(2, 0, 5, 0);
tr_head.addView(tv);
tl.addView(tr, new TableLayout.LayoutParams(
TableLayout.LayoutParams.FILL_PARENT,
TableLayout.LayoutParams.WRAP_CONTENT));
}
}
}
ArrayList is being sent properly there is nothing wrong with that.
Also LayoutParams for TableRow isn't needed, add LayoutParams on TextView instead.
tr_head is not needed also if you have already defined Layout for TableLayout there is no need to give LayoutParams here:
tl.addView(tr, new TableLayout.LayoutParams(
TableLayout.LayoutParams.FILL_PARENT,
TableLayout.LayoutParams.WRAP_CONTENT));
tl.addView(tr) will do just fine.
Try using the below code, this works fine by me!
public class two extends AppCompatActivity { #Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_two);
Intent i = getIntent();
ArrayList<String> al = i.getStringArrayListExtra("al");
TableLayout tl = (TableLayout) findViewById(R.id.main_table);
int co=0;
for(String k:al)
{
co++;
TableRow tr = new TableRow(this);
tr.setId(co);
TextView tv = new TextView(this);
tv.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT,1.0f));
tv.setText(k);
tv.setPadding(2, 0, 5, 0);
tr.addView(tv);
tl.addView(tr);
}}}

Add clickable textview to table layout dynamically

Add clickable textview to table layout dynamically
TextView Delete = new TextView(this);
Delete.setText("Delete");
Delete.setClickable(true);
Delete.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
TransDelete(ac);
}
});
This is my row added dynamically.
public void TransDelete(final String TransID) {
class TransDeleteClass extends AsyncTask<String, Void, String> {
#Override
protected void onPreExecute() {
super.onPreExecute();
}
#Override
protected void onPostExecute(String httpResponseMsg) {
super.onPostExecute(httpResponseMsg);
Toast.makeText(ViewTransactions.this, httpResponseMsg.toString(), Toast.LENGTH_LONG).show();
finish();
}
#Override
protected String doInBackground(String... params) {
// Sending Trans id.
hashMap.put("TransID", params[0]);
finalResult = httpParse.postRequest(hashMap, HttpUrlDeleteRecord);
return finalResult;
}
}
TransDeleteClass TransDeleteClass = new TransDeleteClass();
TransDeleteClass.execute(TransID);
}
This is my onclick function. I tried to click my textview while launching the mulator but the textview does not respond to onclick . I have seen other forums but as I need to pass in my TransID which is not the id i show on screen. For example , I tried filtering the data and although the screen shows numbers added in numeric order, it is not the case for the database i am using.
Edit:
<ScrollView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fillViewport="true"
android:scrollbars="none"
android:layout_below="#+id/textView1">
<TableLayout android:layout_width="wrap_content"
android:layout_height="0dp"
android:stretchColumns="1,1,1"
android:id="#+id/maintable" >
</TableLayout>
</ScrollView>
This is my xml page but the table pops up perfectly.
I have chosen to add my headers of the table layout dynamically too.
void addHeader(){
/** Create a TableRow dynamically **/
tr = new TableRow(this);
/** Creating a TextView to add to the row **/
label = new TextView(this);
label.setText("ID");
label.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
label.setPadding(5, 5, 5, 5);
label.setBackgroundColor(Color.RED);
LinearLayout Ll = new LinearLayout(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT);
params.setMargins(5, 5, 5, 5);
Ll.setPadding(10, 5, 5, 5);
Ll.addView(label,params);
tr.addView((View)Ll); // Adding textView to tablerow.
Just a part of my header of the table as adding more codes would require more description.
final TextView[] myTextViews = new TextView[N]; // create an empty array;
for (int i = 0; i < N; i++) {
// create a new textview
final TextView rowTextView = new TextView(this);
// set some properties of rowTextView or something
rowTextView.setText("This is row #" + i);
// add the textview to the tablelayout
yourtable.addView(rowTextView);
// save a reference to the textview for later
myTextViews[i] = rowTextView;
}

how can we use switch ,Edit Text and button in a single row of a table layout using java code?

i want to know the code to implement this
how can we use switch ,Edit Text and button in a single row of a table layout
so that these three things come in a single row using table layout using java code
Use this.
public class MyPhoneGapActivity extends Activity {
LinearLayout mainLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mainLayout = (LinearLayout) findViewById(R.id.main);
TableLayout ll = new TableLayout(getApplicationContext());
ll.setOrientation(LinearLayout.HORIZONTAL);
ll.setGravity(Gravity.TOP);
ll.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
TableRow tr = new TableRow(this);
tr.setOrientation(LinearLayout.HORIZONTAL);
tr.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
Button b = new Button(this);
b.setText("Hello");
Switch s = new Switch(this);
s.setTextOff("Off");
s.setTextOn("On");
EditText edt = new EditText(this);
edt.setText("Hello Piyush");
tr.addView(b);
tr.addView(s);
tr.addView(edt);
ll.addView(tr);
mainLayout.addView(ll);
}
}

Android first TableRow is not showing

I have a weird problem. I derived MessageRow from TableRow. I populated a table with one TableRow (Header) and 100 MessageRows. But when I add the table to the HorizontalScroll view only the Messagerows are showing.
If I inspect the table in the debugger, all the rows are there, including the Header, with the right children and text.
This is the simplified code:
public class MessageRow extends TableRow {
public TextView tvData1;
public TextView tvData2;
public TextView tvData3;
public MessageRow(Context context) {
this(context, null);
}
public MessageRow(Context context, AttributeSet attrs) {
super(context, attrs);
tvData1 = new TextView(context);
tvData2 = new TextView(context);
tvData3 = new TextView(context);
}
public void setData(String data1, String data2, String data3) {
tvData1.setText(data1);
tvData2.setText(data2);
tvData3.setText(data3);
addView(tvData1);
addView(tvData2);
addView(tvData3);
}
}
The activity code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.viewer);
mTable = new TableLayout(this);
HorizontalScrollView hview = (HorizontalScrollView) findViewById(R.id.hscroll);
mTable.setBackgroundColor(Color.WHITE);
populate(mTable);
hview.addView(mTable);
}
public void setHeader(TableLayout tl) {
TableRow mHeader = new TableRow(getContext());
mHeader.addView(getColumnHeader("Data#1"));
mHeader.addView(getColumnHeader("Data#2"));
mHeader.addView(getColumnHeader("Data#3"));
mHeader.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
tl.addView(mHeader);
}
public void populate(TableLayout tl) {
setHeader(tl);
for(int i = 0; i < 100; i++) {
MessageRow mr = new MessageRow(getContext());
mr.setMessage("xxx"+i,"yyy"+i,"zzz"+i);
mr.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
tl.addView(mr);
}
}
private Button getColumnHeader(String name) {
Button bt = new Button(getContext());
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.rightMargin = params.leftMargin = 2;
bt.setLayoutParams(params);
bt.setText(name);
return bt;
}
Now, I tried bypassing the getColumnHeader() function like this:
public void setHeader(TableLayout tl) {
TableRow mHeader = new TableRow(getContext());
//mHeader.addView(getColumnHeader("Data#1"));
--> Button bt1 = new Button(getContext());
--> bt1.setText("Data#1");
--> mHeader.addView(bt1);
//mHeader.addView(getColumnHeader("Data#2"));
--> Button bt2 = getColumnHeader("Data#2");
--> mHeader.addView(bt2);
mHeader.addView(getColumnHeader("Data#3"));
mHeader.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
tl.addView(mHeader);
}
and then bt1 (Data#1) is displayed!!! but not Data#2 or Data#3. This hints that somehow the function getColumnHeader() is bad. But not acording to the debugger, when I inspect bt1 and bt2 they seem good, I cannot detect what is the difference between the manually instantiated button bt1 and the returned button bt2.
Side note: I don't know if this is the best way, but to compare the two variables I just right-clicked and Copy Variable, then pasted it to notepad++ in two different files (one for bt1 and one for bt2) and used the compare plugin.
Some more info. I managed to display the buttons, further debug showed that setting the Layout params for each button in the function getColumnHeader() caused the problem. Still I don't know why.
The button member mLayoutParams being null is OK, but when created (.setLAyoutParams()) and assigned values (for example width and height) the suddenly the button is not displayed.
This is the patch,... until I figure out why.
private Button getColumnHeader(String name) {
Button bt = new Button(getContext());
//LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
//params.rightMargin = params.leftMargin = 2;
//bt.setLayoutParams(params);
bt.setText(name);
return bt;
}
Last piece of the puzzle.
I tried many variations and finally it worked!
Replacing LayoutParams with TableRow.LayoutParams did the trick.
Their structure is very similar, probably even TableRow.LayoutParams is derived from LayoutParams.
I will leave the mystery here, until some expert sheds light on why this happened.
The final code:
private Button getColumnHeader(String name) {
Button bt = new Button(getContext());
TableRow.LayoutParams params = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.MATCH_PARENT);
params.rightMargin = params.leftMargin = 2;
bt.setLayoutParams(params);
bt.setText(name);
return bt;
}

Onclicklistener for a programmatically created button

I have looked for an answer for this but can't seem to find one.
I have a button that has been created programmatically (rather than in the xml file) and I want something to happen when its clicked, show an alert or move to another screen etc.
Button code:
Button submitButton = new Button(this);
submitButton.setId(99);
submitButton.setText("Submit");
Listener code:
View submitButtonListener = findViewById(99);
submitButtonListener.setOnClickListener(this);
So I have set up the listener to find that button but I get this error
The method setOnClickListener(View.OnClickListener) in the type View
is not applicable for the arguments
(RegisterScreen) RegisterScreen.java /AccessibleApp/src/org/project/accessible line
217 Java Problem
I think it may be something to do with the fact that I don't have setContentView(); at the start of the class because the whole page is written programmatically (because I need to add checkboxes programmatically).
Here is the all the code below if it helps:
public class RegisterScreen extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//initiate the database to be qureied
DatabaseData db = new DatabaseData(this);
db = new DatabaseData(this);
try {
db.createDataBase();
}
catch (IOException ioe) {
throw new Error("Unable to create database");
}
try {
db.openDataBase();
}
catch(SQLException sqle){
throw sqle;
}
SQLiteDatabase rdb = db.getReadableDatabase();
//main layout of the screen
LinearLayout layoutMain = new LinearLayout(this);
layoutMain.setOrientation(LinearLayout.VERTICAL);
layoutMain.setBackgroundColor(Color.WHITE);
//Linear Layout for the Banner
LinearLayout banner = new LinearLayout(this);
banner.setOrientation(LinearLayout.VERTICAL);
banner.setBackgroundColor(Color.rgb(17, 168, 191));
//layout params for height and width
LayoutParams bannerParams = new android.widget.LinearLayout.LayoutParams(
android.widget.LinearLayout.LayoutParams.FILL_PARENT, 40);
banner.setLayoutParams(bannerParams);
//Banner text
TextView bannerText = new TextView(this);
bannerText.setText("Register");
bannerText.setTextColor(Color.WHITE);
banner.addView(bannerText);
bannerText.setTextSize(24);
bannerText.setGravity(Gravity.CENTER);
//add banner layout to main layout
layoutMain.addView(banner);
//Scroll view for the rest of the screen
ScrollView sv = new ScrollView(this);
//Table layout to align the items register form items
TableLayout tl = new TableLayout(this);
//Table rows to put items on left and right sides of the page
TableRow usernameTR = new TableRow(this);
//Username label
TextView usernameL = new TextView(this);
usernameL.setText("Username:");
usernameL.setTextColor(Color.BLACK);
usernameTR.addView(usernameL);
//Username textbox
EditText usernameTB = new EditText(this);
usernameTB.setWidth(250);
usernameTR.addView(usernameTB);
tl.addView(usernameTR);
TableRow emailTR = new TableRow(this);
//email label
TextView emailL = new TextView(this);
emailL.setText("Email:");
emailL.setTextColor(Color.BLACK);
emailTR.addView(emailL);
//email textbox
EditText emailTB = new EditText(this);
emailTB.setWidth(250);
emailTR.addView(emailTB);
tl.addView(emailTR);
TableRow forenameTR = new TableRow(this);
//forename label
TextView forenameL = new TextView(this);
forenameL.setText("Forename:");
forenameL.setTextColor(Color.BLACK);
forenameTR.addView(forenameL);
//forename textbox
EditText forenameTB = new EditText(this);
forenameTB.setWidth(250);
forenameTR.addView(forenameTB);
tl.addView(forenameTR);
TableRow surnameTR = new TableRow(this);
//surname label
TextView surnameL = new TextView(this);
surnameL.setText("Surname:");
surnameL.setTextColor(Color.BLACK);
surnameTR.addView(surnameL);
//surname textbox
EditText surnameTB = new EditText(this);
surnameTB.setWidth(250);
surnameTR.addView(surnameTB);
tl.addView(surnameTR);
TableRow streetTR = new TableRow(this);
//street label
TextView streetL = new TextView(this);
streetL.setText("Street:");
streetL.setTextColor(Color.BLACK);
streetTR.addView(streetL);
//street textbox
EditText streetTB = new EditText(this);
streetTB.setWidth(250);
streetTR.addView(streetTB);
tl.addView(streetTR);
TableRow postcodeTR = new TableRow(this);
//postcode label
TextView postcodeL = new TextView(this);
postcodeL.setText("Postcode:");
postcodeL.setTextColor(Color.BLACK);
postcodeTR.addView(postcodeL);
//postcode textbox
EditText postcodeTB = new EditText(this);
postcodeTB.setWidth(250);
postcodeTR.addView(postcodeTB);
tl.addView(postcodeTR);
TableRow cityTR = new TableRow(this);
//city label
TextView cityL = new TextView(this);
cityL.setText("City:");
cityL.setTextColor(Color.BLACK);
cityTR.addView(cityL);
//city textbox
EditText cityTB = new EditText(this);
cityTB.setWidth(250);
cityTR.addView(cityTB);
tl.addView(cityTR);
TableRow countyTR = new TableRow(this);
//county label
TextView countyL = new TextView(this);
countyL.setText("County:");
countyL.setTextColor(Color.BLACK);
countyTR.addView(countyL);
//county textbox
EditText countyTB = new EditText(this);
countyTB.setWidth(250);
countyTR.addView(countyTB);
tl.addView(countyTR);
//Add table layout to the scroll view
//country dropdown
TextView catTitle = new TextView(this);
catTitle.setText("\nPlease select the categories which affect you:\n");
catTitle.setTextColor(Color.BLACK);
tl.addView(catTitle);
//categories
//categories title
String[] cols = {"_id", "cat_name"}; //columns to be searched
Cursor cursor = rdb.query("aa_category", cols, null, null, null, null, null); // save the query to the db
while (cursor.moveToNext()) {
CheckBox catCB = new CheckBox(this);
String name = cursor.getString(1);
int id = cursor.getInt(0);
catCB.setId(id);
catCB.setText("\n"+name+"\n");
catCB.setTextColor(Color.BLACK);
tl.addView(catCB);
}
//add field for new category with a text field that will become active on clicking the checkbox
Button submitButton = new Button(this);
submitButton.setId(99);
submitButton.setText("Submit");
View submitButtonListener = findViewById(99);
submitButtonListener.setOnClickListener(this);
tl.addView(submitButton);
//Add table layout to the scroll view
sv.addView(tl);
//Add scroll view to the main layout
layoutMain.addView(sv);
this.setContentView(layoutMain);
}
}
Your activity isn't implementing View.OnClickListener. Change your class definition to
public class RegisterScreen extends Activity implements View.OnClickListener
and add
public void onClick(View v) {
}
to your Activity class.
RegisterScreen should implement the View.OnClickListener interface i.e.
public class RegisterScreen extends Activity implements View.OnClickListener
and then provide an implementation for the onClick method.

Categories

Resources