在我的应用程序中,我从服务器获取价值并以tablelayout的形式显示。我不是在任何地方存储价值。每行的开头都有一个按钮,当用户按下该按钮时,值第一列和第一行应插入变量中。我想将名为“product_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);
}
}
这是我的错误
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)
新错误
07-07 09:36:53.845 15932-15932/com.diamond.traders E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.diamond.traders, PID: 15932
java.lang.IndexOutOfBoundsException: Invalid index 1, size is 0
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
at java.util.ArrayList.get(ArrayList.java:308)
at com.diamond.traders.FancyStock.onClick(FancyStock.java:670)
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)
答案 0 :(得分:1)
您尚未分配btn
字段。
由于(我推测)你想要点击按钮的标签,只需使用v.getTag()
(提供的v
是点击的视图)。
改变这个:
int k = (Integer)btn.getTag();
进入这个:
int k = (Integer)v.getTag();
我还建议在课程开始时删除Button btn;
。
至少还有一个问题:
runOnUiThread(new Runnable() {
@Override
public void run() {
ArrayList<Users_fancystock> users = parseJSON(data);
addData(users);
}
});
您没有更新文件顶部定义的users
字段。这个可运行的应该是:
public void run() {
users = parseJSON(data);
addData(users);
}