dimanche 6 juillet 2014

Error in getting value from tablelayout on button click event


Vote count:

0




In my application, i am getting value from the server and displaying it in form of tablelayout. I am not storing value anywhere. There is a button in the beginning of each row and when the user presses that button, the value first column and first row should in inserted into a variable. I want to store the value of field called "product_code" in to variable.


sorry for my bad english. here is my code



public class FancyStock extends Activity implements View.OnClickListener {

String data = "";
TableLayout tl;
TableRow tr;
TextView label;
Button btn;
ArrayList<Users_fancystock> users = new ArrayList<Users_fancystock>();



@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fancystock);

tl = (TableLayout) findViewById(R.id.main_table);

final GetDatafromDB_fancystock getdb = new GetDatafromDB_fancystock();
new Thread(new Runnable() {
public void run() {
data = getdb.getDataFromDB();
System.out.println(data);

runOnUiThread(new Runnable() {
@Override
public void run() {
ArrayList<Users_fancystock> users = parseJSON(data);
addData(users);
}
});

}
}).start();



}

public ArrayList<Users_fancystock> parseJSON(String result) {
ArrayList<Users_fancystock> users = new ArrayList<Users_fancystock>();
try {
JSONArray jArray = new JSONArray(result);
for (int i = 0; i < jArray.length(); i++) {
JSONObject json_data = jArray.getJSONObject(i);
Users_fancystock user = new Users_fancystock();
user.setId(json_data.getInt("id"));
user.setProduct_code(json_data.getString("product_code"));
user.setShapes(json_data.getString("shaps"));
user.setPair(json_data.getString("pair"));
user.setCarats(json_data.getString("carats"));
user.setColor(json_data.getString("color"));
user.setClarity(json_data.getString("clarity"));
user.setService(json_data.getString("service"));
user.setPolish(json_data.getString("polish"));
user.setSymetric(json_data.getString("symetric"));
user.setTables(json_data.getString("tables"));
user.setMeasurements(json_data.getString("measurments"));
user.setFlourscne(json_data.getString("flourscne"));
user.setDescription(json_data.getString("description"));
user.setCerticated(json_data.getString("certificated"));
user.setCcode(json_data.getString("ccode"));
user.setCut(json_data.getString("cut"));
user.setTotal(json_data.getString("total"));
user.setFile(json_data.getString("file"));
users.add(user);
}
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
return users;
}



void addHeader(){
/** Create a TableRow dynamically **/
tr = new TableRow(this);

TextView add = new TextView(this);
add.setText("Add");
add.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
add.setPadding(5, 5, 5, 5);
add.setBackgroundColor(Color.parseColor("#BDB76B"));
LinearLayout Ll = new LinearLayout(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(0, 5, 5, 5);
//Ll.setPadding(10, 5, 5, 5);
Ll.addView(add,params);
tr.addView((View)Ll);

/** Creating a TextView to add to the row **/
label = new TextView(this);
label.setText("Product code");

label.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
label.setPadding(5, 5, 5, 5);
label.setBackgroundColor(Color.parseColor("#BDB76B"));
Ll = new LinearLayout(this);
params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
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.

/** Creating Qty Button **/
TextView shapes = new TextView(this);
shapes.setText("Shapes");
shapes.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
shapes.setPadding(5, 5, 5, 5);
shapes.setBackgroundColor(Color.parseColor("#BDB76B"));
Ll = new LinearLayout(this);
params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
params.setMargins(0, 5, 5, 5);
//Ll.setPadding(10, 5, 5, 5);
Ll.addView(shapes,params);
tr.addView((View)Ll); // Adding textview to tablerow.

TextView pair = new TextView(this);
pair.setText("Shapes");
pair.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
pair.setPadding(5, 5, 5, 5);
pair.setBackgroundColor(Color.parseColor("#BDB76B"));
Ll = new LinearLayout(this);
params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
params.setMargins(0, 5, 5, 5);
//Ll.setPadding(10, 5, 5, 5);
Ll.addView(pair,params);
tr.addView((View)Ll); // Adding textview to tablerow.




// Add the TableRow to the TableLayout
tl.addView(tr, new TableLayout.LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
}

@SuppressWarnings({ "rawtypes" })
public void addData(ArrayList<Users_fancystock> users) {

addHeader();

for (Iterator i = users.iterator(); i.hasNext();) {
for (int j = 0; j < users.size(); j++) {

Users_fancystock p = (Users_fancystock) i.next();

/** Create a TableRow dynamically **/
tr = new TableRow(this);

///-----------------------buttpm here-------------------------------------------------------
Button btn = new Button(this);
btn.setTag(j);
System.out.println(btn.getTag());

btn.setText("Add to Basket");
// btn.setTextSize();
btn.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
// btn.setTag(mLinks.get(index));
btn.setOnClickListener(this);
LinearLayout Ll = new LinearLayout(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(5, 2, 2, 2);
params.width = 200;
params.height = 60;
// btn.setLayoutParams(new LinearLayout.LayoutParams(10, 100));
Ll.addView(btn, params);

tr.addView((View) Ll);


/** Creating a TextView to add to the row **/
label = new TextView(this);
label.setText(p.getproduct_code());
label.setId(p.getId());
label.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
label.setPadding(5, 5, 5, 5);
////label.setBackgroundColor(Color.parseColor("#BDB76B"));
Ll = new LinearLayout(this);
params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(5, 2, 2, 2);
//Ll.setPadding(10, 5, 5, 5);
Ll.addView(label, params);
tr.addView((View) Ll); // Adding textView to tablerow.

/** Creating Qty Button **/
TextView place = new TextView(this);
place.setText(p.getShapes());
place.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
place.setPadding(5, 5, 5, 5);
// place.setBackgroundColor(Color.parseColor("#BDB76B"));
Ll = new LinearLayout(this);
params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(0, 2, 2, 2);
//Ll.setPadding(10, 5, 5, 5);
Ll.addView(place, params);
tr.addView((View) Ll); // Adding textview to tablerow.

label = new TextView(this);
label.setText(p.getpair());
label.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
label.setPadding(5, 5, 5, 5);
////label.setBackgroundColor(Color.parseColor("#BDB76B"));
Ll = new LinearLayout(this);
params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(5, 2, 2, 2);
//Ll.setPadding(10, 5, 5, 5);
Ll.addView(label, params);
tr.addView((View) Ll); // Adding textView to tablerow.



// Add the TableRow to the TableLayout
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
}
}
}
public void onClick(View v)
{
int k=(Integer)btn.getTag();
Users_fancystock rowRecord= users.get(k);
System.out.println(rowRecord);

}
}


here is my error



07-07 09:12:45.950 13184-13184/com.diamond.traders D/AndroidRuntime﹕ Shutting down VM
07-07 09:12:45.950 13184-13184/com.diamond.traders W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x415dcba8)
07-07 09:12:45.950 13184-13184/com.diamond.traders E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.diamond.traders, PID: 13184
java.lang.NullPointerException
at com.diamond.traders.FancyStock.onClick(FancyStock.java:669)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)


asked 1 min ago






Aucun commentaire:

Enregistrer un commentaire