Sunday 30 October 2011

Product.java for fetching data from ksoap and arranging dynamically without using xml


package com.OfficeDepot;

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

import android.app.Activity;
import android.app.ProgressDialog;
import android.app.TabActivity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;

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

ProgressDialog dialog;
String CategoryID_product = "product";
String Name_product = "product";
String MappedParentCategoryID_product = "product";
String[] ImageName_product, SalePrice_product;
int total_data_product = 50;
Bitmap[] bmp;
ByteArrayBuffer baf;
int index = 0;
String[] product_Name_product, ProductID_product;
TextView[] price_left, description_left, price_right, description_right;
ImageView[] imageview_left, imageview_right;
static TabHost tabHost;
String CustomerID_product;
SharedPreferences.Editor editor;
public static final String PREFS_COUNT = "MyPrefsFile";
public ImageLoader1 imageLoader;
String[] Image_Path;
private static LayoutInflater inflater = null;

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

requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);

setContentView(R.layout.product3);
WindowManager w = getWindowManager();
Display d = w.getDefaultDisplay();
int width = d.getWidth();
int height = d.getHeight();
Resources res = getResources();
/** Add tabSpec to the TabHost to display. */

Bundle extras = getIntent().getExtras();

if (extras != null) {

CategoryID_product = extras.getString("CategoryID");
Name_product = extras.getString("Name");
MappedParentCategoryID_product = extras
.getString("MappedParentCategoryID");

}
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) {
// do whatever you need
Intent intent = new Intent(Product.this,
MainScreen.class);
Product.this.finish();
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(Product.this,
LoginActivity.class);
} else {
intent = new Intent(Product.this,
LogoutActivity.class);
}
flag.count_login = 3;
flag.flag_product = true;
intent.putExtra("CategoryID", CategoryID_product);
intent.putExtra("MappedParentCategoryID",
MappedParentCategoryID_product);
intent.putExtra("Name", Name_product);
Product.this.finish();
startActivity(intent);
}
});
getTabWidget().getChildAt(3).setOnClickListener(
new View.OnClickListener() {

@Override
public void onClick(View v) {
// do whatever you need
Intent intent;
if ((flag.flag_wishlistdelete == false)
|| (LoginActivity.flag_login == false)
|| (flag.counter_islogin == 0)) {
intent = new Intent(Product.this,
wishlistActivity.class);
} else {
intent = new Intent(Product.this,
wishlistdeleteActivity.class);
}
flag.count_wishlist = 3;
flag.flag_product_wish = true;
intent.putExtra("CategoryID", CategoryID_product);
intent.putExtra("MappedParentCategoryID",
MappedParentCategoryID_product);
intent.putExtra("Name", Name_product);
Product.this.finish();
startActivity(intent);
}
});
final Button backButton = (Button) this.findViewById(R.id.back);
LinearLayout listlayout_left = (LinearLayout) findViewById(R.id.listlayout1);
LinearLayout listlayout_right = (LinearLayout) findViewById(R.id.listlayout2);
listlayout_left.setGravity(Gravity.TOP);
listlayout_right.setGravity(Gravity.TOP);
LinearLayout.LayoutParams layout1 = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,
Gravity.TOP);// only for first two image,not even data
if ((height == 800) || (height == 854)) {
layout1.setMargins(0, 56, 0, 0);
layout1.width = 220;
} else {
layout1.width = 102;
layout1.setMargins(0, 30, 0, 0);

}
LinearLayout.LayoutParams layout1_rest = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,
Gravity.TOP);
if ((height == 800) || (height == 854)) {
// layout1.setMargins(0, 56, 0, 0);
layout1_rest.width = 220;
} else {
layout1_rest.width = 102;
}

layout1_rest.setMargins(0, 0, 0, 0);
LinearLayout.LayoutParams layout1_data = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,
Gravity.TOP);
if ((height == 800) || (height == 854)) {
// layout1.setMargins(0, 56, 0, 0);
layout1_data.width = 220;
} else {
layout1_data.width = 102;
}
layout1_data.setMargins(6, 0, 0, 0);

LinearLayout.LayoutParams layout2 = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,
Gravity.TOP);
if ((height == 800) || (height == 854)) {
// layout1.setMargins(0, 56, 0, 0);
layout2.width = 220;
} else {
layout2.width = 123;
}
layout2.setMargins((width / 2) + 4, 0, 0, 0);
LinearLayout.LayoutParams layout2_data = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,
Gravity.TOP);
if ((height == 800) || (height == 854)) {
// layout1.setMargins(0, 56, 0, 0);
layout2_data.width = 220;
} else {
layout2_data.width = 123;
}
layout2_data.setMargins((width / 2) + 10, 0, 0, 0);

dialog = ProgressDialog.show(this, "", "Loading Data ...Please Wait",
true);
if (flag.productfromwebservice == 1) {
gettotaldata();
startLongRunningOperation();
} else if (flag.productfromwebservice == 0) {
startOperationFromPreferences();
}

price_left = new TextView[total_data_product]; // create an
// empty
// array;
description_left = new TextView[total_data_product];
imageview_left = new ImageView[total_data_product];

price_right = new TextView[total_data_product]; // create an
// empty
// array;
description_right = new TextView[total_data_product];
imageview_right = new ImageView[total_data_product];
try {
imageview_left[0] = new ImageView(this);

imageview_left[0].setScaleType(ImageView.ScaleType.CENTER_INSIDE);
imageview_left[0].setMaxHeight(5);
imageview_left[0].setMaxWidth(5);

imageview_left[0].setId(0);
listlayout_left.addView(imageview_left[0], layout1);

description_left[0] = new TextView(this);

price_left[0] = new TextView(this);
LinearLayout.LayoutParams lefttextview1 = new LinearLayout.LayoutParams(
100, 38);
description_left[0].setLayoutParams(lefttextview1);
price_left[0].setLayoutParams(lefttextview1);
description_left[0].setMaxWidth((int) (width / 2.5));
price_left[0].setMaxWidth((int) (width / 2.5));
description_left[0].setMaxHeight(height / 5);
price_left[0].setMaxHeight(height / 5);

listlayout_left.addView(description_left[0], layout1_data);
listlayout_left.addView(price_left[0], layout1_data);

description_left[0].setId(0 + 20);
price_left[0].setId(0 + 10);
description_left[0].setTextColor(Color.BLACK);
price_left[0].setTextColor(Color.BLACK);
for (int i = 1; i < total_data_product; i++) {

imageview_left[i] = new ImageView(this);

imageview_left[i]
.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
imageview_left[i].setMaxHeight(5);
imageview_left[i].setMaxWidth(5);

imageview_left[i].setId(i);
listlayout_left.addView(imageview_left[i], layout1_rest);

description_left[i] = new TextView(this);

price_left[i] = new TextView(this);
LinearLayout.LayoutParams lefttextview = new LinearLayout.LayoutParams(
100, 38);
description_left[i].setLayoutParams(lefttextview);
price_left[i].setLayoutParams(lefttextview);
description_left[i].setMaxWidth((int) (width / 2.5));
price_left[i].setMaxWidth((int) (width / 2.5));
description_left[i].setMaxHeight(height / 5);
price_left[i].setMaxHeight(height / 5);

listlayout_left.addView(description_left[i], layout1_data);
listlayout_left.addView(price_left[i], layout1_data);

description_left[i].setId(i + 20);
price_left[i].setId(i + 10);
description_left[i].setTextColor(Color.BLACK);
price_left[i].setTextColor(Color.BLACK);

}
for (int i = 0; i < total_data_product; i++) {

imageview_right[i] = new ImageView(this);

imageview_right[i]
.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
imageview_right[i].setMaxHeight(5);
imageview_right[i].setMaxWidth(5);

listlayout_right.addView(imageview_right[i], layout2);
imageview_right[i].setId(i + 100);
description_right[i] = new TextView(this);

price_right[i] = new TextView(this);
LinearLayout.LayoutParams righttextview = new LinearLayout.LayoutParams(
100, 38);
description_right[i].setLayoutParams(righttextview);
price_right[i].setLayoutParams(righttextview);
description_right[i].setMaxHeight(height / 5);
price_right[i].setMaxHeight(height / 5);
listlayout_right.addView(description_right[i], layout2_data);
listlayout_right.addView(price_right[i], layout2_data);
description_right[i].setId(i + 120);
price_right[i].setId(i + 110);
description_right[i].setTextColor(Color.BLACK);
price_right[i].setTextColor(Color.BLACK);

}
} catch (ArrayIndexOutOfBoundsException e) {

}
ScrollView scrollview = (ScrollView) findViewById(R.id.scrollview);

LayoutParams l = scrollview.getLayoutParams();

l.width = width;
if (height == 320) {
l.height = (int) (height * 0.70);
} else {
l.height = (int) (height * 0.75);
}
scrollview.setLayoutParams(l);

backButton.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
if (SubCategoriesActivity.flag_nosubcat == false) {
Intent m = new Intent(Product.this,
SubCategoriesActivity.class);
m.putExtra("CategoryID", MappedParentCategoryID_product);
m.putExtra("Name", Name_product);
Product.this.finish();
startActivity(m);
} else {
Intent m = new Intent(Product.this,
MainCategoryActivity.class);
Product.this.finish();
startActivity(m);

}
}
});
for (int i = 0; i < total_data_product; i++) {
try {
imageview_left[i]
.setOnClickListener(new View.OnClickListener() {

public void onClick(View arg0) {
for (int i = 0; i < total_data_product; i++) {
if ((arg0.getId() == i)) {
Intent m = new Intent(Product.this,
ProductDetailActivity.class);
flag.productdetailfromwebservice = 1;
m.putExtra("ProductID",
ProductID_product[i]);
m.putExtra("CategoryID",
CategoryID_product);
m.putExtra("MappedParentCategoryID",
MappedParentCategoryID_product);
m.putExtra("CustomerID",
CustomerID_product);
Product.this.finish();
startActivity(m);
}
}
}

});
price_left[i].setOnClickListener(new View.OnClickListener() {

public void onClick(View arg0) {
for (int i = 0; i < total_data_product; i++) {
if ((arg0.getId() == (i + 10))) {
Intent m = new Intent(Product.this,
ProductDetailActivity.class);
flag.productdetailfromwebservice = 1;
m.putExtra("ProductID", ProductID_product[i]);
m.putExtra("CategoryID", CategoryID_product);
m.putExtra("MappedParentCategoryID",
MappedParentCategoryID_product);
m.putExtra("CustomerID", CustomerID_product);
Product.this.finish();
startActivity(m);
}
}
}

});
description_left[i]
.setOnClickListener(new View.OnClickListener() {

public void onClick(View arg0) {
for (int i = 0; i < total_data_product; i++) {
if ((arg0.getId() == (i + 20))) {
Intent m = new Intent(Product.this,
ProductDetailActivity.class);
flag.productdetailfromwebservice = 1;
m.putExtra("ProductID",
ProductID_product[i]);
m.putExtra("CategoryID",
CategoryID_product);
m.putExtra("MappedParentCategoryID",
MappedParentCategoryID_product);
m.putExtra("CustomerID",
CustomerID_product);
Product.this.finish();
startActivity(m);
}
}
}

});
} catch (ArrayIndexOutOfBoundsException j) {

}
}
for (int i = 0; i < total_data_product; i++) {
try {
imageview_right[i]
.setOnClickListener(new View.OnClickListener() {

public void onClick(View arg0) {
for (int i = 0; i < total_data_product; i++) {
if ((arg0.getId() == (i + 100))) {
Intent m = new Intent(Product.this,
ProductDetailActivity.class);
flag.productdetailfromwebservice = 1;
m.putExtra("ProductID",
ProductID_product[i]);
m.putExtra("CategoryID",
CategoryID_product);
m.putExtra("MappedParentCategoryID",
MappedParentCategoryID_product);
m.putExtra("CustomerID",
CustomerID_product);
Product.this.finish();
startActivity(m);
}
}
}

});
price_right[i].setOnClickListener(new View.OnClickListener() {

public void onClick(View arg0) {
for (int i = 0; i < total_data_product; i++) {
if ((arg0.getId() == (i + 110))) {
Intent m = new Intent(Product.this,
ProductDetailActivity.class);
flag.productdetailfromwebservice = 1;
m.putExtra("ProductID", ProductID_product[i]);
m.putExtra("CategoryID", CategoryID_product);
m.putExtra("MappedParentCategoryID",
MappedParentCategoryID_product);
m.putExtra("CustomerID", CustomerID_product);
Product.this.finish();
startActivity(m);
}
}
}

});
description_right[i]
.setOnClickListener(new View.OnClickListener() {

public void onClick(View arg0) {
for (int i = 0; i < total_data_product; i++) {
if ((arg0.getId() == (i + 120))) {
Intent m = new Intent(Product.this,
ProductDetailActivity.class);
flag.productdetailfromwebservice = 1;
m.putExtra("ProductID",
ProductID_product[i]);
m.putExtra("CategoryID",
CategoryID_product);
m.putExtra("MappedParentCategoryID",
MappedParentCategoryID_product);
m.putExtra("CustomerID",
CustomerID_product);
Product.this.finish();
startActivity(m);
}
}
}

});

} catch (ArrayIndexOutOfBoundsException j) {

}
}
}

public void gettotaldata() {

PropertyInfo p[] = new PropertyInfo[2];
p[0] = new PropertyInfo();

p[0].type = PropertyInfo.STRING_CLASS;
p[0].setName("CategoryID");
p[0].setValue(CategoryID_product);
p[1] = new PropertyInfo();

p[1].type = PropertyInfo.STRING_CLASS;
p[1].setName("storeID");
p[1].setValue(15);
try {
SoapObject Soapresponse = Ksoap.CallService("GetProductByCatID", 2,
p);
// Log.e("soap response", Soapresponse.toString());
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) {
total_data_product = Table.getPropertyCount();
}
}
}
}
} catch (NullPointerException f) {

} catch (ClassCastException d) {

} catch (ArrayIndexOutOfBoundsException g) {

}
}

final Handler mHandler = new Handler();

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

protected void startOperationFromPreferences() {
Thread preferences = new Thread() {//worker thread or background thread
public void run() {
SharedPreferences myPrefs = getSharedPreferences(PREFS_COUNT, 0);
total_data_product = myPrefs.getInt("total_data_product", 0);
ProductID_product = new String[total_data_product];
product_Name_product = new String[total_data_product];
ImageName_product = new String[total_data_product];
SalePrice_product = new String[total_data_product];

bmp = new Bitmap[total_data_product];
for (int i = 0; i < total_data_product; i++) {
ProductID_product[i] = myPrefs.getString(
"ProductID_product" + i, " ");
product_Name_product[i] = myPrefs.getString(
"product_Name_product" + i, " ");
ImageName_product[i] = myPrefs.getString(
"ImageName_product" + i, " ");
SalePrice_product[i] = myPrefs.getString(
"SalePrice_product" + i, " ");

}
// Toast.makeText(getApplicationContext(), "" + total_data,
// 2000).show();
convert();
mHandler.post(mUpdateResults);//calling UI thread for futher operation
}
// checkUpdate();
};
preferences.start();
}

protected void startLongRunningOperation() {

Thread getproduct = new Thread() {//worker thread or background thread
public void run() {
flag.productfromwebservice = 0;
PropertyInfo p[] = new PropertyInfo[2];
p[0] = new PropertyInfo();

p[0].type = PropertyInfo.STRING_CLASS;
p[0].setName("CategoryID");
p[0].setValue(CategoryID_product);
p[1] = new PropertyInfo();

p[1].type = PropertyInfo.STRING_CLASS;
p[1].setName("storeID");
p[1].setValue(15);

SoapObject Soapresponse = Ksoap.CallService(
"GetProductByCatID", 2, p);
// Log.e("soap response", 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) {
total_data_product = Table
.getPropertyCount();
ProductID_product = new String[Table
.getPropertyCount()];
product_Name_product = new String[Table
.getPropertyCount()];
ImageName_product = new String[Table
.getPropertyCount()];
SalePrice_product = new String[Table
.getPropertyCount()];

bmp = new Bitmap[total_data_product];
SharedPreferences myPrefs = getSharedPreferences(
PREFS_COUNT, 0);
editor = myPrefs.edit();
editor.putInt("total_data_product",
total_data_product);
for (int i = 0; i < total_data_product; i++) {
SoapObject row = (SoapObject) Table
.getProperty(i);
try {

ProductID_product[i] = row
.getProperty("ProductID")
.toString();
// Log.e("ProductID", ProductID[i]);
product_Name_product[i] = row
.getProperty("Name")
.toString();
// Log.e("Name", product_Name[i]);
ImageName_product[i] = row
.getProperty("ImageName")
.toString();
// Log.e(" ImageName",
// ImageName[i]);
SalePrice_product[i] = row
.getProperty("SalePrice")
.toString();
// Log.e(" SalePrice",
// SalePrice[i]);
editor.putString(
"ProductID_product" + i,
ProductID_product[i]);

editor.putString(
"product_Name_product" + i,
product_Name_product[i]);
editor.putString(
"ImageName_product" + i,
ImageName_product[i]);
editor.putString(
"SalePrice_product" + i,
SalePrice_product[i]);
} catch (NullPointerException e) {

}

}
editor.commit();

}
}
}
}
} catch (NullPointerException f) {

} catch (ClassCastException d) {

} catch (ArrayIndexOutOfBoundsException g) {

}
convert();
mHandler.post(mUpdateResults);
}
};
getproduct.start();
}

public void convert() {
try {
Image_Path = new String[total_data_product];
for (int i = 0; i < total_data_product; i++) {
Image_Path[i] = "http://www.officemachinedepot.com/images/letterfolders/product/icon/"
+ ImageName_product[i];
}
// for (int i = 0; i < total_data_product; i++) {
//
// URL updateURL = new URL(
//
// "http://www.officemachinedepot.com/images/letterfolders/product/icon/"
// + ImageName_product[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) {
try {

dialog.dismiss();

Activity activity;
String[] data;

activity = Product.this;
data = Image_Path;
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

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

for (int i = 0; i < total_data_product; i++) {

if ((i % 2) == 0) {
// imageview_left[i].setImageBitmap(bmp[i]);
description_left[i].setText(product_Name_product[i]);
price_left[i].setText("Price : $"
+ SalePrice_product[i]);
imageview_left[i].setTag(data[i]);
imageLoader.DisplayImage(data[i], activity,
imageview_left[i]);

} else if ((i % 2) == 1) {
// imageview_right[i].setImageBitmap(bmp[i]);
description_right[i].setText(product_Name_product[i]);
price_right[i].setText("Price : $"
+ SalePrice_product[i]);
imageview_right[i].setTag(data[i]);
imageLoader.DisplayImage(data[i], activity,
imageview_right[i]);

}

}
} catch (NullPointerException f) {
Toast.makeText(getApplicationContext(), "reload the page", 5000)
.show();
} catch (ArrayIndexOutOfBoundsException g) {
Toast.makeText(getApplicationContext(), "reload the page", 5000)
.show();
}
}
};

}

1 comment:

  1. Hi! lately i've been following your code.
    can u pls post your ProductDetailActivity Class?
    I wanted to know how did you call the variables you passed in this class. thanks so much!

    ReplyDelete