Sunday 30 October 2011

wishlistdeleteActivity.java for displaying data using handler


package com.OfficeDepot;

import java.io.BufferedInputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

import org.apache.http.util.ByteArrayBuffer;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.app.TabActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TabHost;
import android.widget.TextView;

public class wishlistdeleteActivity extends TabActivity {
/** Called when the activity is first created. */

ProgressDialog dialog;

String[] Quantity, Price, ProductName, ImageName;
Bitmap[] bmp;
ByteArrayBuffer baf;
int index = 0;
int total_data;
static TabHost tabHost;

ListView wishlist;
String CategoryID, ProductID, MappedParentCategoryID, Name, result;

String[] ProductID_wishlist;
String ProductID_final;
AlertDialog alertDialog;
int count;
SharedPreferences.Editor editor;
public static final String PREFS_COUNT = "MyPrefsFile";
public ImageLoader1 imageLoader;
private static LayoutInflater inflater = null;
String[] Image_Path;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

alertDialog = new AlertDialog.Builder(wishlistdeleteActivity.this)
.create();
alertDialog.setTitle("");
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);

setContentView(R.layout.deletewishlist);
flag.flag_wishlistdelete = true;

Bundle extras = getIntent().getExtras();
if (extras != null) {
// CategoryID = extras.getString("CategoryID");
// ProductID = extras.getString("ProductID");
// MappedParentCategoryID =
// extras.getString("MappedParentCategoryID");
CategoryID = extras.getString("CategoryID");
ProductID = extras.getString("ProductID");
MappedParentCategoryID = extras.getString("MappedParentCategoryID");
Name = extras.getString("Name");

}
flag.flag_wishlistdelete = true;
wishlist = (ListView) findViewById(R.id.wishlist);
WindowManager w = getWindowManager();
Display d = w.getDefaultDisplay();
int width = d.getWidth();
int height = d.getHeight();
LayoutParams l = wishlist.getLayoutParams();

l.height = (int) (height * 0.75);
wishlist.setLayoutParams(l);

Resources res = getResources();
/** Add tabSpec to the TabHost to display. */

tabHost = getTabHost();

LayoutInflater.from(this).inflate(R.layout.tabs1,
tabHost.getTabContentView(), true);

tabHost.addTab(tabHost
.newTabSpec("tab1")
.setIndicator("tab1",
getResources().getDrawable(R.drawable.settings))
.setContent(R.id.view1));
tabHost.addTab(tabHost
.newTabSpec("tab3")
.setIndicator("tab2",
getResources().getDrawable(R.drawable.mainimage))
.setContent(R.id.view2));
tabHost.addTab(tabHost
.newTabSpec("tab3")
.setIndicator("tab3",
getResources().getDrawable(R.drawable.wishlist))
.setContent(R.id.view3));
tabHost.addTab(tabHost.newTabSpec("tab4").setIndicator("tab4")
.setContent(R.id.view3));
tabHost.getTabWidget().getChildAt(4).setVisibility(4);
getTabWidget().getChildAt(2).setOnClickListener(
new View.OnClickListener() {

@Override
public void onClick(View v) {
//

if (flag.flag_mainscreen_wish == true) {
Intent intent = new Intent(
wishlistdeleteActivity.this,
MainScreen.class);
flag.flag_mainscreen_wish = false;
startActivity(intent);
} else if (flag.flag_maincategory_wish == true) {
Intent intent = new Intent(
wishlistdeleteActivity.this,
MainCategoryActivity.class);

flag.flag_maincategory_wish = false;
startActivity(intent);
} else if (flag.flag_subcategories_wish == true) {
Intent intent = new Intent(
wishlistdeleteActivity.this,
SubCategoriesActivity.class);
flag.flag_subcategories_wish = false;
intent.putExtra("CategoryID", CategoryID);
intent.putExtra("Name", Name);
intent.putExtra("MappedParentCategoryID",
MappedParentCategoryID);
startActivity(intent);

} else if (flag.flag_product_wish == true) {
Intent intent = new Intent(
wishlistdeleteActivity.this, Product.class);
flag.flag_product_wish = false;
intent.putExtra("CategoryID", CategoryID);
intent.putExtra("MappedParentCategoryID",
MappedParentCategoryID);
intent.putExtra("Name", Name);
startActivity(intent);

} else if (flag.flag_productdetail_wish == true) {

Intent intent = new Intent(
wishlistdeleteActivity.this,
ProductDetailActivity.class);
flag.flag_productdetail_wish = false;
intent.putExtra("ProductID", ProductID);
intent.putExtra("CategoryID", CategoryID);
intent.putExtra("MappedParentCategoryID",
MappedParentCategoryID);
startActivity(intent);

} else {
Intent intent = new Intent(
wishlistdeleteActivity.this,
MainScreen.class);
flag.flag_mainscreen = false;
startActivity(intent);
}
}
});
getTabWidget().getChildAt(1).setOnClickListener(
new View.OnClickListener() {

@Override
public void onClick(View v) {
// do whatever you need
Intent intent;
if (flag.counter_islogin == 0) {
intent = new Intent(wishlistdeleteActivity.this,
LoginActivity.class);
} else {
intent = new Intent(wishlistdeleteActivity.this,
LogoutActivity.class);
}
if (flag.count_wishlist == 0) {
flag.flag_mainscreen = true;
flag.flag_mainscreen_wish = false;
} else if (flag.count_wishlist == 1) {
flag.flag_maincategory = true;
flag.flag_maincategory_wish = false;
} else if (flag.count_wishlist == 2) {
flag.flag_subcategories = true;
flag.flag_subcategories_wish = false;
} else if (flag.count_wishlist == 3) {
flag.flag_product = true;
flag.flag_product_wish = false;
} else if (flag.count_wishlist == 4) {
flag.flag_productdetail = true;
flag.flag_productdetail_wish = false;
}
intent.putExtra("CategoryID", CategoryID);

intent.putExtra("Name", Name);
intent.putExtra("MappedParentCategoryID",
MappedParentCategoryID);
intent.putExtra("ProductID", ProductID);
startActivity(intent);
}
});
final Button doneButton = (Button) this.findViewById(R.id.done);
doneButton.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {

Intent m = new Intent(wishlistdeleteActivity.this,
wishlistActivity.class);

m.putExtra("ProductID", ProductID);
m.putExtra("CategoryID", CategoryID);
m.putExtra("MappedParentCategoryID", MappedParentCategoryID);
startActivity(m);

}
});

dialog = ProgressDialog.show(this, "", "Loading Data ...Please Wait",
true);

if (flag.product_add_delete == 1) {

startLongRunningOperation();
} else if (flag.product_add_delete == 0) {
startOperationFromPreferences();
} else if (flag.product_add_delete == 2) {
if ((((Dialog) dialog).isShowing())) {
dialog.dismiss();
flag.flag_dialog = 1;
}
}
}

protected void startOperationFromPreferences() {
Thread preferences = new Thread() {
public void run() {
SharedPreferences myPrefs = getSharedPreferences(PREFS_COUNT, 0);
total_data = myPrefs.getInt("total_data", 0);
count = total_data;
Quantity = new String[total_data];
Price = new String[total_data];
ProductName = new String[total_data];
ImageName = new String[total_data];
ProductID_wishlist = new String[total_data];
bmp = new Bitmap[total_data];
for (int i = 0; i < total_data; i++) {
Quantity[i] = myPrefs.getString("Quantity" + i, " ");
Price[i] = myPrefs.getString("Price" + i, " ");
ImageName[i] = myPrefs.getString("ImageName" + i, " ");
ProductName[i] = myPrefs.getString("ProductName" + i, " ");
ProductID_wishlist[i] = myPrefs.getString(
"ProductID_wishlist" + i, " ");
}
// Toast.makeText(getApplicationContext(), "" + total_data,
// 2000).show();

// convert();
mHandler.post(mUpdateResults);
}
// checkUpdate();
};
preferences.start();
}

final Handler mHandler = new Handler();

// Create runnable for posting
final Runnable mUpdateResults = new Runnable() {
public void run() {
handler.sendEmptyMessage(0);
}
};

// public void checkUpdate() {
protected void startLongRunningOperation() {

Thread display_wishlist = new Thread() {
public void run() {
flag.product_add_delete = 0;

PropertyInfo p[] = new PropertyInfo[2];
p[0] = new PropertyInfo();
p[0].type = PropertyInfo.INTEGER_CLASS;
p[0].setName("CustomerID");
p[0].setValue(LoginActivity.CustomerID);
p[1] = new PropertyInfo();
p[1].type = PropertyInfo.INTEGER_CLASS;
p[1].setName("StoreID");
p[1].setValue(15);
SoapObject Soapresponse = Ksoap.CallService(
"GetWishListByCustomerID", 2, p);

// Log.e("soap response delete wishlist",
// Soapresponse.toString());
try {
if (Soapresponse != null) {
SoapObject Soapresult = (SoapObject) Soapresponse
.getProperty(0);
// Log.e("data", "" + Soapresult.toString());
if (Soapresult != null) {
SoapObject Dataset = (SoapObject) Soapresult
.getProperty(1);
if (Dataset != null) {
SoapObject Table = (SoapObject) Dataset
.getProperty(0);

if (Table != null) {
Quantity = new String[Table
.getPropertyCount()];
Price = new String[Table.getPropertyCount()];
ProductName = new String[Table
.getPropertyCount()];
ImageName = new String[Table
.getPropertyCount()];
ProductID_wishlist = new String[Table
.getPropertyCount()];
total_data = Table.getPropertyCount();
Log.e("total_data delete activity", ""
+ total_data);
bmp = new Bitmap[total_data];
// if (total_data <= 1) {
count = total_data;

// }
SharedPreferences myPrefs = getSharedPreferences(
PREFS_COUNT, 0);
editor = myPrefs.edit();
editor.putInt("total_data", total_data);
for (int i = 0; i < total_data; i++) {
SoapObject row = (SoapObject) Table
.getProperty(i);
try {

Quantity[i] = row.getProperty(
"Quantity").toString();

Price[i] = row.getProperty("Price")
.toString();

ImageName[i] = row.getProperty(
"ImageName").toString();
Log.e(" ImageName", ImageName[i]);
ProductName[i] = row.getProperty(
"ProductName").toString();
ProductID_wishlist[i] = row
.getProperty("ProductID")
.toString();
Log.e("productid",
ProductID_wishlist[i]);
editor.putString("Quantity" + i,
Quantity[i]);

editor.putString("Price" + i,
Price[i]);
editor.putString("ImageName" + i,
ImageName[i]);
editor.putString("ProductName" + i,
ProductName[i]);
editor.putString(
"ProductID_wishlist" + i,
ProductID_wishlist[i]);

} catch (NullPointerException e) {
if (dialog.isShowing()) {
dialog.dismiss();
flag.flag_dialog = 1;
}
}
}
editor.commit();
// convert();
// handler.sendEmptyMessage(0);

}
}
}
}
} catch (NullPointerException f) {
if (dialog.isShowing()) {
dialog.dismiss();
flag.flag_dialog = 1;
}
} catch (ClassCastException d) {
if (dialog.isShowing()) {
dialog.dismiss();
flag.flag_dialog = 1;
}

} catch (ArrayIndexOutOfBoundsException g) {
if (dialog.isShowing()) {
dialog.dismiss();
flag.flag_dialog = 1;
}
}
mHandler.post(mUpdateResults);
}
};
display_wishlist.start();
}

// public void convert() {
// try {
// for (int i = 0; i < total_data; i++) {
//
// URL updateURL = new URL(
//
// "http://www.officemachinedepot.com/images/letterfolders/product/icon/"
// + ImageName[i]);
// URLConnection conn = updateURL.openConnection();
// InputStream is = conn.getInputStream();
// BufferedInputStream bis = new BufferedInputStream(is);
// baf = new ByteArrayBuffer(50);
//
// int current = 0;
// while ((current = bis.read()) != -1) {
// baf.append((byte) current);
// }
// bmp[i] = BitmapFactory.decodeByteArray(baf.toByteArray(), 0,
// baf.length());
// }
// } catch (Exception e) {
// }
// }

final Handler handler = new Handler() {
public void handleMessage(Message msg) {
// wishlist.setAdapter(new ImageAdapter(getApplicationContext()));
Image_Path = new String[total_data];
for (int i = 0; i < total_data; i++) {
Image_Path[i] = "http://www.officemachinedepot.com/images/letterfolders/product/icon/"
+ ImageName[i];
}
wishlist.setAdapter(new ImageAdapter(wishlistdeleteActivity.this,
Image_Path));

try {
Thread.sleep(6 * 1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

if ((dialog.isShowing())) {
dialog.dismiss();
flag.flag_dialog = 1;
}

}
};

private class ImageAdapter extends BaseAdapter {
public final String ImageLoader = null;
private Activity activity;
private String[] data;

public ImageAdapter(Activity a, String[] d) {
activity = a;
data = d;
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

imageLoader = new ImageLoader1(activity.getApplicationContext());
}

public class ViewHolder {
public ImageView iv;
public TextView name;
public TextView price;
public TextView quantity;

}

public ImageAdapter(Context c) {
mContext = c;
}

public int getCount() {
return DATA.length;
}

public Object getItem(int position) {

return position;
}

public long getItemId(int position) {

return position;
}

public View getView(final int position, View convertView,
ViewGroup parent) {
ViewHolder holder;
View v = convertView;

if (convertView == null) {
LayoutInflater li = getLayoutInflater();
v = li.inflate(R.layout.customwishdelete, null, true);
holder = new ViewHolder();
holder.name = (TextView) v.findViewById(R.id.productname);

holder.price = (TextView) v.findViewById(R.id.price);

holder.quantity = (TextView) v.findViewById(R.id.quantity);

holder.iv = (ImageView) v.findViewById(R.id.productimage);

v.setTag(holder);
} else {
holder = (ViewHolder) v.getTag();
}
holder.name.setText(ProductName[position]);
holder.price.setText(Price[position]);
holder.quantity.setText(Quantity[position]);
// holder.iv.setImageBitmap(bmp[position]);
holder.iv.setTag(data[position]);
imageLoader.DisplayImage(data[position], activity, holder.iv);

Button show_delete = (Button) v.findViewById(R.id.show_delete);
final Button delete = (Button) v.findViewById(R.id.delete);
show_delete.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
delete.setVisibility(0);
}
});

delete.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
if (flag.flag_dialog == 1) {
ProductID_final = ProductID_wishlist[position];
// Toast.makeText(getApplicationContext(),ProductID_final
// , 9000).show();
flag.product_add_delete = 1;

wishlist_delete();
}
}
});

return v;
}

private Context mContext;
Integer[] DATA = new Integer[total_data];
{
for (int o = 0; o < total_data; o++) {
DATA[o] = R.drawable.icon;
}
}

}

public void wishlist_delete() {
flag.flag_dialog = 0;
PropertyInfo p[] = new PropertyInfo[3];
p[0] = new PropertyInfo();
p[0].type = PropertyInfo.INTEGER_CLASS;
p[0].setName("CustomerID");
p[0].setValue(LoginActivity.CustomerID);
p[2] = new PropertyInfo();
p[2].type = PropertyInfo.INTEGER_CLASS;
p[2].setName("Storeid");
p[2].setValue(15);

p[1] = new PropertyInfo();
p[1].type = PropertyInfo.INTEGER_CLASS;
p[1].setName("ProductID");
p[1].setValue(ProductID_final);
try {
SoapObject Soapresponse = Ksoap.CallService("DeleteWishlist", 3, p);
// Log.e("ProductID_final", ProductID_final);

if (Soapresponse != null) {
result = Soapresponse.getProperty("DeleteWishlistResult")
.toString();
// if (result.equalsIgnoreCase("Deleted Successfully")) {
dialog = ProgressDialog.show(wishlistdeleteActivity.this, "",
"Loading Data ...Please Wait", true);

// }
// else

// if (count == 1) {
// flag.product_add_delete = 2;
// Intent m = new Intent(wishlistdeleteActivity.this,
// wishlistdeleteActivity.class);
//
// m.putExtra("ProductID", ProductID);
// m.putExtra("CategoryID", CategoryID);
// m.putExtra("MappedParentCategoryID", MappedParentCategoryID);
// startActivity(m);
// }
// if ((((Dialog) dialog).isShowing())) {
// dialog.dismiss();
//
// }
if (result.equalsIgnoreCase("Deleted Successfully")) {
// nothing
} else// if clicked more than once this condition will occur
{
if ((((Dialog) dialog).isShowing())) {
dialog.dismiss();
flag.flag_dialog = 1;
}
}
alertDialog.setMessage("product " + result);
alertDialog.setButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
// if (result
// .equalsIgnoreCase("Deleted Successfully")) {
Log.e("count", "" + count);
if (count <= 1) {
SharedPreferences myPrefs = getSharedPreferences(
PREFS_COUNT, 0);
editor = myPrefs.edit();
editor.putInt("total_data", 0);
for (int i = 0; i < total_data; i++) {

editor.putString("Quantity" + i, null);

editor.putString("Price" + i, null);
editor.putString("ImageName" + i, null);
editor.putString("ProductName" + i,
null);
editor.putString("ProductID_wishlist"
+ i, null);

}
editor.commit();
flag.product_add_delete = 2;
Intent m = new Intent(
wishlistdeleteActivity.this,
wishlistdeleteActivity.class);

m.putExtra("ProductID", ProductID);
m.putExtra("CategoryID", CategoryID);
m.putExtra("MappedParentCategoryID",
MappedParentCategoryID);
startActivity(m);

} else {

startLongRunningOperation();
}
// }
// else
// {
// // if ((((Dialog) dialog).isShowing())) {
// // dialog.dismiss();
// //
// // }
// startLongRunningOperation();
//
// }

}
});

alertDialog.show();

} else {

}

} catch (NullPointerException f) {

} catch (ClassCastException d) {

} catch (ArrayIndexOutOfBoundsException g) {

}
}
}

No comments:

Post a Comment