Java tutorial
/* * Copyright 2013 Niek Haarman * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.laquysoft.bycon; import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; import android.app.Dialog; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.AsyncTask; import android.os.Bundle; import android.support.v4.util.LruCache; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AbsListView; import android.widget.Button; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; import com.nhaarman.listviewanimations.ArrayAdapter; import com.nhaarman.listviewanimations.itemmanipulation.OnDismissCallback; import com.nhaarman.listviewanimations.itemmanipulation.swipedismiss.SwipeDismissAdapter; import com.nhaarman.listviewanimations.swinginadapters.prepared.SwingBottomInAnimationAdapter; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import org.json.JSONArray; public class GoogleCardsActivity extends BaseActivity implements OnDismissCallback { private GoogleCardsAdapter mGoogleCardsAdapter; @Override protected void onCreate(final Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_googlecards); ListView listView = (ListView) findViewById(R.id.activity_googlecards_listview); mGoogleCardsAdapter = new GoogleCardsAdapter(this); SwingBottomInAnimationAdapter swingBottomInAnimationAdapter = new SwingBottomInAnimationAdapter( new SwipeDismissAdapter(mGoogleCardsAdapter, this)); swingBottomInAnimationAdapter.setInitialDelayMillis(300); swingBottomInAnimationAdapter.setAbsListView(listView); listView.setAdapter(swingBottomInAnimationAdapter); mGoogleCardsAdapter.addAll(getItems()); } private ArrayList<Integer> getItems() { ArrayList<Integer> items = new ArrayList<Integer>(); for (int i = 0; i < 5; i++) { items.add(i); } return items; } @Override public void onDismiss(final AbsListView listView, final int[] reverseSortedPositions) { for (int position : reverseSortedPositions) { mGoogleCardsAdapter.remove(position); } } private static class GoogleCardsAdapter extends ArrayAdapter<Integer> { private final Context mContext; private final LruCache<Integer, Bitmap> mMemoryCache; public GoogleCardsAdapter(final Context context) { mContext = context; final int cacheSize = (int) (Runtime.getRuntime().maxMemory() / 1024); mMemoryCache = new LruCache<Integer, Bitmap>(cacheSize) { @Override protected int sizeOf(final Integer key, final Bitmap bitmap) { // The cache size will be measured in kilobytes rather than // number of items. return bitmap.getRowBytes() * bitmap.getHeight() / 1024; } }; } @Override public View getView(final int position, final View convertView, final ViewGroup parent) { ViewHolder viewHolder; View view = convertView; if (view == null) { view = LayoutInflater.from(mContext).inflate(R.layout.activity_googlecards_card, parent, false); viewHolder = new ViewHolder(); viewHolder.textView = (TextView) view.findViewById(R.id.activity_googlecards_card_textview); view.setTag(viewHolder); viewHolder.imageView = (ImageView) view.findViewById(R.id.activity_googlecards_card_imageview); } else { viewHolder = (ViewHolder) view.getTag(); } setImageView(viewHolder, position); return view; } private void setImageView(final ViewHolder viewHolder, final int position) { int imageResId; switch (getItem(position) % 5) { case 0: imageResId = R.drawable.pizza; viewHolder.textView.setText("Pizza"); break; case 1: imageResId = R.drawable.gamberi; viewHolder.textView.setText("Tempura"); break; case 3: imageResId = R.drawable.arancini; viewHolder.textView.setText("Arancini"); break; default: imageResId = R.drawable.birra; viewHolder.textView.setText("Birra"); } Bitmap bitmap = getBitmapFromMemCache(imageResId); if (bitmap == null) { bitmap = BitmapFactory.decodeResource(mContext.getResources(), imageResId); addBitmapToMemoryCache(imageResId, bitmap); } viewHolder.imageView.setImageBitmap(bitmap); } private void addBitmapToMemoryCache(final int key, final Bitmap bitmap) { if (getBitmapFromMemCache(key) == null) { mMemoryCache.put(key, bitmap); } } private Bitmap getBitmapFromMemCache(final int key) { return mMemoryCache.get(key); } private static class ViewHolder { TextView textView; ImageView imageView; } } public void postOrder(View w) { Log.i("ListActivity", "Post Order"); new OrderTask().execute(); // custom dialog final Dialog dialog = new Dialog(this); dialog.setContentView(R.layout.payment_dialog); dialog.setTitle("Place & Pay Order"); // set the custom dialog components - text, image and button ImageView image = (ImageView) dialog.findViewById(R.id.image); image.setImageResource(R.drawable.ic_launcher); Button dialogButton = (Button) dialog.findViewById(R.id.btn_accept); // if button is clicked, close the custom dialog dialogButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { dialog.dismiss(); } }); Button dialogCancelButton = (Button) dialog.findViewById(R.id.btn_reject); // if button is clicked, close the custom dialog dialogCancelButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { dialog.dismiss(); } }); dialog.show(); } public class OrderTask extends AsyncTask<Void, Void, String> { @Override protected String doInBackground(Void... params) { try { HttpClient httpclient = new DefaultHttpClient(); HttpGet httpget = new HttpGet("http://192.168.1.60:8000/order/85"); // Add your data /* List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); nameValuePairs.add(new BasicNameValuePair("id", "12345")); nameValuePairs.add(new BasicNameValuePair("stringdata", "AndDev is Cool!")); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); */ // Execute HTTP Post Request HttpResponse response = httpclient.execute(httpget); BufferedReader reader = new BufferedReader( new InputStreamReader(response.getEntity().getContent(), "iso-8859-1"), 8); StringBuilder sb = new StringBuilder(); sb.append(reader.readLine() + "\n"); String line = "0"; while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } reader.close(); String result11 = sb.toString(); // parsing data return result11; } catch (Exception e) { e.printStackTrace(); return null; } } @Override protected void onPostExecute(String result) { if (result != null) { Log.i("ListActivity", "Post Order return " + result); } else { // error occured } } } }