让我解释一下该应用程序的简短描述,以便您轻松理解我的问题。
用户可以保存项目名称、项目描述和照片。它保存在应用程序中,并将其发送到数据库的服务器端。在应用程序上,有显示已保存项目的列表视图。在每个项目的布局上,都有编辑按钮来更正名称或描述或图像。如果用户单击他们保存的项目之一,则显示他们之前保存的项目的信息。现在,我想将编辑按钮从列表视图上的项目布局移动到查看页面。
我修改了一些代码,但是,当我单击视图页面上的编辑按钮时,如果我保存它,它会正确保存在应用程序上。
但问题是它没有将其发送到服务器端!..我认为存在传递数据问题..
这是我的代码。
ViewWishlist.java
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view_wishlist);
setUpViews();
//Edit Button Click Event
editBtn = (Button) findViewById(R.id.edit);
editBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(ViewWishlist.this, AddEditWishlists.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Bundle current_item = new Bundle();
current_item.putInt("id", id);
current_item.putString("name", name.getText().toString());
current_item.putString("note", note.getText().toString());
current_item.putByteArray("blob", image);
intent.putExtras(current_item);
startActivity(intent);
}
});
}
public void setUpViews() {
name = (TextView) findViewById(R.id.inputname);
note = (TextView) findViewById(R.id.inputnote);
photo = (ImageButton) findViewById(R.id.inputphoto);
// When a photo is clicked, this event would be implemented
photo.setOnClickListener(new ImageButton.OnClickListener(){
// Display bigger images
//send byte array data with bundle to "view_photo.java"
public void onClick(View v){
Intent intent = new Intent(ViewWishlist.this, view_photo.class);
Bundle current_photo = new Bundle();
current_photo.putByteArray("blob", image);
intent.putExtras(current_photo);
startActivity(intent);
}
});
Bundle extras = getIntent().getExtras();
// Get the data from the sent Bundle from CustomWishlistsAdapter.java
if (extras != null) {
id=extras.getInt("id");
name.setText(extras.getString("name"));
note.setText(extras.getString("note"));
image = extras.getByteArray("blob");
if (image != null) {
if (image.length > 3) {
photo.setImageBitmap(BitmapFactory.decodeByteArray(image,0,image.length));
}
}
}
}
AddEditWishlist.java
public class AddEditWishlists extends Activity {
// Client-Server - Start //////////////////////
JSONParser jsonParser = new JSONParser();
// url to create new product http://192.168.1.28/android_connect/create_product.php
private static String url_create_product = "http://192.168.3.9/android_connect/create_product.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
// Client-Server - End //////////////////////
//Define Variables
private EditText inputname;
private EditText inputnote;
private Button upload;
private Bitmap yourSelectedImage;
private ImageView inputphoto;
private Button save;
private int id;
private byte[] blob=null;
byte[] image=null;
/**
* Show the layout when this class is started
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_wishlist);
setUpViews();
}
/**
* Implemented when users click the one of the item on the wishlist
*/
private void setUpViews() {
inputname = (EditText) findViewById(R.id.inputname);
inputnote = (EditText) findViewById(R.id.inputnote);
inputphoto = (ImageView) findViewById(R.id.inputphoto);
Bundle extras = getIntent().getExtras();
if (extras != null) {
id=extras.getInt("id");
inputname.setText(extras.getString("name"));
inputnote.setText(extras.getString("note"));
image = extras.getByteArray("blob");
if (image != null) {
if (image.length > 3) {
inputphoto.setImageBitmap(BitmapFactory.decodeByteArray(image,0,image.length));
}
}
}
//Image Upload Button
upload = (Button) findViewById(R.id.upload);
upload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, 0);
}
});
// Save the data
save = (Button) findViewById(R.id.save);
save.setOnClickListener(new View.OnClickListener() {
/**
* This event is implemented when users save the data
*/
@Override
public void onClick(View v) {
if (inputname.getText().length() != 0) {
AsyncTask<Object, Object, Object> saveContactTask = new AsyncTask<Object, Object, Object>() {
@Override
protected Object doInBackground(Object... params) {
saveItem();
return null;
}
@Override
protected void onPostExecute(Object result) {
finish();
}
};
saveContactTask.execute((Object[]) null);
} else {
AlertDialog.Builder alert = new AlertDialog.Builder(
AddEditWishlists.this);
alert.setTitle("Error In Save Wish List");
alert.setMessage("You need to Enter Name of the Product");
alert.setPositiveButton("OK", null);
alert.show();
}
}
});
}
/** If users save data, this method would be implemented.
* Data would be saved in the android database.
* And also, these data would be sent to the serverDB using HTTP post request with JSON packet.
*/
private void saveItem() {
if(yourSelectedImage!=null){
ByteArrayOutputStream outStr = new ByteArrayOutputStream();
yourSelectedImage.compress(CompressFormat.JPEG, 100, outStr);
blob = outStr.toByteArray();
//Client-Server
// Client-Server - Start //////////////////////////////////////
String name = inputname.getText().toString();
String description = inputnote.getText().toString();
// Encode the image file to String !! by using Base64
String encodedImage = Base64.encodeToString(blob, Base64.DEFAULT);
// Building Parameters
List<NameValuePair> params1 = new ArrayList<NameValuePair>();
params1.add(new BasicNameValuePair("name", name));
params1.add(new BasicNameValuePair("description", description));
params1.add(new BasicNameValuePair("photo",encodedImage));
Log.v("log_tag", System.currentTimeMillis()+".jpg");
// getting JSON Object
// Note that create product url accepts POST method
JSONObject json = jsonParser.makeHttpRequest(url_create_product, "POST", params1);
// check log cat fro response
Log.d("Create Response", json.toString());
// check for success tag
try {
int success = json.getInt(TAG_SUCCESS);
Log.v("log_tag", "In the try Loop" );
if (success == 1) {
// closing this screen
finish();
} else {
// failed to create product
}
} catch (JSONException e) {
e.printStackTrace();
}
}
else{blob=image;}
// Change Text type to string type to save in the DB
SQLiteConnector sqlCon = new SQLiteConnector(this);
if (getIntent().getExtras() == null) {
sqlCon.insertWishlist(inputname.getText().toString(), inputnote.getText().toString(), blob);
}
else {
sqlCon.updateWishlist(id, inputname.getText().toString(), inputnote.getText().toString(), blob);
}
}
数据应该传递给 AddEditWishlist.java 上的“private void saveItem()” 但是,没有传递的数据..
我该如何解决?