Wednesday 2 November 2011

MainCategoryActivity.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.graphics.Bitmap;
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.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TabHost;

public class MainCategoryActivity extends TabActivity {
String[] CategoryID_main, Name_main, MappedParentCategoryID_main;
ProgressDialog dialog;
int total_data_main;
SoapObject Soapresult;
SoapObject Soapresponse;
ListView list_category;
SimpleAdapter adapter_cat;
final ArrayList<HashMap<String, String>> list_cat = new ArrayList<HashMap<String, String>>();
static TabHost tabHost;
SharedPreferences.Editor editor;
public static final String PREFS_COUNT = "MyPrefsFile";

/** Called when the activity is first created. */
@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.maincategory);

final Button backButton = (Button) this.findViewById(R.id.back);
backButton.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {

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

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

l.width = width;
if (height == 320) {
l.height = (int) (height * 0.75);
} else {
l.height = (int) (height * 0.80);
}
listview.setLayoutParams(l);
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(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(MainCategoryActivity.this,
LoginActivity.class);
} else {
intent = new Intent(MainCategoryActivity.this,
LogoutActivity.class);
}
flag.flag_maincategory = true;
flag.count_login = 1;
MainCategoryActivity.this.finish();
startActivity(intent);
}
});
getTabWidget().getChildAt(2).setOnClickListener(
new View.OnClickListener() {

@Override
public void onClick(View v) {
// do whatever you need
Intent intent = new Intent(MainCategoryActivity.this,
MainScreen.class);
MainCategoryActivity.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(MainCategoryActivity.this,
wishlistActivity.class);
} else {
intent = new Intent(MainCategoryActivity.this,
wishlistdeleteActivity.class);
}
flag.count_wishlist = 1;
flag.flag_maincategory_wish = true;
MainCategoryActivity.this.finish();
startActivity(intent);
}
});
list_category = (ListView) findViewById(R.id.list_category);
dialog = ProgressDialog.show(this, "", "Loading Data ...Please Wait",
true);
if (flag.maincatfromwebservice == 1) {

startLongRunningOperation();
} else if (flag.maincatfromwebservice == 0) {
startOperationFromPreferences();
}
list_category.setOnItemClickListener(new OnItemClickListener() {

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

Intent m = new Intent(MainCategoryActivity.this,
SubCategoriesActivity.class);
flag.subcatfromwebservice = 1;
m.putExtra("CategoryID", CategoryID_main[arg2]);
m.putExtra("Name", Name_main[arg2]);
m.putExtra("MappedParentCategoryID",
MappedParentCategoryID_main[arg2]);
MainCategoryActivity.this.finish();
startActivity(m);

}

});

}

private void populateList() {
for (int i = 0; i < total_data_main; i++) {
HashMap<String, String> temp = new HashMap<String, String>();

temp.put("text1", Name_main[i]);

list_cat.add(temp);

}

}

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_main = myPrefs.getInt("total_data_main", 0);
CategoryID_main = new String[total_data_main];
Name_main = new String[total_data_main];
MappedParentCategoryID_main = new String[total_data_main];

for (int i = 0; i < total_data_main; i++) {
CategoryID_main[i] = myPrefs.getString("CategoryID_main" + i, " ");
Name_main[i] = myPrefs.getString("Name_main" + i, " ");
MappedParentCategoryID_main[i] = myPrefs.getString(
"MappedParentCategoryID_main" + i, " ");

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

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

protected void startLongRunningOperation() {

Thread getmaincat = new Thread() {
public void run() {
flag.maincatfromwebservice = 0;
PropertyInfo p[] = new PropertyInfo[2];
p[0] = new PropertyInfo();

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

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

Soapresponse = Ksoap.CallService("GetCategory", 2, p);

try {
if (Soapresponse != null) {
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_main = new String[Table
.getPropertyCount()];
Name_main = new String[Table.getPropertyCount()];
MappedParentCategoryID_main = new String[Table
.getPropertyCount()];
total_data_main = Table.getPropertyCount();
SharedPreferences myPrefs = getSharedPreferences(
PREFS_COUNT, 0);
editor = myPrefs.edit();
editor.putInt("total_data_main",
total_data_main);
for (int i = 0; i < total_data_main; i++) {
SoapObject row = (SoapObject) Table
.getProperty(i);
try {
CategoryID_main[i] = row.getProperty(
"CategoryID").toString();
Log.e("category", CategoryID_main[i]);
Name_main[i] = row.getProperty("Name")
.toString();
Log.e("Name", Name_main[i]);
MappedParentCategoryID_main[i] = row
.getProperty(
"MappedParentCategoryID")
.toString();
Log.e(" MappedParentCategoryID",
MappedParentCategoryID_main[i]);
editor.putString("CategoryID_main" + i,
CategoryID_main[i]);

editor.putString("Name_main" + i,
Name_main[i]);
editor.putString(
"MappedParentCategoryID_main"
+ i,
MappedParentCategoryID_main[i]);

} catch (NullPointerException e) {

}

}
editor.commit();

}
}
}
}
} catch (NullPointerException f) {

} catch (ClassCastException d) {

}
mHandler.post(mUpdateResults);
}
};
getmaincat.start();
}

// division data handler

final Handler handler = new Handler() {
public void handleMessage(Message msg) {
try {
populateList();

adapter_cat = new SimpleAdapter(MainCategoryActivity.this,
list_cat, R.layout.custom_maincat,
new String[] { "text1" }, new int[] { R.id.text1 });
list_category.setAdapter(adapter_cat);

dialog.dismiss();

} catch (NullPointerException f) {

}
}
};
}

No comments:

Post a Comment