Wednesday 2 November 2011

SubCategoriesActivity.java


package com.OfficeDepot;

import java.util.ArrayList;
import java.util.HashMap;

import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;

import android.app.ProgressDialog;
import android.app.TabActivity;
import android.content.Intent;
import android.content.SharedPreferences;
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.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TabHost;

public class SubCategoriesActivity extends TabActivity {
ListView[] list_category;
String[] CategoryID_sub, Name_sub, MappedParentCategoryID_sub;
final ArrayList<HashMap<String, String>> list_cat1 = new ArrayList<HashMap<String, String>>();
final ArrayList<HashMap<String, String>> list_cat2 = new ArrayList<HashMap<String, String>>();
BaseAdapter adapter_cat1, adapter_cat2, adapter_temp;
int total_data_sub = 0;
String CategoryID_level1, Name_level1, MappedParentCategoryID_level1;
ProgressDialog dialog;
int count = 2;
int position = 0;
static boolean flag_nosubcat = false;
static TabHost tabHost;
static TabHost.TabSpec spec;
SharedPreferences.Editor editor;
public static final String PREFS_COUNT = "MyPrefsFile";

@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.categorylist);
count--;
Bundle extras = getIntent().getExtras();

if (extras != null) {
count = 1;
CategoryID_level1 = extras.getString("CategoryID");
Name_level1 = extras.getString("Name");
MappedParentCategoryID_level1 = extras
.getString("MappedParentCategoryID");
CategoryID_sub = new String[1];
Name_sub = new String[1];
MappedParentCategoryID_sub = new String[1];
CategoryID_sub[0] = CategoryID_level1;
Name_sub[0] = Name_level1;
flag_nosubcat = true;
MappedParentCategoryID_sub[0] = MappedParentCategoryID_level1;

}
list_category = new ListView[2];
list_category[0] = (ListView) findViewById(R.id.list_category1);
list_category[1] = (ListView) findViewById(R.id.list_category2);
WindowManager w = getWindowManager();
Display d = w.getDefaultDisplay();
int width = d.getWidth();
int height = d.getHeight();
LayoutParams l = list_category[0].getLayoutParams();

l.width = width;
if (height == 320) {
l.height = (int) (height * 0.75);
} else {
l.height = (int) (height * 0.80);
}
list_category[0].setLayoutParams(l);
LayoutParams m = list_category[1].getLayoutParams();

m.width = width;
if (height == 320) {
m.height = (int) (height * 0.75);
} else {
m.height = (int) (height * 0.80);
}
list_category[1].setLayoutParams(m);
tabHost = getTabHost();

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

spec = tabHost
.newTabSpec("tab1")
.setIndicator("tab1",
getResources().getDrawable(R.drawable.settings))
.setContent(R.id.view1);

tabHost.addTab(spec);

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(SubCategoriesActivity.this,
MainScreen.class);
SubCategoriesActivity.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(SubCategoriesActivity.this,
LoginActivity.class);
} else {
intent = new Intent(SubCategoriesActivity.this,
LogoutActivity.class);
}
flag.count_login = 2;
flag.flag_subcategories = true;
intent.putExtra("CategoryID", CategoryID_level1);

intent.putExtra("Name", Name_level1);
intent.putExtra("MappedParentCategoryID",
MappedParentCategoryID_level1);
SubCategoriesActivity.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(SubCategoriesActivity.this,
wishlistActivity.class);
} else {
intent = new Intent(SubCategoriesActivity.this,
wishlistdeleteActivity.class);
}
flag.count_wishlist = 2;
flag.flag_subcategories_wish = true;
intent.putExtra("CategoryID", CategoryID_level1);

intent.putExtra("Name", Name_level1);
intent.putExtra("MappedParentCategoryID",
MappedParentCategoryID_level1);
SubCategoriesActivity.this.finish();
startActivity(intent);
}
});
final Button backButton = (Button) this.findViewById(R.id.back);
dialog = ProgressDialog.show(this, "", "Loading Data ...Please Wait",
true);
if (flag.subcatfromwebservice == 1) {

startLongRunningOperation();
} else if (flag.subcatfromwebservice == 0) {
startOperationFromPreferences();
}

list_category[0].setOnItemClickListener(new OnItemClickListener() {

public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Log.e("arg2", "" + arg2);
CategoryID_level1 = CategoryID_sub[arg2];
position = arg2;
startLongRunningOperation();

}

});
backButton.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {

Intent m = new Intent(SubCategoriesActivity.this,
MainCategoryActivity.class);
SubCategoriesActivity.this.finish();
startActivity(m);

}
});
list_category[1].setOnItemClickListener(new OnItemClickListener() {

public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {

CategoryID_level1 = CategoryID_sub[arg2];
position = arg2;
startLongRunningOperation();

}

});

}

final Handler mHandler = new Handler();

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

protected void startOperationFromPreferences() {
Thread preferences = new Thread() {
public void run() {
SharedPreferences myPrefs = getSharedPreferences(PREFS_COUNT, 0);
total_data_sub = myPrefs.getInt("total_data_sub", 0);
CategoryID_sub = new String[total_data_sub];
Name_sub = new String[total_data_sub];
MappedParentCategoryID_sub = new String[total_data_sub];

for (int i = 0; i < total_data_sub; i++) {
CategoryID_sub[i] = myPrefs.getString("CategoryID_sub" + i,
" ");
Name_sub[i] = myPrefs.getString("Name_sub" + i, " ");
MappedParentCategoryID_sub[i] = myPrefs.getString(
"MappedParentCategoryID_sub" + i, " ");

}
// Toast.makeText(getApplicationContext(), "" + total_data,
// 2000).show();

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

protected void startLongRunningOperation() {

Thread getsubcat = new Thread() {
@SuppressWarnings("unused")
public void run() {
flag.subcatfromwebservice = 0;
count++;
// Log.e("count", "" + count);
PropertyInfo p[] = new PropertyInfo[2];
p[0] = new PropertyInfo();

p[0].type = PropertyInfo.STRING_CLASS;
p[0].setName("Parentid");
p[0].setValue(CategoryID_level1);
p[1] = new PropertyInfo();
// Log.e("parent id", CategoryID_level1);
p[1].type = PropertyInfo.STRING_CLASS;
p[1].setName("Storeid");
p[1].setValue(15);

SoapObject Soapresponse = Ksoap
.CallService("GetCategory", 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) {
CategoryID_sub = new String[Table
.getPropertyCount()];
Name_sub = new String[Table
.getPropertyCount()];
MappedParentCategoryID_sub = new String[Table
.getPropertyCount()];
total_data_sub = Table.getPropertyCount();
SharedPreferences myPrefs = getSharedPreferences(
PREFS_COUNT, 0);
editor = myPrefs.edit();
editor.putInt("total_data_sub",
total_data_sub);

for (int i = 0; i < total_data_sub; i++) {
SoapObject row = (SoapObject) Table
.getProperty(i);

try {
flag_nosubcat = false;
CategoryID_sub[i] = row
.getProperty("CategoryID")
.toString();
// Log.e("category", CategoryID[i]);
Name_sub[i] = row.getProperty(
"Name").toString();
// Log.e("Name", Name[i]);
MappedParentCategoryID_sub[i] = row
.getProperty(
"MappedParentCategoryID")
.toString();
// Log.e(" MappedParentCategoryID",
// MappedParentCategoryID[i]);
editor.putString("CategoryID_sub"
+ i, CategoryID_sub[i]);

editor.putString("Name_sub" + i,
Name_sub[i]);
editor.putString(
"MappedParentCategoryID_sub"
+ i,
MappedParentCategoryID_sub[i]);

} catch (NullPointerException e) {

}

}
editor.commit();
}

} else {
Intent m = new Intent(
SubCategoriesActivity.this,
Product.class);

m.putExtra("CategoryID",
CategoryID_sub[position]);
m.putExtra("Name", Name_sub[position]);
m.putExtra("MappedParentCategoryID",
MappedParentCategoryID_sub[position]);
flag.productfromwebservice = 1;
SubCategoriesActivity.this.finish();
startActivity(m);
}
} else {
Intent m = new Intent(SubCategoriesActivity.this,
Product.class);

m.putExtra("CategoryID", CategoryID_sub[position]);
m.putExtra("Name", Name_sub[position]);
m.putExtra("MappedParentCategoryID",
MappedParentCategoryID_sub[position]);
flag.productfromwebservice = 1;
SubCategoriesActivity.this.finish();
startActivity(m);
}
} else {
Intent m = new Intent(SubCategoriesActivity.this,
Product.class);

m.putExtra("CategoryID", CategoryID_sub[position]);
m.putExtra("Name", Name_sub[position]);
m.putExtra("MappedParentCategoryID",
MappedParentCategoryID_sub[position]);
flag.productfromwebservice = 1;
SubCategoriesActivity.this.finish();

startActivity(m);
}

} catch (NullPointerException f) {

} catch (ClassCastException d) {

}

catch (ArrayIndexOutOfBoundsException j) {

Intent m = new Intent(SubCategoriesActivity.this,
Product.class);

m.putExtra("CategoryID", CategoryID_sub[position]);
m.putExtra("Name", Name_sub[position]);
m.putExtra("MappedParentCategoryID",
MappedParentCategoryID_sub[position]);

// if (flag == true) {
SubCategoriesActivity.this.finish();
// }
flag.productfromwebservice = 1;
SubCategoriesActivity.this.finish();
startActivity(m);

}
mHandler.post(mUpdateResults);

}
};
getsubcat.start();
}

final Handler handler = new Handler() {
public void handleMessage(Message msg) {
try {
if ((count % 2) == 0) {

list_cat2.clear();
list_cat1.clear();
list_category[0].setVisibility(View.VISIBLE);
list_category[1].setVisibility(View.INVISIBLE);

populateList1();

adapter_cat1 = new SimpleAdapter(
SubCategoriesActivity.this, list_cat1,
R.layout.custom_list1, new String[] { "text1" },
new int[] { R.id.text1 });

list_category[0].setVisibility(View.GONE);

list_category[0].setVisibility(View.VISIBLE);
list_category[0].setAdapter(adapter_cat1);
if ((dialog.isShowing()))
dialog.dismiss();
adapter_cat1.notifyDataSetChanged();

} else {
list_cat1.clear();
list_cat2.clear();
list_category[1].setVisibility(View.VISIBLE);
list_category[0].setVisibility(View.GONE);
populateList2();

adapter_cat2 = new SimpleAdapter(
SubCategoriesActivity.this, list_cat2,
R.layout.custom_list2, new String[] { "text1" },
new int[] { R.id.text1 });
list_category[1].setAdapter(adapter_cat2);
if ((dialog.isShowing()))
dialog.dismiss();
if (count >= 5)
adapter_cat2.notifyDataSetChanged();

}
} catch (IllegalArgumentException g) {

}
}
};

private void populateList1() {

for (int i = 0; i < total_data_sub; i++) {
HashMap<String, String> temp = new HashMap<String, String>();

if (Name_sub[i].toString().equalsIgnoreCase(null)) {
temp.put("text1", "product");
} else
temp.put("text1", Name_sub[i]);

list_cat1.add(temp);

}

}

private void populateList2() {
for (int i = 0; i < total_data_sub; i++) {
HashMap<String, String> temp = new HashMap<String, String>();
if (Name_sub[i].toString().equalsIgnoreCase(null)) {
temp.put("text1", "product");
} else
temp.put("text1", Name_sub[i]);

list_cat2.add(temp);

}

}

}

No comments:

Post a Comment