get TableRow id - android

I want to get index or id of clicked row in TableLayout. Some of the codes looks:
final TableRow rows = new TableRow(this);
rows.setClickable(true);
Click action of the button, which adds rows to TableLayout
TableRow row = new TableRow(getApplicationContext());
row.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
row.setTag(String.valueOf(queue_number));
row.setClickable(true);
if (queue_number % 2 == 0) {
row.setBackgroundColor(getResources().getColor(R.color.blue_grey));
} else {
row.setBackgroundColor(getResources().getColor(R.color.white));
}
TextView row_queue = new TextView(Satish.this);
row_queue.setText(String.valueOf(queue_number));
row_queue.setPadding(6, 6, 0, 6);
TextView row_product_name = new TextView(Satish.this);
row_product_name.setText(product_name.getText().toString());
row_product_name.setPadding(0, 6, 0, 6);
TextView row_unit_of_measurement = new TextView(Satish.this);
row_unit_of_measurement.setText(unit_of_measurement.getText().toString());
row_unit_of_measurement.setPadding(0, 6, 0, 6);
TextView row_price = new TextView(Satish.this);
row_price.setText(price.getText().toString());
row_price.setPadding(0, 6, 0, 6);
TextView row_sum = new TextView(Satish.this);
row_sum.setText(sum.getText().toString());
row_sum.setPadding(0, 6, 0, 6);
row.addView(row_queue);
row.addView(row_product_name);
row.addView(row_unit_of_measurement);
row.addView(row_price);
row.addView(row_sum);
root_table.addView(row);
queue_number++;
And in the click event of rows looks:
rows.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), String.valueOf(v.getId()), LENGTH_LONG).show();
// client_name.setText(String.valueOf(rows.getChildAt(1)));
// TableRow t = (TableRow) v.getParent();
// Toast.makeText(getApplicationContext(), String.valueOf(t.getId()), LENGTH_LONG).show();
// for (int id = 0; id < root_table.getChildCount(); id++) {
// if (v.getId() == id) {
// Toast.makeText(Satish.this, String.valueOf(r[0].getId()), LENGTH_LONG).show();
// }
// }
}
});
But this event returns just nothing. I don't know what's the wrong in my codes. I couldn't see mistake.
Any helpful comment/answer appreciated.
P.S I have searched out in the internet about this, but couldn't found the correct solution for this problem. But, maybe this question is possibly duplicate.
Edit
I can replace field rows with row. If I do it there'll be only one TableRow element. So code looks like:
final TableRow row = new TableRow(getApplicationContext());
row.setClickable(true);
Click action of the button, which adds rows to TableLayout
row.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
row.setTag(String.valueOf(queue_number));
row.setClickable(true);
if (queue_number % 2 == 0) {
row.setBackgroundColor(getResources().getColor(R.color.blue_grey));
} else {
row.setBackgroundColor(getResources().getColor(R.color.white));
}
TextView row_queue = new TextView(Satish.this);
row_queue.setText(String.valueOf(queue_number));
row_queue.setPadding(6, 6, 0, 6);
TextView row_product_name = new TextView(Satish.this);
row_product_name.setText(product_name.getText().toString());
row_product_name.setPadding(0, 6, 0, 6);
TextView row_unit_of_measurement = new TextView(Satish.this);
row_unit_of_measurement.setText(unit_of_measurement.getText().toString());
row_unit_of_measurement.setPadding(0, 6, 0, 6);
TextView row_price = new TextView(Satish.this);
row_price.setText(price.getText().toString());
row_price.setPadding(0, 6, 0, 6);
TextView row_sum = new TextView(Satish.this);
row_sum.setText(sum.getText().toString());
row_sum.setPadding(0, 6, 0, 6);
row.addView(row_queue);
row.addView(row_product_name);
row.addView(row_unit_of_measurement);
row.addView(row_price);
row.addView(row_sum);
root_table.addView(row);
queue_number++;
In this situation some design problems occur in TableLayout.

I found the best solution for my problem.
I moved the following lines from inside of onCreate() to button click event:
final TableRow row = new TableRow(getApplicationContext());
row.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
row.setId(Integer.valueOf(String.valueOf(queue_number)));
row.setClickable(true);
if (queue_number % 2 == 0) {
row.setBackgroundColor(getResources().getColor(R.color.blue_grey));
} else {
row.setBackgroundColor(getResources().getColor(R.color.white));
}
TextView row_queue = new TextView(Satish.this);
row_queue.setText(String.valueOf(queue_number));
row_queue.setPadding(6, 6, 0, 6);
TextView row_product_name = new TextView(Satish.this);
row_product_name.setText(product_name.getText().toString());
row_product_name.setPadding(0, 6, 0, 6);
TextView row_unit_of_measurement = new TextView(Satish.this);
row_unit_of_measurement.setText(unit_of_measurement.getText().toString());
row_unit_of_measurement.setPadding(0, 6, 0, 6);
TextView row_price = new TextView(Satish.this);
row_price.setText(price.getText().toString());
row_price.setPadding(0, 6, 0, 6);
TextView row_sum = new TextView(Satish.this);
row_sum.setText(sum.getText().toString());
row_sum.setPadding(0, 6, 0, 6);
row.addView(row_queue);
row.addView(row_product_name);
row.addView(row_unit_of_measurement);
row.addView(row_price);
row.addView(row_sum);
root_table.addView(row/*, queue_number*/);
// set onClickListener at runtime. This is working well for me.
root_table.getChildAt(queue_number).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TableRow tr = (TableRow) v;
TextView num, p_n, u_o_m, p, s;
// num = (TextView) tr.getChildAt(0);
p_n = (TextView) tr.getChildAt(1);
u_o_m = (TextView) tr.getChildAt(2);
p = (TextView) tr.getChildAt(3);
s = (TextView) tr.getChildAt(4);
// row_id = Integer.getInteger(num.getText().toString());
product_name.setText(p_n.getText().toString());
unit_of_measurement.setText(u_o_m.getText().toString());
price.setText(p.getText().toString());
sum.setText(s.getText().toString());
}
});
queue_number++;

Related

Getting null in getTag() used with editText

I'm working on an app created for the purpose of auditing the hotels. I have already asked a couple questions regarding this. Help needed again.
This is a method used to create layout programatically based on the type of 'question' extracted from JSON:
public void getQuestion(String page) {
.
.
.
for (int i = 0; i < questionList.size(); i++) {
LL.setId(i);
if (questionList.get(i).categoryName.equalsIgnoreCase("general")) {
TableRow row = new TableRow(getActivity());
row.setId(i);
row.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
//LinearLayout generalLayout = new LinearLayout(AuditForm.this,)
if (questionList.get(i).questionType.equalsIgnoreCase("editText")) {
TextView txtLable = new TextView(getActivity());
txtLable.setId(i);
txtLable.setHeight(110);
txtLable.setPadding(0, 8, 0, 0);
txtLable.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);
txtLable.setText(questionList.get(i).questionText);
txtLable.setTag(questionList.get(i).questId);
if (questionList.get(i).questionText.equalsIgnoreCase("Date of inspection(dd-mm-yyyy)")) {
edtdateBox = new EditText(getActivity());
edtdateBox.setHeight(70);
edtdateBox.setPadding(2, 5, 3, 3);
edtdateBox.setId(edtId_1);
edtdateBox.setPadding(5, 0, 0, 3) edtdateBox.setTag(questionList.get(i).questionText);
edtdateBox.setClickable(true);
edtdateBox.setKeyListener(null);
View v =new View(getActivity());
v.setMinimumHeight(70);
v.setPadding(2, 5, 3, 3);
v.setId(edtId*1000);
edtdateBox.setText(inspctDate);
LL.addView(txtLable);
LL.addView(edtdateBox);
LL.addView(v);
}
else if (questionList.get(i).questionText.equalsIgnoreCase("This Audit Is For City:")) {
edtdateBox = new EditText(getActivity());
edtdateBox.setHeight(70);
edtdateBox.setPadding(2, 5, 3, 3);
edtdateBox.setId(edtId_1);
edtdateBox.setPadding(5, 0, 0, 3);
edtdateBox.setText(PropertySelectionPage.selectedCity);
edtdateBox.setTag(questionList.get(i).questionText);
edtdateBox.setClickable(true);
edtdateBox.setKeyListener(null);
View v =new View(getActivity());
v.setMinimumHeight(70);
v.setPadding(2, 5, 3, 3);
v.setId(edtId*1000);
LL.addView(txtLable);
LL.addView(edtdateBox);
LL.addView(v);
} else if (questionList.get(i).questionText.equalsIgnoreCase("This Audit Is For Property:")) {
edtdateBox = new EditText(getActivity());
edtdateBox.setHeight(70);
edtdateBox.setPadding(2, 5, 3, 3);
edtdateBox.setId(edtId_1);
edtdateBox.setPadding(5, 0, 0, 3);
edtdateBox.setText(PropertySelectionPage.selectedProperty);
edtdateBox.setTag(questionList.get(i).questionText);
edtdateBox.setClickable(true);
edtdateBox.setKeyListener(null);
View v =new View(getActivity());
v.setMinimumHeight(70);
v.setPadding(2, 5, 3, 3);
v.setId(edtId*1000);
LL.addView(txtLable);
LL.addView(edtdateBox);
LL.addView(v);
}
else if (questionList.get(i).questionText.equalsIgnoreCase("Property Address:")) {
edtdateBox = new EditText(getActivity());
edtdateBox.setHeight(70);
edtdateBox.setPadding(2, 5, 3, 3);
edtdateBox.setId(edtId_1);
edtdateBox.setPadding(5, 0, 0, 3);
edtdateBox.setText(PropertySelectionPage.propAddress);
edtdateBox.setTag(questionList.get(i).questionText);
edtdateBox.setClickable(true);
edtdateBox.setKeyListener(null);
View v =new View(getActivity());
v.setMinimumHeight(70);
v.setPadding(2, 5, 3, 3);
v.setId(edtId*1000);
LL.addView(txtLable);
LL.addView(edtdateBox);
LL.addView(v);
}
else {
EditText edtBox = new EditText(getActivity());
edtBox.setHeight(70);
edtBox.setPadding(2, 5, 3, 3);
edtBox.setId(edtId);
edtBox.setPadding(5, 0, 0, 3);
edtBox.setTag(questionList.get(i).questionText);
View v =new View(getActivity());
v.setMinimumHeight(70);
v.setPadding(2, 5, 3, 3);
v.setId(edtId*1000);
LL.addView(txtLable);
LL.addView(edtBox);
LL.addView(v);
edtId++;
}
.
.
.
.
//Similar code for other types of questions (like spinner, ratingBar etc.)
}
The views in above method are used in this method:
private void loopQuestions(ViewGroup parent) {
JSONArray ansArry = new JSONArray();
JSONObject params;
String inspectedDate = "", InspectedBy = "" , prop_city = "", prop_Name = "" ;
// Property p = null;
try {
//loop1
Log.e("Total Child", parent.getChildCount() + "");
for (int i = 0; i < parent.getChildCount(); i++) {
View child = parent.getChildAt(i);
String header =""; // getHeader(i);
if (child instanceof LinearLayout) {
//Support for linearlayout
ViewGroup llchild = (LinearLayout) child;
//loop 2
for (int j = 0; j < llchild.getChildCount(); j++) {
View subChild = llchild.getChildAt(j);
params = new JSONObject();
if (subChild instanceof EditText) {
nosOfQustion++;
EditText edtText = (EditText) subChild;
//USING THE BELOW LINE PREVENTS CRASHING OF APP
if (edtText.getTag() == null){ break;}
if (edtText.getTag().toString().equalsIgnoreCase("Inspected By")) {
InspectedBy = getText(edtText.getText().toString().trim());
} else {
params.put("Question", edtText.getTag() + "");
params.put("Answer", getText(edtText.getText().toString()));
params.put("Category", header);
ansArry.put(params);
}
.
.
.
I have indicated in the comment in above method that using `"if (edtText.getTag() == null){ break;}" prevents crashing of app. But the problem is that the value of 'edtTxt' is still null, because of which I'm not able to do tasks like saving the audit.
Also, the above methods are in a fragment created to serve as a Tab in TabLayout . I have called the first method (getQuestion()) in the onViewCreated() method. Tried to call it in onCreateView(), but got NullPointerException on doing that.
Please help!
if you want get text from edittext text then use below code.
edtText.getText().toString();
For compare value of edittext use below code.
String edtText = edtAns.getText().toString();
if (edtText.isEmpty()) {
Toast.makeText(QuestionActivity.this, "Please enter correct value.", Toast.LENGTH_SHORT).show();
}

how to solve android.os.NetworkOnMainThreadException?

public class DoLogin extends AsyncTask<String,String,String>
{
ResultSet rs2;
String z = "";
Boolean isSuccess = false;
TextView DateOfBooking,Product,CustomerName,
Quantity,Destination,DealerName,Remarks,DueDate;
ArrayList DateOfBooking1 = new ArrayList();
ArrayList Product1 = new ArrayList();
ArrayList CustomerName1 = new ArrayList();
ArrayList Quantity1 = new ArrayList();
ArrayList Destination1 = new ArrayList();
ArrayList DealerName1 = new ArrayList();
ArrayList Remarks1 = new ArrayList();
ArrayList DueDate1 = new ArrayList();
#Override
protected void onPreExecute() {
}
#Override
protected void onPostExecute(String r) {
Toast.makeText(OrderRequest.this, r, Toast.LENGTH_SHORT).show();
if(isSuccess) {
try {
addHeaders();
do{
s1 = rs2.getString(1);
DateOfBooking1.add(s1);
s2 = rs2.getString(2);
CustomerName1.add(s2);
s3 = rs2.getString(3);
Destination1.add(s3);
s4 = rs2.getString(4);
DealerName1.add(s4);
s5 = rs2.getString(5);
Product1.add(s5);
s6 = rs2.getString(6);
Quantity1.add(s6);
s7 =rs2.getString(7);
Remarks1.add(s7);
s8 =rs2.getString(8);
DueDate1.add(s8);
}while(rs2.next());
if (DateOfBooking1.size() != 0) {
for (int j = 0; j < DateOfBooking1.size(); j++) {
/** Create a TableRow dynamically **/
tr = new TableRow(OrderRequest.this);
tr.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
/** Creating a TextView to add to the row **/
DateOfBooking = new TextView(OrderRequest.this);
DateOfBooking.setText(DateOfBooking1.get(j).toString());
DateOfBooking.setTextColor(Color.BLACK);
DateOfBooking.setTypeface(Typeface.DEFAULT,
Typeface.ITALIC);
DateOfBooking.setLayoutParams(new
TableRow.LayoutParams
(TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
DateOfBooking.setPadding(5, 5, 5, 5);
DateOfBooking.setId(j);
tr.addView(DateOfBooking); // Adding textView to
tablerow.
CustomerName = new TextView(OrderRequest.this);
CustomerName.setText(CustomerName1.get(j).toString());
CustomerName.setTextColor(Color.BLACK);
CustomerName.setTypeface(Typeface.DEFAULT,
Typeface.ITALIC);
CustomerName.setLayoutParams(new
TableRow.LayoutParams
(TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
CustomerName.setPadding(5, 5, 5, 5);
CustomerName.setId(j);
tr.addView(CustomerName); // Adding textView to
tablerow.
Destination = new TextView(OrderRequest.this);
Destination.setText(Destination1.get(j).toString());
Destination.setTextColor(Color.BLACK);
Destination.setTypeface(Typeface.DEFAULT,
Typeface.ITALIC);
Destination.setLayoutParams(new
TableRow.LayoutParams
(TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
Destination.setPadding(5, 5, 5, 5);
Destination.setId(j);
tr.addView(Destination); // Adding textView to
tablerow.
DealerName = new TextView(OrderRequest.this);
DealerName.setText(DealerName1.get(j).toString());
DealerName.setTextColor(Color.BLACK);
DealerName.setTypeface(Typeface.DEFAULT,
Typeface.ITALIC);
DealerName.setLayoutParams(new TableRow.LayoutParams
(TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
DealerName.setPadding(5, 5, 5, 5);
DealerName.setId(j);
tr.addView(DealerName); // Adding textView to
tablerow.
Product = new TextView(OrderRequest.this);
Product.setText(Product1.get(j).toString());
Product.setTextColor(Color.BLACK);
Product.setTypeface(Typeface.DEFAULT,
Typeface.ITALIC);
Product.setLayoutParams(new TableRow.LayoutParams
(TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
Product.setPadding(5, 5, 5, 5);
Product.setId(j);
tr.addView(Product); // Adding textView to
tablerow.
Quantity = new TextView(OrderRequest.this);
Quantity.setText(Quantity1.get(j).toString());
Quantity.setTextColor(Color.BLACK);
Quantity.setTypeface(Typeface.DEFAULT,
Typeface.ITALIC);
Quantity.setLayoutParams(new TableRow.LayoutParams
(TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
Quantity.setPadding(5, 5, 5, 5);
Quantity.setId(j);
tr.addView(Quantity); // Adding textView to
tablerow.
Remarks = new TextView(OrderRequest.this);
Remarks.setText(Remarks1.get(j).toString());
Remarks.setTextColor(Color.BLACK);
Remarks.setTypeface(Typeface.DEFAULT,
Typeface.ITALIC);
Remarks.setLayoutParams(new TableRow.LayoutParams
(TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
Remarks.setPadding(5, 5, 5, 5);
Remarks.setId(j);
tr.addView(Remarks); // Adding textView to
tablerow.
DueDate = new TextView(OrderRequest.this);
DueDate.setText(DueDate1.get(j).toString());
DueDate.setTextColor(Color.BLACK);
DueDate.setTypeface(Typeface.DEFAULT,
Typeface.ITALIC);
DueDate.setLayoutParams(new
TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
DueDate.setPadding(5, 5, 5, 5);
DueDate.setId(j);
tr.addView(DueDate); // Adding textView to
tablerow.
tl.addView(tr, new TableLayout.LayoutParams(
TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
}
} else {
Toast.makeText(OrderRequest.this,
DateOfBooking1+""+Product1+""+DealerName1
+""+Destination1+""+DueDate1+""+CustomerName1
+""+Quantity1+""+Remarks1+"Sorry.....",
Toast.LENGTH_LONG).show();
}
}catch(Exception e)
{
Log.e("showing",e+"");
}
}
}
#Override
protected String doInBackground(String... params) {
try {
Connection con = (Connection) connectionClass.CONN();
if (con == null) {
z = "Error in connection with SQL server";
} else {
String query = "select
DocDate,CustomerName,Destination,DealerName,
ProductName,Quantity,Remarks,DueDate from [Dealer].[dbo].
[BookingOrder]";
Statement stmt = con.createStatement();
rs2 = stmt.executeQuery(query);
try {
if (rs2.next()) {
isSuccess = true;
z = "Successfully Viewed";
}
}catch (Exception n)
{
z = "selecting";
Log.e("selecting",n+"");
}
}
}
catch (Exception ex)
{
isSuccess = false;
z = "Exceptions";
Log.e("Exc", ex + "");
return null;
}
return z;
}
}
I am getting exception as "android.os.NetworkOnMainThreadException",Resultset is giving me the values which are located in SQL server.but the code after getting values from resultset is not executing.How can I overcome this exception,Already I checked this exception,but the solutions are not working.Help me.Thank you in advance.
android.os.NetworkOnMainThreadException
is thrown when an application attempts to perform a network related operation on the main thread.
This is only thrown for applications targeting the Honeycomb SDK or higher versions. Ensure that your application is not attempting to perform any network
related operation on its main thread.

Changing background color of table row according to clicks

I have a table layout with table rows building according to data given by JSON. I want to highlight a row after pressing it but I need that only one row to be highlighted in every given time, it means that if I highlight a row and then press another row the last row should be highlighted and the first row should not. Everything is working fine but I can't get a row to not be highlighted after it became highlighted. This is my code:
TableLayout tv = (TableLayout) findViewById(R.id.tblFrnd);
tv.removeAllViewsInLayout();
int flag = 1;
for (int i = -1; i < ans.length(); i++) {
TableRow tr = new TableRow(AddFriend.this);
tr.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
if (flag == 1) {
TextView b3 = new TextView(AddFriend.this);
b3.setText("Display Name");
b3.setTextColor(Color.BLUE);
b3.setTextSize(20);
b3.setPadding(10, 0, 0, 0);
tr.addView(b3);
TextView b4 = new TextView(AddFriend.this);
b4.setPadding(50, 0, 0, 0);
b4.setTextSize(20);
b4.setText("User Name");
b4.setTextColor(Color.BLUE);
tr.addView(b4);
TextView b5 = new TextView(AddFriend.this);
b5.setPadding(90, 0, 0, 0);
b5.setTextSize(20);
b5.setText("ID");
b5.setVisibility(View.INVISIBLE);
b5.setTextColor(Color.BLUE);
tr.addView(b5);
tv.addView(tr);
final View vline = new View(AddFriend.this);
vline.setLayoutParams(new
TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 2));
vline.setBackgroundColor(Color.BLUE);
tv.addView(vline);
flag = 0;
} else {
JSONObject json_data = ans.getJSONObject(i);
TextView b = new TextView(AddFriend.this);
String str = json_data.getString("DisplayName");
b.setText(str);
b.setPadding(10, 0, 0, 0);
b.setTextColor(Color.RED);
b.setTextSize(15);
tr.addView(b);
TextView b1 = new TextView(AddFriend.this);
b1.setPadding(50, 0, 0, 0);
b1.setTextSize(15);
String str1 = json_data.getString("UserName");
b1.setText(str1);
b1.setTextColor(Color.RED);
tr.addView(b1);
final TextView b2 = new TextView(AddFriend.this);
b2.setPadding(90, 0, 0, 0);
b2.setTextSize(15);
String str2 = String.valueOf(json_data.getInt("UserID"));
b2.setText(str2);
b2.setVisibility(View.INVISIBLE);
b2.setTextColor(Color.RED);
tr.addView(b2);
tr.setClickable(true);
tr.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
v.setBackgroundColor(getResources().getColor(R.color.Yellow));
}
});
tv.addView(tr);
final View vline1 = new View(AddFriend.this);
vline1.setLayoutParams(new
TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 1));
vline1.setBackgroundColor(Color.WHITE);
tv.addView(vline1);
}
Thank you in advance!
Take int selectedRow = -1 in class.
Add a function in the class:
private void updateHighlightedRow()
{
// loop through the rows and set the background
// for a row to highlighted color if the row index == selectedRow
// else sent normal bg color
}
While adding row, make the row clickable and in click listener set row index to selectedRow, then call updateHighlightedRow
This should do the things.
make the textview setClickable(false).

SetOnClickListener to change view in List Linearlayout

i have created list linearlayout with my code
String[] name = {"Chicken", "Beef", "Milk" ,"Juice"};
final Integer[] price = {10, 50, 5, 3};
final Integer[] qty = {1, 2, 5, 5};
listOrder.removeAllViews();
LinearLayout.LayoutParams image = new LinearLayout.LayoutParams(Utility.getDip(175, CartActivity.this), Utility.getDip(70, CartActivity.this));
LinearLayout.LayoutParams text = new LinearLayout.LayoutParams(Utility.getDip(140, CartActivity.this), Utility.getDip(80, CartActivity.this));
LinearLayout.LayoutParams textQty = new LinearLayout.LayoutParams(Utility.getDip(100, CartActivity.this), Utility.getDip(80, CartActivity.this));
LinearLayout.LayoutParams textPrice = new LinearLayout.LayoutParams(Utility.getDip(125, CartActivity.this), Utility.getDip(80, CartActivity.this));
LinearLayout.LayoutParams textTotal = new LinearLayout.LayoutParams(Utility.getDip(130, CartActivity.this), Utility.getDip(80, CartActivity.this));
LinearLayout.LayoutParams editImage = new LinearLayout.LayoutParams(Utility.getDip(40, CartActivity.this), Utility.getDip(40, CartActivity.this));
for(int i = 0; i < 4; i++){
list = new LinearLayout(ctx);
list.setOrientation(LinearLayout.HORIZONTAL);
//if((i % 2) == 0) list.setBackgroundColor(R.color.bg_grey);
listImage = new ImageView(ctx);
//listImage.setImageDrawable(getResources().getDrawable(R.drawable.sample_logo_publisher));
listImage.setBackgroundColor(getResources().getColor(R.color.bg_grey));
listImage.setAdjustViewBounds(Boolean.TRUE);
listImage.setScaleType(ScaleType.FIT_XY);
image.gravity = Gravity.CENTER;
image.setMargins(Utility.getDip(10, CartActivity.this), 0, Utility.getDip(10, CartActivity.this), 0);
list.addView(listImage, image);
listText = new TextView(ctx);
listText.setText(name[i]);
listText.setGravity(Gravity.CENTER);
text.gravity = Gravity.CENTER;
text.setMargins(Utility.getDip(10, CartActivity.this), 0, 0, 0);
list.addView(listText, text);
listQty = new TextView(ctx);
listQty.setText(qty[i].toString());
listQty.setGravity(Gravity.CENTER);
textQty.gravity = Gravity.CENTER;
textQty.setMargins(Utility.getDip(5, CartActivity.this), 0, Utility.getDip(5, CartActivity.this), 0);
list.addView(listQty, textQty);
listPrice = new TextView(ctx);
listPrice.setText(price[i].toString());
listPrice.setGravity(Gravity.CENTER_VERTICAL);
textPrice.gravity = Gravity.CENTER;
list.addView(listPrice, textPrice);
Integer total = qty[i]*price[i];
listTotal = new TextView(ctx);
listTotal.setText(total.toString());
listTotal.setGravity(Gravity.CENTER_VERTICAL);
textTotal.gravity = Gravity.CENTER;
textTotal.setMargins(Utility.getDip(5, CartActivity.this), 0, Utility.getDip(2, CartActivity.this), 0);
list.addView(listTotal, textTotal);
plus = new ImageView(ctx);
plus.setImageDrawable(getResources().getDrawable(R.drawable.plus));
plus.setAdjustViewBounds(Boolean.TRUE);
plus.setScaleType(ScaleType.FIT_XY);
editImage.gravity = Gravity.CENTER;
list.addView(plus, editImage);
final int pos = i;
plus.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
qty[pos] = qty[pos] + 1;
listQty.setText(qty[pos].toString());
listTotal.setText(""+ (qty[pos]*price[pos]));
}
});
minus = new ImageView(ctx);
minus.setImageDrawable(getResources().getDrawable(R.drawable.min));
minus.setAdjustViewBounds(Boolean.TRUE);
minus.setScaleType(ScaleType.FIT_XY);
editImage.gravity = Gravity.CENTER;
list.addView(minus, editImage);
remove = new ImageView(ctx);
remove.setImageDrawable(getResources().getDrawable(R.drawable.trash));
remove.setAdjustViewBounds(Boolean.TRUE);
remove.setScaleType(ScaleType.FIT_XY);
editImage.gravity = Gravity.CENTER;
list.addView(remove, editImage);
listOrder.addView(list);
}
I want to add quantity when onclick on plus image. But when i clicked plus image in the list, textQty changed in end of the list, not in the same list that I have clicked the image.
how to fix my code?
You need to link the TextViews and button together. They don't know they are in the same iteration of the for loop.
You could put each set of buttons and textviews into an array when you go through your loop.
Then in your onClickListener you would for do something like:
for(int i = 0; i < buttonArray.length; i++){
if(v == buttonArray[i]){
// make whatever changes to TextViews at index i
}
}
I have resolved my problem. Here is my code ...
I set id in listQty and listTotal
listQty = new TextView(ctx);
listQty.setId(99000+i);
listQty.setText(qty[i].toString());
listQty.setGravity(Gravity.CENTER);
textQty.gravity = Gravity.CENTER;
textQty.setMargins(Utility.getDip(5, CartActivity.this), 0, Utility.getDip(5, CartActivity.this), 0);
list.addView(listQty, textQty);
listTotal = new TextView(ctx);
listTotal.setId(98000+i);
listTotal.setText(total.toString());
listTotal.setGravity(Gravity.CENTER_VERTICAL);
textTotal.gravity = Gravity.CENTER;
textTotal.setMargins(Utility.getDip(5, CartActivity.this), 0, Utility.getDip(2, CartActivity.this), 0);
list.addView(listTotal, textTotal);
and in clicklistener set variable of listQty and listTotal
plus = new ImageView(ctx);
plus.setImageDrawable(getResources().getDrawable(R.drawable.plus));
plus.setAdjustViewBounds(Boolean.TRUE);
plus.setScaleType(ScaleType.FIT_XY);
editImage.gravity = Gravity.CENTER;
list.addView(plus, editImage);
final int pos = i;
plus.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
listQty = (TextView)findViewById(99000+pos);
listTotal = (TextView)findViewById(98000+pos);
qty[pos] = qty[pos] + 1;
listQty.setText(qty[pos].toString());
listTotal.setText(""+ (qty[pos]*price[pos]));
}
});

Increase width of tablerow of dynamically created tablelayout

I have created a table layout in which my table row height is too small, so I want to increase the height of the table row , please guide me table is dynamically created so I don't have any idea.
Code: I have tried to specify width in Latoutparam integer but not working
public void createTableLayout()
{
String status="Pending";
TextView txtid=(TextView)findViewById(R.id.txtcid);
//TextView txtdate=(TextView)findViewById(R.id.txtDate);
txtid.setText("Customer Id/Name : "+shopnum+"/"+shopname1);
btnorder1=(Button)findViewById(R.id.btnOrder123);
btnBack1=(Button)findViewById(R.id.btnBack1);
txtTotalAmount=(TextView)findViewById(R.id.txtTotalAmt);
btnorder1.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
ShowcOrder1();
int rcount=(table).getChildCount();
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
String currentDateandTime = sdf.format(new Date());
for(int r=1;r<rcount;r++)
{
TableRow row=(TableRow)table.getChildAt(r);
int vcount=(row).getChildCount();
// String str=
for(int v1=0;v1<vcount;v1++)
{
TextView tv = (TextView)(((TableRow)row)).getChildAt(v1);
insertIntoOrderDetails(String.valueOf(orderid),ProdcutId,PackageId,Quantity);
}
insertIntoOrder(String.valueOf(orderid), currentDateandTime, shopnum,String.valueOf(roundtotal), sid);
Toast.makeText(getApplicationContext(), "Order Placed",Toast.LENGTH_LONG).show();
finish();
// Toast.makeText(getApplicationContext(), "round: "+roundtotal,Toast.LENGTH_LONG).show();
}
});
btnBack1.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View arg0)
{
// TODO Auto-generated method stub
finish();
}
});
//txtdate.setText("Order Date is : "+odate);
table = (TableLayout) findViewById(R.id.tbpastorderdetails);
TableRow tr_heading = new TableRow(Pastorder.this);
tr_heading.setId(10);
tr_heading.setBackgroundColor(Color.BLACK);
tr_heading.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
TextView label_product_id = new TextView(Pastorder.this);
label_product_id.setId(20);
label_product_id.setText("PID");
label_product_id.setTextColor(Color.WHITE);
//label_question.setPadding(2, 2, 2, 2);
// label_date.setPadding(5, 5, 5, 5);
tr_heading.addView(label_product_id); // add the column to the table row
//TextView label_question = new TextView(this);
TextView label_package_id = new TextView(Pastorder.this);
label_package_id.setId(20);
label_package_id.setText(" Pack.ID");
label_package_id.setTextColor(Color.WHITE);
//label_question.setPadding(2, 2, 2, 2);
// label_date.setPadding(5, 5, 5, 5);
tr_heading.addView(label_package_id); // add the column to the table row
//TextView label_question = new TextView(this);
TextView label_product = new TextView(Pastorder.this);
label_product.setId(20);
label_product.setText(" PRODUCT");
label_product.setTextColor(Color.WHITE);
//label_question.setPadding(2, 2, 2, 2);
// label_date.setPadding(5, 5, 5, 5);
tr_heading.addView(label_product); // add the column to the table row
// here
// Toast.makeText(ExaminationActivity.this,"hi try ",Toast.LENGTH_LONG).show();
//TextView label_answer = new TextView(this);
TextView label_catagory = new TextView(Pastorder.this);
label_catagory.setId(21);// define id that must be unique
label_catagory.setText(" CATAGORY"); // set the text for the header
label_catagory.setTextColor(Color.WHITE); // set the color
// label_weight_kg.setPadding(5, 5, 5, 5); // set the padding (if
// required)
// label_weight_kg.setPadding(2, 2, 2, 2);
tr_heading.addView(label_catagory); // add the column to the table row
// here
//TextView label_youranswer = new TextView(this);
TextView label_package = new TextView(Pastorder.this);
label_package.setId(20);
label_package.setText(" PACKAGE");
label_package.setTextColor(Color.WHITE);
//label_question.setPadding(2, 2, 2, 2);
// label_date.setPadding(5, 5, 5, 5);
tr_heading.addView(label_package); // add the column to the table row
// here
TextView label_weight = new TextView(Pastorder.this);
label_weight.setId(20);
label_weight.setText(" WEIGHT");
label_weight.setTextColor(Color.WHITE);
//label_question.setPadding(2, 2, 2, 2);
// label_date.setPadding(5, 5, 5, 5);
tr_heading.addView(label_weight); // add the column to the table row
TextView label_qty = new TextView(Pastorder.this);
label_qty.setId(20);
label_qty.setText(" QUANTITY");
label_qty.setTextColor(Color.WHITE);
//label_question.setPadding(2, 2, 2, 2);
// label_date.setPadding(5, 5, 5, 5);
tr_heading.addView(label_qty); // add the column to the table row
TextView label_amt = new TextView(Pastorder.this);
label_amt.setId(20);
label_amt.setText(" AMOUNT");
label_amt.setTextColor(Color.WHITE);
//label_question.setPadding(2, 2, 2, 2);
// label_date.setPadding(5, 5, 5, 5);
tr_heading.addView(label_amt); // add the column to the table row
table.addView(tr_heading, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
}
public void addRows()
{
try
{
msaledb = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE,null);
Cursor allrows = msaledb.rawQuery("SELECT * FROM " + PRODUCT_TABLE+" WHERE Product_ID = '" + pid +"'", null);
Cursor allrows1 = msaledb.rawQuery("SELECT * FROM " + PACKAGING_TABLE ,null);
if(allrows.moveToFirst() && allrows1.moveToFirst())// && allrows1.moveToFirst())
{
int i=1;
do
{
tr = new TableRow(Pastorder.this);
tr.setId(i);
tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
TextView tvpid = new TextView(Pastorder.this);
tvpid.setId(11);
tvpid.setText(""+pid);
tvpid.setPadding(2, 0, 5, 0);
tvpid.setTextColor(Color.BLACK);
tr.addView(tvpid);
TextView tv_pack_id = new TextView(Pastorder.this);
tv_pack_id.setId(12);
tv_pack_id.setText(" "+packid);
tv_pack_id.setPadding(2, 0, 5, 0);
tv_pack_id.setTextColor(Color.BLACK);
tr.addView(tv_pack_id);
TextView tv = new TextView(Pastorder.this);
tv.setId(1);
tv.setText(" "+allrows.getString(2));
tv.setPadding(2, 0, 5, 0);
tv.setTextColor(Color.BLACK);
tr.addView(tv);
TextView tv1 = new TextView(Pastorder.this);
tv1.setId(2);
tv1.setText(" "+allrows.getString(3));
tv1.setPadding(2, 0, 5, 0);
tv1.setTextColor(Color.BLACK);
tr.addView(tv1);
do
{
if(allrows1.getString(1).trim().equals(packid.trim()) && allrows1.getString(2).trim().equals(pid.trim()))
{
// Toast.makeText(getApplicationContext(), ""+allrows1.getString(2).trim(),Toast.LENGTH_LONG).show();
// Toast.makeText(getApplicationContext(), ""+p_id.get(0).trim(),Toast.LENGTH_LONG).show();
TextView tv2 = new TextView(Pastorder.this);
tv2.setId(3);
tv2.setText(" "+allrows1.getString(3));
tv2.setPadding(2, 0, 5, 0);
tv2.setTextColor(Color.BLACK);
tr.addView(tv2);
TextView tv3 = new TextView(Pastorder.this);
tv3.setId(4);
tv3.setText(" "+allrows1.getString(4));
tv3.setPadding(2, 0, 5, 0);
tv3.setTextColor(Color.BLACK);
tr.addView(tv3);
TextView tv4 = new TextView(Pastorder.this);
tv4.setId(4);
tv4.setText(" "+qty);
tv4.setPadding(2, 0, 5, 0);
tv4.setTextColor(Color.BLACK);
tr.addView(tv4);
TextView tv5 = new TextView(Pastorder.this);
tv5.setId(4);
tv5.setText(" "+Integer.parseInt(allrows1.getString(6).trim())*Integer.parseInt(qty.trim()));
tv5.setPadding(2, 0, 5, 0);
tv5.setTextColor(Color.BLACK);
tr.addView(tv5);
}
}while(allrows1.moveToNext());
table.addView(tr, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
i++;
tr.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view)
{
view.setBackgroundColor(Color.LTGRAY);
// Toast.makeText(getApplicationContext(),""+tr.getId(),Toast.LENGTH_LONG).show();
tr1=(TableRow)view; //assign selected TableRow gobally
registerForContextMenu(tr1);
openContextMenu(tr1);
}
});
}while(allrows.moveToNext());
}
}
catch(Exception e)
{
Toast.makeText(getApplicationContext(), "Error in search", Toast.LENGTH_LONG).show();
}
}
The TableRow layout parameters must be wrap content. Instead of trying to expand the row in order to fit your needs, try to manually set the appropriate layout parameters in the views that are inside the TableRow object. Wrap content or fill parent won't work, try playing around with actual density pixels.
Hope this helps

Categories

Resources