Back to project page foodroid.
The source code is released under:
GNU General Public License
If you think the Android project foodroid listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.wmc.ReservationClient; /* www .j av a 2s . co m*/ import java.io.FileInputStream; import java.io.InputStream; import java.util.ArrayList; import java.util.List; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPut; import org.apache.http.impl.client.DefaultHttpClient; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; import android.database.Cursor; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.os.Bundle; import android.preference.PreferenceManager; import android.view.View; import android.widget.ArrayAdapter; import android.widget.CheckBox; import android.widget.EditText; import android.widget.ImageButton; import android.widget.ImageView; import android.widget.ListView; import android.widget.RatingBar; import android.widget.TextView; public class BranchPage extends Activity { ListView listComment; RatingBar ratingbar; ImageButton btnMap; public static final String Branch_ID = "com.wmc.ReservationClient.Branch_ID"; String branchID, lat, lon; String Server; private List<Comment> comments=new ArrayList<Comment>(); DatabaseHelper db; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.branchpage); listComment = (ListView)findViewById(R.id.listComment); btnMap = (ImageButton)findViewById(R.id.imageButtonMap); ratingbar = (RatingBar)findViewById(R.id.ratingBarbranch); ratingbar.setEnabled(false); branchID = getIntent().getExtras().getString(Branch_ID); Server = getString(R.string.Server); try { db = new DatabaseHelper(this); loadBranch(); LoadRating(); LoadComment(); } catch (Exception e) { e.printStackTrace(); } } private void loadBranch() { try { String[] select = {"Name","Picture","Address","Tel","lat","lon"}; Cursor branchCursor = db.SelectTable("branch", select, "ID=" + branchID, ""); branchCursor.moveToFirst(); TextView txtName = (TextView)findViewById(R.id.txtName); txtName.setText(branchCursor.getString(0)); TextView txtTel = (TextView)findViewById(R.id.txtTel); txtTel.setText(branchCursor.getString(3)); TextView txtAddress = (TextView)findViewById(R.id.txtAddress); txtAddress.setText(branchCursor.getString(2)); ImageView imageBranch = (ImageView)findViewById(R.id.imageView1); String urlImage = "/data/data/com.wmc.ReservationClient/" + branchCursor.getString(1); InputStream is = new FileInputStream(urlImage); Bitmap bmp = BitmapFactory.decodeStream(is); imageBranch.setImageBitmap(bmp); lat = branchCursor.getString(4); lon = branchCursor.getString(5); } catch (Exception e) { e.printStackTrace(); } } void LoadRating() { try { String[] select2 = {"sum(rate)","count(rate)"}; Cursor rateCursor = db.SelectTable("comment", select2, "BranchID=" + branchID, ""); rateCursor.moveToFirst(); if(rateCursor.getFloat(1) != 0) { float starRate = (float)rateCursor.getFloat(0)/rateCursor.getFloat(1); ratingbar.setRating(starRate); } } catch(Exception e) { e.printStackTrace(); } } void LoadComment() { try { String[] select = {"ID","message","rate"}; Cursor commentCursor = db.SelectTable("comment", select, "BranchID=" + branchID, ""); commentCursor.moveToFirst(); for(int i=0;i<commentCursor.getCount();i++) { Comment newComment = new Comment(); newComment.id = "" + commentCursor.getInt(0); newComment.message = commentCursor.getString(1); newComment.rate = commentCursor.getString(2); comments.add(newComment); commentCursor.moveToNext(); } String[] commentItems = new String[comments.size()]; for(int i = 0; i< comments.size(); i++) commentItems[i] = comments.get(i).message; listComment.setAdapter(new ArrayAdapter<String>(this, R.layout.rowcomment, R.id.txtCommentMessage, commentItems)); } catch (Exception e) { e.printStackTrace(); } } public void onClick(View v) { switch(v.getId()) { case R.id.BtnAddComment: final View addComment=getLayoutInflater().inflate(R.layout.dialogcomment, null); new AlertDialog.Builder(this) .setTitle("??? ???") .setView(addComment) .setPositiveButton("?????", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { EditText message=(EditText)addComment.findViewById(R.id.txtMessage); RatingBar rb = (RatingBar)addComment.findViewById(R.id.ratingBar); CheckBox chkFavorite = (CheckBox)addComment.findViewById(R.id.checkBoxFavorite); try { InsertComment(message.getText().toString(), chkFavorite.isChecked(), rb.getRating()); LoadRating(); LoadComment(); } catch (Exception e) { e.printStackTrace(); } } } ) .setNegativeButton("???????", null) .show(); break; case R.id.imageButtonMap: Uri uri=Uri.parse("geo:"+lat+","+lon); startActivity(new Intent(Intent.ACTION_VIEW, uri)); break; case R.id.buttonOrder: Intent i = new Intent().setClass(this, FoodList.class); i.putExtra(FoodList.Branch_ID, branchID); startActivity(i); break; } } void InsertComment(String message, boolean favorite, float rate) { try { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); String username = prefs.getString("login", ""); if(!message.equals("") || rate != 0) { String[] fields = {"BranchID","message","rate"}; String[] values = {branchID, message, "" + rate}; db.InsertRow("comment", fields, values); if(Utility.isNetworkAvailable(this)) { HttpClient client=new DefaultHttpClient(); HttpPut putMethod=new HttpPut( "http://" + Server + "/RestaurantReservation/resources/comment/" + (favorite ? "true" : "false") + "/" + rate + "/" + branchID + "/" + username + "/" + message.replace(' ', '-')); client.execute(putMethod); } else { Utility.sendSMS("InsertComment/" + (favorite ? "true" : "false") + "/" + rate + "/" + branchID + "/" + username + "/" + message, this); } } if(favorite) { try { String[] select = {"count(ID)"}; Cursor favoriteCursor = db.SelectTable("Vw_favorite", select, "BranchID=" + branchID + " AND Username='" + username + "'", ""); favoriteCursor.moveToFirst(); if(favoriteCursor.getInt(0) == 0) { String[] fields = {"UserID","BranchID"}; String[] values = {Utility.getCurrentUserID(this),branchID}; db.InsertRow("favorite", fields, values); } } catch(Exception e) { e.printStackTrace(); } } } catch (Exception e) { e.printStackTrace(); } } }