在我的应用程序中,我首先从服务器获取数据,然后以表格的形式在屏幕上显示它,然后我根据用户将其发送回服务器。每行的开头都有一个按钮,当用户按下该按钮时,该行的第一列被提取,然后发送回服务器。服务器从该表中检索数据并将其存储在另一个表中。
我已使用评论标记了按钮onclick事件....
这是我的代码
public class FancyStock extends Activity implements View.OnClickListener {
String data = "";
TableLayout tl;
TableRow tr;
TextView label,tv;
// Button btn;
HttpPost httppost;
StringBuffer buffer;
HttpResponse response;
HttpClient httpclient;
List<NameValuePair> nameValuePairs;
ProgressDialog dialog = null;
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() {
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);
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.
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
}
}
}
///----------------------------button onclick event here-----------------------------
public void onClick(View v)
{
int k = (Integer) v.getTag();
System.out.println("int k" + k);
Users_fancystock rowRecord = users.get(k);
String row_productcode = rowRecord.getproduct_code();
System.out.println(row_productcode);
login(row_productcode);
}
void login(String value){
try{
httpclient=new DefaultHttpClient();
httppost= new HttpPost("http://192.168.0.105/test/book.php"); // make sure the url is correct.
//add your data
nameValuePairs = new ArrayList<NameValuePair>();
// Always use the same variable name for posting i.e the android side variable name and php side variable name should be similar,
nameValuePairs.add(new BasicNameValuePair("productcode",value.trim()));
// $Edittext_value = $_POST['Edittext_value'];
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
//Execute HTTP Post Request
response=httpclient.execute(httppost);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
final String response = httpclient.execute(httppost, responseHandler);
System.out.println("Response : " + response);
runOnUiThread(new Runnable() {
public void run() {
tv.setText("Response from PHP : " + response);
dialog.dismiss();
}
});
if(response.equalsIgnoreCase("Booking")){
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(FancyStock.this, "Booking successful", Toast.LENGTH_SHORT).show();
}
});
}else{
Toast.makeText(FancyStock.this, "Problem in Booking", Toast.LENGTH_SHORT).show();
}
}catch(Exception e){
dialog.dismiss();
System.out.println("Exception : " + e.getMessage());
}
}
}
这是我的PHP代码
<?php
$hostname_localhost ="localhost";
$database_localhost ="testdb";
$username_localhost ="root";
$password_localhost ="";
$localhost = mysql_connect($hostname_localhost,$username_localhost,$password_localhost)
or
trigger_error(mysql_error(),E_USER_ERROR);
mysql_select_db($database_localhost, $localhost);
$productcode = $_POST['productcode'];
$query_search = "INSERT INTO checkorder SELECT * from fancystock where product_code = '$productcode'";
$query_exec = mysql_query($query_search) or die(mysql_error());
//$rows = mysql_num_rows($query_exec);
//echo $rows;
echo "Booking";
?>
这是错误
07-07 12:10:51.035 3638-3638/com.diamond.traders W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x4157dba8)
07-07 12:10:51.045 3638-3638/com.diamond.traders E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.diamond.traders, PID: 3638
java.lang.NullPointerException
at com.diamond.traders.FancyStock.login(FancyStock.java:740)
at com.diamond.traders.FancyStock.onClick(FancyStock.java:697)
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)
here is new exception error after removing dialog.dismiss();
07-07 12:23:50.016 6387-6387/com.diamond.traders I/System.out﹕ Exception : null
07-07 12:23:50.016 6387-6387/com.diamond.traders W/System.err﹕ android.os.NetworkOnMainThreadException
07-07 12:23:50.016 6387-6387/com.diamond.traders W/System.err﹕ at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145)
07-07 12:23:50.016 6387-6387/com.diamond.traders W/System.err﹕ at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
07-07 12:23:50.016 6387-6387/com.diamond.traders W/System.err﹕ at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
07-07 12:23:50.016 6387-6387/com.diamond.traders W/System.err﹕ at libcore.io.IoBridge.connect(IoBridge.java:112)
07-07 12:23:50.016 6387-6387/com.diamond.traders W/System.err﹕ at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
07-07 12:23:50.016 6387-6387/com.diamond.traders W/System.err﹕ at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
07-07 12:23:50.016 6387-6387/com.diamond.traders W/System.err﹕ at java.net.Socket.connect(Socket.java:843)
07-07 12:23:50.016 6387-6387/com.diamond.traders W/System.err﹕ at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
07-07 12:23:50.016 6387-6387/com.diamond.traders W/System.err﹕ at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
07-07 12:23:50.016 6387-6387/com.diamond.traders W/System.err﹕ at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
07-07 12:23:50.016 6387-6387/com.diamond.traders W/System.err﹕ at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
07-07 12:23:50.016 6387-6387/com.diamond.traders W/System.err﹕ at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
07-07 12:23:50.016 6387-6387/com.diamond.traders W/System.err﹕ at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
07-07 12:23:50.016 6387-6387/com.diamond.traders W/System.err﹕ at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
07-07 12:23:50.016 6387-6387/com.diamond.traders W/System.err﹕ at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
07-07 12:23:50.016 6387-6387/com.diamond.traders W/System.err﹕ at com.diamond.traders.FancyStock.login(FancyStock.java:714)
07-07 12:23:50.026 6387-6387/com.diamond.traders W/System.err﹕ at com.diamond.traders.FancyStock.onClick(FancyStock.java:697)
07-07 12:23:50.026 6387-6387/com.diamond.traders W/System.err﹕ at android.view.View.performClick(View.java:4438)
07-07 12:23:50.026 6387-6387/com.diamond.traders W/System.err﹕ at android.view.View$PerformClick.run(View.java:18422)
07-07 12:23:50.026 6387-6387/com.diamond.traders W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:733)
07-07 12:23:50.026 6387-6387/com.diamond.traders W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:95)
07-07 12:23:50.026 6387-6387/com.diamond.traders W/System.err﹕ at android.os.Looper.loop(Looper.java:136)
07-07 12:23:50.026 6387-6387/com.diamond.traders W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5001)
07-07 12:23:50.026 6387-6387/com.diamond.traders W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
07-07 12:23:50.026 6387-6387/com.diamond.traders W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:515)
07-07 12:23:50.026 6387-6387/com.diamond.traders W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
07-07 12:23:50.026 6387-6387/com.diamond.traders W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
07-07 12:23:50.026 6387-6387/com.diamond.traders W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
可能dialog
实例为null
,因为您没有在按钮点击上初始化对话框。在调用null
方法之前检查dismiss()
的对话框实例:
if(dialog !=null){
if(dialog.isShowing())
dialog.dismiss();
}
答案 1 :(得分:0)
我想你忘了提及
dialog = new ProgressDialog(getApplicationContext());
如果系统无法找到对话框,则会返回 NullPointerException
并遵循ρяσѕρєяK的解决方案..
这可能对你有帮助..