I am getting error to ArrayIndexOutOFBound...last two i am try to this to complete but it not done by me..guide me ..to solve this problem.in this when i run first tab is OffersActivity is open List when i clicked list item open its Details View (OfferDetailsActivity)and press Back it give error force close..
i am Applied this example :- http://www.jtben.com/document/695851
but it give the error 05-15 10:35:00.755: E/AndroidRuntime(279): FATAL EXCEPTION: main
05-15 10:35:00.755: E/AndroidRuntime(279): java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1
05-15 10:35:00.755: E/AndroidRuntime(279): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:257)
05-15 10:35:00.755: E/AndroidRuntime(279): at java.util.ArrayList.remove(ArrayList.java:406)
05-15 10:35:00.755: E/AndroidRuntime(279): at com.wae.saterra.view.OffersActivity.back(OffersActivity.java:149)
05-15 10:35:00.755: E/AndroidRuntime(279): at com.wae.saterra.view.OffersActivity.onBackPressed(OffersActivity.java:173)
05-15 10:35:00.755: E/AndroidRuntime(279): at android.app.Activity.onKeyUp(Activity.java:1888)
05-15 10:35:00.755: E/AndroidRuntime(279): at android.view.KeyEvent.dispatch(KeyEvent.java:1061)
05-15 10:35:00.755: E/AndroidRuntime(279): at android.app.Activity.dispatchKeyEvent(Activity.java:2068)
05-15 10:35:00.755: E/AndroidRuntime(279): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1643)
05-15 10:35:00.755: E/AndroidRuntime(279): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:788)
05-15 10:35:00.755: E/AndroidRuntime(279): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:788)
05-15 10:35:00.755: E/AndroidRuntime(279): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:788)
05-15 10:35:00.755: E/AndroidRuntime(279): at android.widget.TabHost.dispatchKeyEvent(TabHost.java:275)
05-15 10:35:00.755: E/AndroidRuntime(279): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:788)
05-15 10:35:00.755: E/AndroidRuntime(279): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:788)
05-15 10:35:00.755: E/AndroidRuntime(279): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:788)
05-15 10:35:00.755: E/AndroidRuntime(279): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:788)
05-15 10:35:00.755: E/AndroidRuntime(279): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:1667)
05-15 10:35:00.755: E/AndroidRuntime(279): at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1102)
05-15 10:35:00.755: E/AndroidRuntime(279): at android.app.Activity.dispatchKeyEvent(Activity.java:2063)
05-15 10:35:00.755: E/AndroidRuntime(279): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1643)
05-15 10:35:00.755: E/AndroidRuntime(279): at android.view.ViewRoot.deliverKeyEventToViewHierarchy(ViewRoot.java:2471)
05-15 10:35:00.755: E/AndroidRuntime(279): at android.view.ViewRoot.handleFinishedEvent(ViewRoot.java:2441)
05-15 10:35:00.755: E/AndroidRuntime(279): at android.view.ViewRoot.handleMessage(ViewRoot.java:1735)
05-15 10:35:00.755: E/AndroidRuntime(279): at android.os.Handler.dispatchMessage(Handler.java:99)
05-15 10:35:00.755: E/AndroidRuntime(279): at android.os.Looper.loop(Looper.java:123)
05-15 10:35:00.755: E/AndroidRuntime(279): at android.app.ActivityThread.main(ActivityThread.java:4627)
05-15 10:35:00.755: E/AndroidRuntime(279): at java.lang.reflect.Method.invokeNative(Native Method)
05-15 10:35:00.755: E/AndroidRuntime(279): at java.lang.reflect.Method.invoke(Method.java:521)
05-15 10:35:00.755: E/AndroidRuntime(279): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-15 10:35:00.755: E/AndroidRuntime(279): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-15 10:35:00.755: E/AndroidRuntime(279): at dalvik.system.NativeStart.main(Native Method)
My Code :
TabActivity
'
extends TabActivity { TabHost tabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sattera_tab_activity);
tabHost = getTabHost();
init();
}
private void init() {
Intent intent;
intent=new Intent().setClass(this, OffersActivity.class);
tabHost.addTab(tabHost
.newTabSpec("OffersActivity")
.setIndicator("OffersActivity",
getResources().getDrawable(R.drawable.ic_prominent))
.setContent(intent));
// tabHost.addTab(tabHost.newTabSpec(null)
// .setIndicator(null, getResources().getDrawable(R.drawable.ic_prominent))
// .setContent(new Intent(this,OffersActivity.class)));
tabHost.addTab(tabHost.newTabSpec(null)
.setIndicator(null, getResources().getDrawable(R.drawable.ic_products))
.setContent(new Intent(this,ProductsActivity.class)));
tabHost.addTab(tabHost.newTabSpec(null)
.setIndicator(null, getResources().getDrawable(R.drawable.ic_service_ol))
.setContent(new Intent(this,ServiceActivity.class)));
tabHost.addTab(tabHost.newTabSpec(null)
.setIndicator(null, getResources().getDrawable(R.drawable.ic_tenda_ol))
.setContent(new Intent(this,TendaOLActivity.class)));
tabHost.addTab(tabHost.newTabSpec(null)
.setIndicator(null, getResources().getDrawable(R.drawable.ic_notify))
.setContent(new Intent(this,ContactUsActivity.class)));
//int currentTab = getIntent().getExtras().getInt(OffersActivity.TAB_ID);
tabHost.setCurrentTab(0);
// Intent intent;
// intent=new Intent().setClass(this, OffersActivity.class);
// tabHost.addTab(tabHost
// .newTabSpec(null)
// .setIndicator(null,
// getResources().getDrawable(R.drawable.ic_prominent))
// .setContent(intent));
// intent=new Intent().setClass(this, ProductsActivity.class);
// tabHost.addTab(tabHost
// .newTabSpec(null)
// .setIndicator(null,
// getResources().getDrawable(R.drawable.ic_products))
// .setContent(intent));
// intent=new Intent().setClass(this, ServiceActivity.class);
// tabHost.addTab(tabHost
// .newTabSpec(null)
// .setIndicator(null,
// getResources().getDrawable(R.drawable.ic_service_ol))
// .setContent(intent));
// intent=new Intent().setClass(this, TendaOLActivity.class);
// tabHost.addTab(tabHost
// .newTabSpec(null)
// .setIndicator(null,
// getResources().getDrawable(R.drawable.ic_tenda_ol))
// .setContent(intent));
// intent=new Intent().setClass(this, ContactUsActivity.class);
// tabHost.addTab(tabHost
// .newTabSpec(null)
// .setIndicator(null,
// getResources().getDrawable(R.drawable.ic_notify))
// .setContent(intent));
//
// tabHost.setCurrentTab(2);
//
}
}'
1) OffersActivity
'public class OffersActivity extends ActivityGroup implements
OnItemClickListener {
CustomArrayAdapter adapter;
ListView lvProducts;
ArrayList<Products> productObj;
Products product;
public static OffersActivity group;
private ArrayList<View> history;
// View rootView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.product_list);
this.history = new ArrayList<View>();
group = this;
init();
// hideFB();
// hideTwitter();
setTitle(getResources().getString(R.string.title_offer));
// API Call
if (Global.isNetworkAvailable(OffersActivity.this)) {
new ProductAsyncTask().execute(Constants.url);
} else {
Intent intent = new Intent(OffersActivity.this,
OfflineActivity.class);
startActivity(intent);
// AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
// this);
//
// // set title
// alertDialogBuilder.setTitle("No Internet Connection Available ");
//
// // set dialog message
// alertDialogBuilder
// .setMessage("Click yes to exit!")
// .setCancelable(false)
// .setPositiveButton("Yes",
// new DialogInterface.OnClickListener() {
// public void onClick(DialogInterface dialog,
// int id) {
// Intent intent = new Intent(
// OffersActivity.this,
// OffersActivity.class);
// startActivity(intent);
//
// }
// });
//
// AlertDialog alertDialog = alertDialogBuilder.create();
// // show it
// alertDialog.show();
}
}
@Override
protected void onResume() {
super.onResume();
if (adapter != null) {
adapter.notifyDataSetChanged();
}
}
private void init() {
productObj = new ArrayList<Products>();
lvProducts = (ListView) findViewById(R.id.lv_products);
lvProducts.setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Intent intent = new Intent(OffersActivity.this,
OffersDetailActivity.class);
intent.putExtra("product", (Products) productObj.get(position));
// startActivity(intent);
View view = getLocalActivityManager().startActivity("OffersDetailActivity",
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView();
replaceView(view);
}
public void loadData() {
adapter = new CustomArrayAdapter(OffersActivity.this,
R.layout.product_list_row, productObj);
lvProducts.setAdapter(adapter);
}
protected void back() {
if(history.size() > 0) {
history.remove(history.size());
setContentView(history.get(history.size()));
}else {
finish();
}
}
protected void replaceView(View view) {
// if (history.size() == 0) {
// if (rootView != null) {
// history.add(rootView);
// rootView = null;
// }
// }
// Adds the old one to history
history.add(view);
// Changes this Groups View to the new View.
setContentView(view);
}
@Override
public void onBackPressed() {
OffersActivity.group.back();
return;
}
public class ProductAsyncTask extends
AsyncTask<String, String, ArrayList<Products>> {
String responseString = null;
ProgressDialog progressDialog;
public ProductAsyncTask() {
super();
progressDialog = new ProgressDialog(OffersActivity.this);
progressDialog.setCancelable(false);
}
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog.setMessage("Loading...");
progressDialog.show();
}
@Override
protected ArrayList<Products> doInBackground(String... params) {
responseString = readJSONSFeed(params[0]);
if (responseString != null) {
return processUserListJSON(responseString);
} else {
return null;
}
}
@Override
protected void onPostExecute(ArrayList<Products> result) {
super.onPostExecute(result);
if (result.size() == 0 || result == null) {
}
progressDialog.dismiss();
loadData();
}
private String readJSONSFeed(String url) {
StringBuilder stringBuilder = new StringBuilder();
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url + Constants.REQ_FOR_OFFER);
try {
HttpResponse response = httpClient.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statuscode = statusLine.getStatusCode();
if (statuscode == 200) {
HttpEntity httpEntity = response.getEntity();
InputStream inputStream = httpEntity.getContent();
BufferedReader buffer = new BufferedReader(
new InputStreamReader(inputStream));
String line = null;
if ((line = buffer.readLine()) != null) {
stringBuilder.append(line);
}
// buffer.close();
inputStream.close();
} else {
}
} catch (Exception e) {
}
return stringBuilder.toString();
}
private synchronized ArrayList<Products> processUserListJSON(
String response) {
try {
JSONObject json = new JSONObject(response);
JSONArray Records = json
.getJSONArray(Constants.JSON_OBJECT_DATA);
for (int i = 0; i < Records.length(); i++) {
JSONObject Record = Records.getJSONObject(i);
String tProductID = Record.getString("product_id");
String tTitle = Record.getString("title");
String tImage = Record.getString("image");
String tDetails = Record.getString("short_description");
String tLongDetails = Record.getString("long_description");
String tOffer = Record.getString("offer");
String tActive = Record.getString("active");
String tPromoText = Record.getString("promotion_text");
String tProminent = Record.getString("prominent");
product = new Products(tProductID, tTitle, tDetails,
tLongDetails, tImage, tOffer, tActive, tPromoText,
tProminent);
if (product == null) {
} else {
productObj.add(product);
}
}
} catch (JSONException ex) {
System.out.println(ex);
return null;
}
return productObj;
}
}
}'
OfferDeatilsActivity
'
public class OffersDetailActivity extends Activity implements
OnClickListener, OnItemSelectedListener {
Gallery gallery;
TextView prod_descri, productTitle1, productTitle2;
ImageAdapter imageAdapter;
ImageView leftArrow, rightArrow;
private int selectedImagePosition = 0;
private List drawables;
String title, url, title1;
Bundle b;
Products product;
ProductDetails pDetails;
ArrayList productList;
Button btnFacebook, btnTwitter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.products_details);
init();
setTitle(getResources().getString(R.string.title_offer));
b = getIntent().getExtras();
product = (Products) b.getSerializable("product");
if (b != null) {
if (Global.isNetworkAvailable(OffersDetailActivity.this)) {
new ProductAsyncTask().execute(Constants.url,
product.product_id);
} else {
Intent intent = new Intent(OffersDetailActivity.this,
OfflineActivity.class);
startActivity(intent);
}
} else {
}
}
// @Override // public void onBackPressed() { // OffersActivity.group.back(); // // super.onBackPressed(); // // }
private void init() {
productList = new ArrayList<ProductDetails>();
gallery = (Gallery) findViewById(R.id.product_gallery);
prod_descri = (TextView) findViewById(R.id.product_desc);
productTitle1 = (TextView) findViewById(R.id.productTitle1);
productTitle2 = (TextView) findViewById(R.id.productTitle2);
leftArrow = (ImageView) findViewById(R.id.left_arrow_imageview);
rightArrow = (ImageView) findViewById(R.id.right_arrow_imageview);
btnFacebook = (Button) findViewById(R.id.btnFB);
btnFacebook = (Button) findViewById(R.id.btnTwitter);
//btnFacebook.setOnClickListener(this);
// btnTwitter.setOnClickListener(this);
leftArrow.setOnClickListener(this);
rightArrow.setOnClickListener(this);
gallery.setOnItemSelectedListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.left_arrow_imageview:
if (selectedImagePosition > 0) {
// leftArrow.setVisibility(View.VISIBLE);
--selectedImagePosition;
} else if (selectedImagePosition == 0) {
leftArrow.setVisibility(View.GONE);
rightArrow.setVisibility(View.VISIBLE);
}
gallery.setSelection(selectedImagePosition, true);
break;
case R.id.right_arrow_imageview:
if (selectedImagePosition < drawables.size() - 1) {
// rightArrow.setVisibility(View.VISIBLE);
++selectedImagePosition;
} else if (selectedImagePosition == drawables.size() - 1) {
rightArrow.setVisibility(View.GONE);
leftArrow.setVisibility(View.VISIBLE);
}
gallery.setSelection(selectedImagePosition, true);
break;
default:
break;
}
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
selectedImagePosition = position;
productTitle1.setText(product.title);
productTitle2.setText(productList.get(position).title);
prod_descri.setText(productList.get(position).description);
if (selectedImagePosition > 0
&& selectedImagePosition < drawables.size() - 1) {
leftArrow.setVisibility(View.VISIBLE);
rightArrow.setVisibility(View.VISIBLE);
} else if (selectedImagePosition == 0) {
leftArrow.setVisibility(View.GONE);
} else if (selectedImagePosition == drawables.size() - 1) {
rightArrow.setVisibility(View.GONE);
}
// setSelectedImage(selectedImagePosition);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
// AsyncTask
public class ProductAsyncTask extends
AsyncTask<String, String, ArrayList<ProductDetails>> {
private Context getDialogContext() {
Context context;
if (getParent() != null)
context = getParent();
else
context = OffersDetailActivity.this;
return context;
}
String responseString = null;
ProgressDialog progressDialog;
public ProductAsyncTask() {
super();
// progressDialog = new ProgressDialog(ProductDetailsActivity.this);
progressDialog = new ProgressDialog(getDialogContext());
progressDialog.setCancelable(false);
}
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog.setMessage("Loading...");
progressDialog.show();
}
@Override
protected ArrayList<ProductDetails> doInBackground(String... params) {
responseString = readJSONSFeed(params[0], params[1]);
if (responseString != null) {
return processProductDetailsJSON(responseString);
} else {
return null;
}
}
@Override
protected void onPostExecute(ArrayList<ProductDetails> result) {
super.onPostExecute(result);
if (result.size() == 0 || result == null) {
}
progressDialog.dismiss();
drawables = createDrawables(result);
imageAdapter = new ImageAdapter(OffersDetailActivity.this,
drawables);
if (drawables.size() > 0) {
gallery.setSelection(selectedImagePosition, true);
}
if (drawables.size() == 1) {
leftArrow.setVisibility(View.GONE);
rightArrow.setVisibility(View.GONE);
}
gallery.setAdapter(imageAdapter);
}
private List<Drawable> createDrawables(ArrayList<ProductDetails> result) {
List<Drawable> drawablesUrl = new ArrayList<Drawable>();
try {
for (ProductDetails objProDetails : result) {
drawablesUrl.add(Global
.getBitmapFromURL(objProDetails.image_path));
// drawablesUrl.add(loadImageFromURL(objProDetails.image_path));
}
return drawablesUrl;
} catch (Exception e) {
return null;
}
}
@SuppressWarnings("unused")
private Drawable loadImageFromURL(String image_path) {
try {
InputStream is = (InputStream) new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, "src name");
return d;
} catch (Exception e) {
return null;
}
}
private String readJSONSFeed(String url, String productID) {
StringBuilder stringBuilder = new StringBuilder();
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url + Constants.REQ_FOR_PRODUCT_IMAGE
+ productID + Constants.REQ_FOR_OFFER);
try {
HttpResponse response = httpClient.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statuscode = statusLine.getStatusCode();
if (statuscode == 200) {
HttpEntity httpEntity = response.getEntity();
InputStream inputStream = httpEntity.getContent();
BufferedReader buffer = new BufferedReader(
new InputStreamReader(inputStream));
String line = null;
if ((line = buffer.readLine()) != null) {
stringBuilder.append(line);
}
// buffer.close();
inputStream.close();
} else {
}
} catch (Exception e) {
}
return stringBuilder.toString();
}
private synchronized ArrayList<ProductDetails> processProductDetailsJSON(
String response) {
try {
JSONObject json = new JSONObject(response);
JSONArray Records = json
.getJSONArray(Constants.JSON_OBJECT_DATA);
for (int i = 0; i < Records.length(); i++) {
JSONObject Record = Records.getJSONObject(i);
String tTitle = Record.getString("title");
String tImagePath = Record.getString("image_path");
String tDescription = Record.getString("description");
pDetails = new ProductDetails(tDescription, tTitle,
tImagePath);
if (pDetails == null) {
Log.v("SATERRA", "product object null");
} else {
productList.add(pDetails);
Log.v("SATERRA", "product details title ="
+ productList.get(i).title);
}
}
} catch (JSONException ex) {
System.out.println(ex);
return null;
}
return productList;
}
}
}'
- this solve ...use this Code..
1)OffersActivityGroup add one Activity to add your root activity...
public class OffersActivityGroup extends ActivityGroup {
public static OffersActivityGroup group;
private ArrayList<View> history;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.history = new ArrayList<View>();
group = this;
// Start the root activity withing the group and get its view
View view = getLocalActivityManager().startActivity(
"OffersActivity",
new Intent(this, OffersActivity.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
.getDecorView();
// Replace the view of this ActivityGroup
replaceView(view);
}
public void replaceView(View v) {
// Adds the old one to history
history.add(v);
// Changes this Groups View to the new View.
setContentView(v);
}
public void back() {
if (history.size() > 0) {
history.remove(history.size() - 1);
setContentView(history.get(history.size() - 1));
} else {
finish();
}
}
@Override
public void onBackPressed() {
OffersActivityGroup.group.back();
return;
}
}
2)OffersActivity
public class OffersActivity extends Activity implements OnItemClickListener {
CustomArrayAdapter adapter;
ListView lvProducts;
ArrayList<Products> productObj;
Products product;
// View rootView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.product_list);
init();
// hideFB();
// hideTwitter();
setTitle(getResources().getString(R.string.title_offer));
// API Call
if (Global.isNetworkAvailable(OffersActivity.this)) {
new ProductAsyncTask().execute(Constants.url);
} else {
Intent intent = new Intent(OffersActivity.this,
OfflineActivity.class);
startActivity(intent);
}
}
@Override
protected void onResume() {
super.onResume();
if (adapter != null) {
adapter.notifyDataSetChanged();
}
}
private void init() {
productObj = new ArrayList<Products>();
lvProducts = (ListView) findViewById(R.id.lv_products);
lvProducts.setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Intent intent = new Intent(OffersActivity.this,
OffersDetailActivity.class);
intent.putExtra("product", (Products) productObj.get(position));
View view = OffersActivityGroup.group
.getLocalActivityManager()
.startActivity("OffersDetailActivity",
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
.getDecorView();
OffersActivityGroup.group.replaceView(view);
}
public void loadData() {
adapter = new CustomArrayAdapter(OffersActivity.this,
R.layout.product_list_row, productObj);
lvProducts.setAdapter(adapter);
}
// @Override
// public void onBackPressed() {
// OffersActivity.group.back();
// return;
// }
public class ProductAsyncTask extends
AsyncTask<String, String, ArrayList<Products>> {
String responseString = null;
ProgressDialog progressDialog;
public ProductAsyncTask() {
super();
progressDialog = new ProgressDialog(getParent());
progressDialog.setCancelable(false);
}
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog.setMessage("Loading...");
progressDialog.show();
}
@Override
protected ArrayList<Products> doInBackground(String... params) {
responseString = readJSONSFeed(params[0]);
if (responseString != null) {
return processUserListJSON(responseString);
} else {
return null;
}
}
@Override
protected void onPostExecute(ArrayList<Products> result) {
super.onPostExecute(result);
if (result.size() == 0 || result == null) {
}
progressDialog.dismiss();
loadData();
}
private String readJSONSFeed(String url) {
StringBuilder stringBuilder = new StringBuilder();
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url + Constants.REQ_FOR_OFFER);
try {
HttpResponse response = httpClient.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statuscode = statusLine.getStatusCode();
if (statuscode == 200) {
HttpEntity httpEntity = response.getEntity();
InputStream inputStream = httpEntity.getContent();
BufferedReader buffer = new BufferedReader(
new InputStreamReader(inputStream));
String line = null;
if ((line = buffer.readLine()) != null) {
stringBuilder.append(line);
}
// buffer.close();
inputStream.close();
} else {
}
} catch (Exception e) {
}
return stringBuilder.toString();
}
private synchronized ArrayList<Products> processUserListJSON(
String response) {
try {
JSONObject json = new JSONObject(response);
JSONArray Records = json
.getJSONArray(Constants.JSON_OBJECT_DATA);
for (int i = 0; i < Records.length(); i++) {
JSONObject Record = Records.getJSONObject(i);
String tProductID = Record.getString("product_id");
String tTitle = Record.getString("title");
String tImage = Record.getString("image");
String tDetails = Record.getString("short_description");
String tLongDetails = Record.getString("long_description");
String tOffer = Record.getString("offer");
String tActive = Record.getString("active");
String tPromoText = Record.getString("promotion_text");
String tProminent = Record.getString("prominent");
product = new Products(tProductID, tTitle, tDetails,
tLongDetails, tImage, tOffer, tActive, tPromoText,
tProminent);
if (product == null) {
} else {
productObj.add(product);
}
}
} catch (JSONException ex) {
System.out.println(ex);
return null;
}
return productObj;
}
}
}
2)OfferDetailsActivity
public class OffersDetailActivity extends Activity implements
OnClickListener, OnItemSelectedListener {
Gallery gallery;
TextView prod_descri, productTitle1, productTitle2;
ImageAdapter imageAdapter;
ImageView leftArrow, rightArrow;
private int selectedImagePosition = 0;
private List<Drawable> drawables;
String title, url, title1;
Bundle b;
Products product;
ProductDetails pDetails;
ArrayList<ProductDetails> productList;
Button btnFacebook, btnTwitter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.products_details);
init();
setTitle(getResources().getString(R.string.title_offer));
b = getIntent().getExtras();
product = (Products) b.getSerializable("product");
if (b != null) {
if (Global.isNetworkAvailable(OffersDetailActivity.this)) {
new ProductAsyncTask().execute(Constants.url,
product.product_id);
} else {
Intent intent = new Intent(OffersDetailActivity.this,
OfflineActivity.class);
startActivity(intent);
}
} else {
}
}
// @Override
// public void onBackPressed() {
// OffersActivity.group.back();
// // super.onBackPressed();
//
// }
private void init() {
productList = new ArrayList<ProductDetails>();
gallery = (Gallery) findViewById(R.id.product_gallery);
prod_descri = (TextView) findViewById(R.id.product_desc);
productTitle1 = (TextView) findViewById(R.id.productTitle1);
productTitle2 = (TextView) findViewById(R.id.productTitle2);
leftArrow = (ImageView) findViewById(R.id.left_arrow_imageview);
rightArrow = (ImageView) findViewById(R.id.right_arrow_imageview);
btnFacebook = (Button) findViewById(R.id.btnFB);
btnFacebook = (Button) findViewById(R.id.btnTwitter);
//btnFacebook.setOnClickListener(this);
// btnTwitter.setOnClickListener(this);
leftArrow.setOnClickListener(this);
rightArrow.setOnClickListener(this);
gallery.setOnItemSelectedListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.left_arrow_imageview:
if (selectedImagePosition > 0) {
// leftArrow.setVisibility(View.VISIBLE);
--selectedImagePosition;
} else if (selectedImagePosition == 0) {
leftArrow.setVisibility(View.GONE);
rightArrow.setVisibility(View.VISIBLE);
}
gallery.setSelection(selectedImagePosition, true);
break;
case R.id.right_arrow_imageview:
if (selectedImagePosition < drawables.size() - 1) {
// rightArrow.setVisibility(View.VISIBLE);
++selectedImagePosition;
} else if (selectedImagePosition == drawables.size() - 1) {
rightArrow.setVisibility(View.GONE);
leftArrow.setVisibility(View.VISIBLE);
}
gallery.setSelection(selectedImagePosition, true);
break;
default:
break;
}
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
selectedImagePosition = position;
productTitle1.setText(product.title);
productTitle2.setText(productList.get(position).title);
prod_descri.setText(productList.get(position).description);
if (selectedImagePosition > 0
&& selectedImagePosition < drawables.size() - 1) {
leftArrow.setVisibility(View.VISIBLE);
rightArrow.setVisibility(View.VISIBLE);
} else if (selectedImagePosition == 0) {
leftArrow.setVisibility(View.GONE);
} else if (selectedImagePosition == drawables.size() - 1) {
rightArrow.setVisibility(View.GONE);
}
// setSelectedImage(selectedImagePosition);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
// AsyncTask
public class ProductAsyncTask extends
AsyncTask<String, String, ArrayList<ProductDetails>> {
private Context getDialogContext() {
Context context;
if (getParent() != null)
context = getParent();
else
context = OffersDetailActivity.this;
return context;
}
String responseString = null;
ProgressDialog progressDialog;
public ProductAsyncTask() {
super();
// progressDialog = new ProgressDialog(ProductDetailsActivity.this);
progressDialog = new ProgressDialog(getDialogContext());
progressDialog.setCancelable(false);
}
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog.setMessage("Loading...");
progressDialog.show();
}
@Override
protected ArrayList<ProductDetails> doInBackground(String... params) {
responseString = readJSONSFeed(params[0], params[1]);
if (responseString != null) {
return processProductDetailsJSON(responseString);
} else {
return null;
}
}
@Override
protected void onPostExecute(ArrayList<ProductDetails> result) {
super.onPostExecute(result);
if (result.size() == 0 || result == null) {
}
progressDialog.dismiss();
drawables = createDrawables(result);
imageAdapter = new ImageAdapter(OffersDetailActivity.this,
drawables);
if (drawables.size() > 0) {
gallery.setSelection(selectedImagePosition, true);
}
if (drawables.size() == 1) {
leftArrow.setVisibility(View.GONE);
rightArrow.setVisibility(View.GONE);
}
gallery.setAdapter(imageAdapter);
}
private List<Drawable> createDrawables(ArrayList<ProductDetails> result) {
List<Drawable> drawablesUrl = new ArrayList<Drawable>();
try {
for (ProductDetails objProDetails : result) {
drawablesUrl.add(Global
.getBitmapFromURL(objProDetails.image_path));
// drawablesUrl.add(loadImageFromURL(objProDetails.image_path));
}
return drawablesUrl;
} catch (Exception e) {
return null;
}
}
@SuppressWarnings("unused")
private Drawable loadImageFromURL(String image_path) {
try {
InputStream is = (InputStream) new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, "src name");
return d;
} catch (Exception e) {
return null;
}
}
private String readJSONSFeed(String url, String productID) {
StringBuilder stringBuilder = new StringBuilder();
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url + Constants.REQ_FOR_PRODUCT_IMAGE
+ productID + Constants.REQ_FOR_OFFER);
try {
HttpResponse response = httpClient.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statuscode = statusLine.getStatusCode();
if (statuscode == 200) {
HttpEntity httpEntity = response.getEntity();
InputStream inputStream = httpEntity.getContent();
BufferedReader buffer = new BufferedReader(
new InputStreamReader(inputStream));
String line = null;
if ((line = buffer.readLine()) != null) {
stringBuilder.append(line);
}
// buffer.close();
inputStream.close();
} else {
}
} catch (Exception e) {
}
return stringBuilder.toString();
}
private synchronized ArrayList<ProductDetails> processProductDetailsJSON(
String response) {
try {
JSONObject json = new JSONObject(response);
JSONArray Records = json
.getJSONArray(Constants.JSON_OBJECT_DATA);
for (int i = 0; i < Records.length(); i++) {
JSONObject Record = Records.getJSONObject(i);
String tTitle = Record.getString("title");
String tImagePath = Record.getString("image_path");
String tDescription = Record.getString("description");
pDetails = new ProductDetails(tDescription, tTitle,
tImagePath);
if (pDetails == null) {
} else {
productList.add(pDetails);
}
}
} catch (JSONException ex) {
System.out.println(ex);
return null;
}
return productList;
}
}
}