I have a problem that I am showing images in grid view which is parsed from Url, I have designed a refine search dialog to refine the search in grid view but the problem is that parsing works fine but Grid view does not refreshed next time when I processed Refine Search. Means Grid View items does not refreshed it shows previous result. Please help me out for this Problem.
Code:
package org.shopzilla.android.search;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.shopzilla.android.common.R;
import org.shopzilla.android.parsing.SearchResult_Json;
import org.shopzilla.android.parsing.Search_Refine_Json;
import org.shopzilla.android.parsing.Search_refine_grid;
import org.shopzilla.android.product.ProductComparisonActivity;
import com.flurry.android.FlurryAgent;
import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
public class SearchGridActivity extends Activity{
URL url1;
public static Bitmap[] bmp;
Bundle bundle;
GridView grid;
String prod_id;
public static String str,str1;
public static Boolean flag_grid=false;
public static int count =0;
ImageButton _gridImageButton, brand_options_btn,color_options_btn,mat_options_btn;
Button categories_btn;
public static String keyword;
ArrayList<HashMap<String,Object>> list ;
ListView listView,brandlist, colorlist;
static final int DIALOG_PRODUCE_ID = 0,DIALOG_REFINE_ID=1;
Dialog dialog_category,dialog_refine;
public static ArrayList<String> brand_list_load = new ArrayList<String>();
public static ArrayList<String> store_list_load = new ArrayList<String>();
public static ArrayList<String> brand_count = new ArrayList<String>();
public static ArrayList<String> brand_id = new ArrayList<String>();
public static ArrayList<String> brand_array_list = new ArrayList<String>();
public static ArrayList<String> store_array_list = new ArrayList<String>();
public static ArrayList<String> store_count = new ArrayList<String>();
public static ArrayList<String> store_id = new ArrayList<String>();
public static ArrayList<String> brand_selected = new ArrayList<String>();
public static ArrayList<HashMap<String,Object>> arr_list = new ArrayList<HashMap<String,Object>>();
String final_keyword;
public static Boolean falg=false;
String store_name;
String brand_name;
public String temp;
public static Boolean checkflag=true;
EditText et_search;
ArrayList<String> store_selected = new ArrayList<String>();
ArrayAdapter<String> slected_brand;
ArrayAdapter<String> slected_store;
ListView brand_list,store_list;
ArrayAdapter<String> arrayAdapter,storeAdapter, store_new_adapter, brand_new_adapter;
SimpleAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.searchgrid);
bundle = getIntent().getExtras();
if(count==0)
{
keyword = bundle.getString("keyword");
final_keyword = keyword;
}
//str1 = bundle.getString("temp");
grid = (GridView)findViewById(R.id.Search_Result_Grid);
grid.invalidateViews();
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
final ProgressDialog dialog = ProgressDialog.show(this, "BIZRATE", "Loading.......", true);
final Handler handler = new Handler() {
public void handleMessage(Message msg) {
callHereForEff();
dialog.dismiss();
}
};
Thread checkUpdate = new Thread() {
public void run() {
if(count==1)
{
SearchResult_Json.json_parsing(final_keyword);
}
//SearchResultActivity.set_image();
Search_refine_grid.Json_parsing();
handler.sendEmptyMessage(0);
}
};
checkUpdate.start();
EditText et_search_grid = (EditText)findViewById(R.id.txt_search_grid);
et_search_grid.setText(keyword);
et_search_grid.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(SearchGridActivity.this,SearchTabActivity.class);
startActivity(intent);
}
});
Button btn_list = (Button)findViewById(R.id.btn_searchresult_list);
Button btn_refine = (Button)findViewById(R.id.btn_RefineSearch);
btn_refine.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
arrayAdapter =
new ArrayAdapter<String>(SearchGridActivity.this,R.layout.ref_row, R.id.label, brand_list_load);
storeAdapter =
new ArrayAdapter<String>(SearchGridActivity.this,R.layout.ref_row, R.id.label, store_list_load);
showDialog(0);
}
});
btn_list.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(SearchGridActivity.this,SearchResultActivity.class);
intent.putExtra("keyword", keyword);
startActivity(intent);
finish();
}
});
/*for(int j=0;j<SearchResultActivity.image_me.size();j++)
{
ImageView iv = new ImageView(this);
//iv.setImageBitmap(SearchResultActivity.me_bmp[j]);
HashMap<String,Object> temp = new HashMap<String,Object>();
temp.put("image",iv.VISIBLE);
temp.put("price", SearchResultActivity.min_price.get(j));
arr_list.add(temp);
}
adapter = new SimpleAdapter(SearchGridActivity.this,arr_list,R.layout.search_result_griditem,new String[] {"image","price"},new int[] {R.id.img_SearchResultGrid_Product,R.id.txt_SearchResultGrid_Price});
grid.setAdapter(adapter);*/
grid.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(SearchGridActivity.this, "" + position, Toast.LENGTH_SHORT).show();
if(SearchResultActivity.type.get(position).equals("OFFER"))
{
prod_id = SearchResultActivity.id.get(position);
System.out.println("Offer Id==================="+prod_id);
//type_po=true;
Intent intent = new Intent(SearchGridActivity.this,ProductComparisonActivity.class);
intent.putExtra("prod_type", "true");
intent.putExtra("prod_id", SearchResultActivity.id.get(position));
startActivity(intent);
}
else
{
prod_id = SearchResultActivity.id.get(position);
System.out.println("Product Id==================="+prod_id);
//type_po=false;
Intent intent = new Intent(SearchGridActivity.this,ProductComparisonActivity.class);
//intent.putExtra("productType", "SZPID");
intent.putExtra("prod_type", "false");
intent.putExtra("prod_id", SearchResultActivity.id.get(position));
startActivity(intent);
}
}
});
}
//Clear Arraylists
public static void arralist_clear()
{
SearchResultActivity.min_price.clear();
SearchResultActivity.image_me.clear();
}
protected void callHereForEff() {
if(count==1)
{
//ImageAdapter obj1 = new ImageAdapter(this);
//obj1.notifyDataSetChanged();
grid.setAdapter(new ImageAdapter(this));
final ImageAdapter im = new ImageAdapter(this);
im.notifyDataSetChanged();
}
else
{
//ImageAdapter obj = new ImageAdapter(this);
//obj.notifyDataSetChanged();
grid.setAdapter(new ImageAdapter(this));//Load the GridView
final ImageAdapter im = new ImageAdapter(this);
im.notifyDataSetChanged();
}
//grid.setAdapter(adapter);
}
//Showing Images with TextViews in Grid View
public class ImageAdapter extends BaseAdapter{
Context mContext;
public static final int ACTIVITY_CREATE = 10;
public ImageAdapter(Context c){
mContext = c;
}
public int getCount() {
// TODO Auto-generated method stub
return SearchResultActivity.image_me.size();
}
public View getView(int position, View convertView, ViewGroup parent) {
View v;
if(convertView==null){
LayoutInflater li = getLayoutInflater();
v = li.inflate(R.layout.search_result_griditem, null);
TextView tv = (TextView)v.findViewById(R.id.txt_SearchResultGrid_Price);
tv.setText(SearchResultActivity.min_price.get(position));
ImageView iv = (ImageView)v.findViewById(R.id.img_SearchResultGrid_Product);
iv.setImageBitmap(SearchResultActivity.me_bmp[position]);
}
else
{
v = convertView;
}
return v;
}
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
}
//Dialog Creation for Refine Search.
@Override
protected Dialog onCreateDialog(int id) {
brand_list_load.add(brand_array_list.get(0));
store_list_load.add(store_array_list.get(0));
store_new_adapter = new ArrayAdapter<String>(this,R.layout.ref_row, R.id.label, store_array_list);
brand_new_adapter = new ArrayAdapter<String>(this,R.layout.ref_row, R.id.label, brand_array_list);
switch(id) {
case 0:
dialog_refine = new Dialog(this);
dialog_refine.setContentView(R.layout.refine_dialog);
brand_list= (ListView)dialog_refine.findViewById(R.id.exp);
store_list= (ListView)dialog_refine.findViewById(R.id.exp_mat);
brand_list.invalidate();
store_list.invalidate();
brand_options_btn = (ImageButton)dialog_refine.findViewById(R.id.options_btn1);
et_search = (EditText)dialog_refine.findViewById(R.id.txt_RefineSearch);
et_search.setText(keyword);
mat_options_btn = (ImageButton)dialog_refine.findViewById(R.id.options_btn3);
//Loads Brands List
brand_list.setAdapter(arrayAdapter);
//Loads Color List
//Loads Material List
store_list.setAdapter(storeAdapter);
//Reset Button
//Click on any Brand
brand_list.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> arg0, View arg1,int arg2, long arg3)
{
String brand = (String) (brand_list.getAdapter()).getItem(arg2);
brand_name = "+"+brand;
temp = (String) (brand_list.getAdapter()).getItem(0);
brand_selected.clear();
brand_selected.add(brand);
//brand_list_load.clear();
arrayAdapter.clear();
brand_list.invalidate();
reload_list();
//arrayAdapter.add(brand);
//slected_brand = new ArrayAdapter<String>(this,R.layout.ref_row, R.id.label, brand_selected);
System.out.println("POSITION IS-->"+ brand);
}
});
//Click on any Store
store_list.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> arg0, View arg1,int arg2, long arg3)
{
String store = (String) (store_list.getAdapter()).getItem(arg2);
store_name = "+"+store;
store_selected.clear();
store_selected.add(store);
//storeAdapter.add(store);
storeAdapter.clear();
store_list.invalidate();
reload_storelist();
System.out.println("POSITION IS-->"+ store);
}
});
//Brand options
brand_options_btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
arrayAdapter.clear();
brand_list.invalidate();
brand_list.setAdapter(brand_new_adapter);
}
});
// Material Options
mat_options_btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
storeAdapter.clear();
store_list.invalidate();
store_list.setAdapter(store_new_adapter);
}
});
Button btn_reset = (Button)dialog_refine.findViewById(R.id.btn_Refine_reset);
btn_reset.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
dialog_refine.dismiss();
}
});
//Done Button
Button done_btn=(Button)dialog_refine.findViewById(R.id.btn_Refine_Done);
done_btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
//checkflag=false;
count=1;
SearchResultActivity.arraylists_clear();
final_keyword=et_search.getText().toString()+brand_name+store_name;
Intent intent = new Intent(SearchGridActivity.this, SearchGridActivity.class);
intent.putExtra("keyword", keyword);
startActivity(intent);
}
});
break;
default:
dialog_refine = null;
}
return dialog_refine;
}
protected void reload_storelist() {
// TODO Auto-generated method stub
slected_store= new ArrayAdapter<String>(this,R.layout.ref_row, R.id.label, store_selected);
store_list.setAdapter(slected_store);
}
protected void reload_list() {
//brand_list.invalidate();
//arrayAdapter.clear();
slected_brand = new ArrayAdapter<String>(this,R.layout.ref_row, R.id.label, brand_selected);
brand_list.setAdapter(slected_brand);
//brand_selected.clear();
}
//Flurry Implementation
public void onStart()
{
super.onStart();
FlurryAgent.onStartSession(this, "AJT5DWUVF6IAE78NQV69");
// your code
}
public void onStop()
{
super.onStop();
FlurryAgent.onEndSession(this);
// your code
}
}
I haven't check all your code, but I can see a few issues :
1.
grid.setAdapter(new ImageAdapter(this));//Load the GridView
final ImageAdapter im = new ImageAdapter(this);
im.notifyDataSetChanged();
Not sure what this is supposed to do, you create a new ImageAdapter, and then a second and call notifyDataSetChanged on it, which seems pretty useless as it isn't binded to any view.
2. Your ImageAdapter class is broken, especially the getView method. The main idea behind getView is that you recieve a convertView to recycle. If it's null, create a view and set the info in it. If it isn't null, reuse the convertView and set the info in it. In your code, you only set the info if the view is null, so once you scroll or change the data it will try to recycle the views, and you won't change their contents.
The method should be something like
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null){
// No view passed, create one !
LayoutInflater li = getLayoutInflater();
v = li.inflate(R.layout.search_result_griditem, parent, false);
}
// Set the view's content
TextView tv = (TextView)v.findViewById(R.id.txt_SearchResultGrid_Price);
tv.setText(SearchResultActivity.min_price.get(position));
ImageView iv = (ImageView)v.findViewById(R.id.img_SearchResultGrid_Product);
iv.setImageBitmap(SearchResultActivity.me_bmp[position]);
return v;
}
You could also keep a reference to your adapter and call notifyDataSetChanged
when you want the GridView to refresh its contents.
Last thing, you probably don't want the getItemId method in your ImageAdapter to return 0 all the time, as the GridView uses that id to manage recycled views and all. An easier implementation would be
public long getItemId(int position){
return position;
}
It might also be worth filling the getItem method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With