I implemented an setOnClickListener in my getView method to add the a product to my ArrayList, but my class doesn't recognize the method itemClicked, when i call the OnClick method inside the getView.
Adapter:
public class GridAdapter extends BaseAdapter{
private Context ctx;
private ArrayList<Produto> list ;
GridAdapter(Context context, ArrayList<Produto> s){
this.ctx = context;
this.list = s;
}
#Override
public View getView(int position, View convertiew, ViewGroup parent) {
View grid;
LayoutInflater inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertiew == null){
grid = new View(ctx);
grid = inflater.inflate(R.layout.grid_row, null);
TextView textView = (TextView) grid.findViewById(R.id.tv_gridrow);
ImageView imageView = (ImageView)grid.findViewById(R.id.img_gridrow);
textView.setText(list.get(position).getNome());
imageView.setImageResource(list.get(position).getImagem());
imageView.setAdjustViewBounds(true);
imageView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
ctx.itemClicked(position);
}
});
} else {
grid = (View) convertiew;
}
return grid;
}
MainActivity
public class MainActivity extends Activity {
private ArrayList<Produto> array_produtos;
private ArrayList<Pedido> produtos_pedidos;
private int numero_pedidos;
private Produto produto_1;
private Produto produto_2;
private Produto produto_3;
private Produto produto_4;
private Produto produto_5;
private Pedido pedido_1;
private TextView apresenta_total;
private GridView gv;
private ListView lv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
array_produtos = new ArrayList<Produto>();
produtos_pedidos = new ArrayList<Pedido>();
numero_pedidos=0;
//Inicialização dos produtos
produto_1 = new Produto(1, "Café",0.60,4,4,R.drawable.cafe1);
produto_2 = new Produto(2, "Água", 0.90,3,2, R.drawable.luso1);
produto_3 = new Produto(3, "Sopa", 1.20,2,5, R.drawable.sopa3);
produto_4 = new Produto(4, "Cerveja", 1.00,5,4, R.drawable.fino2);
produto_5 = new Produto(5, "Coca-cola", 1.10, 3,2, R.drawable.coca_cola1);
array_produtos.add(produto_1);
array_produtos.add(produto_2);
array_produtos.add(produto_3);
array_produtos.add(produto_4);
array_produtos.add(produto_5);
pedido_1 = new Pedido(); //cria primeiro pedido
produtos_pedidos.add(pedido_1); //Atribui à lista de pedidos
gv = (GridView)findViewById(R.id.gv_produtos);
lv = (ListView) findViewById(R.id.lv_produtos_pedidos);
gv.setAdapter(new GridAdapter(this, array_produtos));
ArrayAdapter<Pedido> arrayAdapter = new ArrayAdapter<Pedido>(this, android.R.layout.simple_list_item_1,produtos_pedidos);
lv.setAdapter(arrayAdapter);
}
public void itemClicked(int position) {
produtos_pedidos.get(0).addProduto(produto_1);
}
Have you try to cast your context?
#Override
public void onClick(View v) {
if(ctx instanceof MyActivity){
((MyActivity)ctx).itemClicked(int position);
}
Cheers
Related
So I have an activity with a listview inside of it and when I click a delete button I want to remove the object from the list. I already know how to find which object I want to remove and remove it from the list that populates my array adapter but I am not sure what i need to do to call the .notifyDataSetChanged() routine which Is what I believe I need to do. Any help would be much appreciated.
My activity code
public class MealActivity2 extends AppCompatActivity {
private ListView lv;
public static ArrayList<Model> modelArrayList;
private CustomAdapter customAdapter;
private Button btnnext;
private String[] fruitlist = new String[]{"Apples", "Oranges", "Potatoes", "Tomatoes","Grapes"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_meal2);
Integer numFoodItems = ((Globals) getApplication()).getLength();
lv = (ListView) findViewById(R.id.lv);
btnnext = (Button) findViewById(R.id.next);
modelArrayList = getModel(numFoodItems);
customAdapter = new CustomAdapter(this);
lv.setAdapter(customAdapter);
btnnext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MealActivity2.this,NextActivity.class);
startActivity(intent);
}
});
}
// this builds the array list of model opbjects that will be used by the adapter to populate
// the list view dynamically
private ArrayList<Model> getModel(Integer length){
ArrayList<Model> list = new ArrayList<>();
for(int i = 0; i < length; i++){
FoodDetailsPost food = ((Globals) getApplication()).getFoodList().get(i);
Model model = new Model();
model.setNumber(((Globals) getApplication()).getServing(i));
model.setFruit(food.getDescription());
list.add(model);
}
return list;
}
}
and here is my custom adapter code if you look near the end at the last comment that is where i need to notify my activity that I have changed the contents of the list and should update the displaying items
public class CustomAdapter extends BaseAdapter {
private Context context;
private adapterGlobalAccess g = new adapterGlobalAccess();
private CustomAdapter customAdapter;
public CustomAdapter(Context context) {
this.context = context;
}
#Override
public int getViewTypeCount() {
return getCount();
}
#Override
public int getItemViewType(int position) {
return position;
}
#Override
public int getCount() {
return MealActivity2.modelArrayList.size();
}
#Override
public Object getItem(int position) {
return MealActivity2.modelArrayList.get(position);
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder(); LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.lv_item, null, true);
holder.tvFruit = (TextView) convertView.findViewById(R.id.animal);
holder.tvnumber = (TextView) convertView.findViewById(R.id.number);
holder.btn_edit = (Button) convertView.findViewById(R.id.plus);
holder.btn_minus = (Button) convertView.findViewById(R.id.minus);
convertView.setTag(holder);
}else {
// the getTag returns the viewHolder object set as a tag to the view
holder = (ViewHolder)convertView.getTag();
}
holder.tvFruit.setText(MealActivity2.modelArrayList.get(position).getFruit());
holder.tvnumber.setText(String.valueOf(MealActivity2.modelArrayList.get(position).getNumber()));
holder.btn_edit.setTag(R.integer.btn_plus_view, convertView);
holder.btn_edit.setTag(R.integer.btn_plus_pos, position);
holder.btn_edit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
View tempview = (View) holder.btn_edit.getTag(R.integer.btn_plus_view);
TextView tv = (TextView) tempview.findViewById(R.id.number);
Integer pos = (Integer) holder.btn_edit.getTag(R.integer.btn_plus_pos);
Double number = Double.parseDouble(tv.getText().toString()) + 1;
tv.setText(String.valueOf(number));
MealActivity2.modelArrayList.get(pos).setNumber(number);
g.setServing(pos, number);
}
});
holder.btn_minus.setTag(R.integer.btn_minus_view, convertView);
holder.btn_minus.setTag(R.integer.btn_minus_pos, position);
holder.btn_minus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
View tempview = (View) holder.btn_minus.getTag(R.integer.btn_minus_view);
TextView tv = (TextView) tempview.findViewById(R.id.number);
Integer pos = (Integer) holder.btn_minus.getTag(R.integer.btn_minus_pos);
((Globals) context.getApplicationContext()).remove(pos);
MealActivity2.modelArrayList.remove(pos);
// i need to do .notifyDataSetChanged() here but im not sure how
}
});
return convertView;
}
You can just use a interface to solve the problem
CustomAdapter
public class CustomAdapter extends BaseAdapter {
...
private CustomAdapterListener listener;
interface CustomAdapterListener{
void itemClick();
}
public CustomAdapter(Context context, CustomAdapterListener listener) {
this.context = context;
this.listener = listener;
}
MainActivity2
public class MealActivity2 extends AppCompatActivity implements CustomAdapterListener {
...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_meal2);
...
customAdapter = new CustomAdapter(this, this);
}
#Override
public void itemClick() {
// you can do .notifyDataSetChanged() here
}
after that you can use listener to callback to Activity and do anything you want
CustomAdapter
#Override
public void onClick(View v) {
listener.itemClick();
// i need to do .notifyDataSetChanged() here but im not sure how
}
Recycle view not displaying data its shows an empty screen, It displays data when I go back and click the icon again. Not sure why I'm facing this problem.Please check the activity, adaptor and fragment code below. I'm new to android and I'm doing my capstone project it would be great if you can help me.
public class AppDetailsActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle("App Details");
setContentView(R.layout.activity_app);
AppDetailsFragment newFragment = new AppDetailsFragment();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.recordAppFragmentContainer, newFragment);
transaction.addToBackStack(null);
transaction.commit();
}
public void refreshAppDuration(View v) {
Intent intent = new Intent(this, AppDetailsActivity.class);
startActivity(intent);
}
protected void onPause() {
super.onPause();
}
protected void onStop() {
super.onStop();
}
protected void onDestroy() {
super.onDestroy();
}
public void onBackAppDuration(View v) {
if (getFragmentManager().getBackStackEntryCount() > 0) {
getFragmentManager().popBackStack();
} else {
super.onBackPressed();
}
}
Adaptor
public class AppDetailsAdaptor extends RecyclerView.Adapter<AppDetailsAdaptor.ViewHolder> {
private List<AppUsage> mApps;
private Context mContext;
public AppDetailsAdaptor(Context context, List<AppUsage> apps) {
mApps = apps;
mContext = context;
}
private Context getContext() {
return mContext;
}
#Override
public AppDetailsAdaptor.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
// Inflate the custom layout
View appUsageView = inflater.inflate(R.layout.item_app_details, parent, false);
// Return a new holder instance
AppDetailsAdaptor.ViewHolder viewHolder = new AppDetailsAdaptor.ViewHolder(appUsageView);
return viewHolder;
}
#Override
public void onBindViewHolder(AppDetailsAdaptor.ViewHolder viewHolder, final int position) {
// Get the data model based on position
final AppUsage ch = mApps.get(position);
// Set item views based on your views and data model
viewHolder._appTextView.setText(ch.getAppName());
viewHolder._durationTextView.setText(ch.getDurationInMinutes());
viewHolder._dateTextView.setText(ch.getDate().substring(0,10));
final String key = ch.getKey();
}
#Override
public int getItemCount() {
return mApps.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView _appTextView;
public TextView _durationTextView;
public TextView _dateTextView;
super(itemView);
_appTextView = (TextView) itemView.findViewById(R.id.rvs_app_name);
_durationTextView = (TextView) itemView.findViewById(R.id.rvs_app_duration);
_dateTextView =(TextView) itemView.findViewById(R.id.rvs_app_date);
}
}
}
Fragment
public class AppDetailsFragment extends Fragment {
final FirebaseDatabase database = FirebaseDatabase.getInstance();
ArrayList<AppUsage> appList = new ArrayList<>();
ArrayList<AppUsage> appListTemp = new ArrayList<>();
ArrayList<AppUsage> appListAdaptor = new ArrayList<>();
ArrayList<AppUsage> appObject = new ArrayList<>();
ArrayList<AppUsage> appListDate = new ArrayList<>();
private AppDetailsFragment.OnFragmentInteractionListener mListener;
private RecyclerView rvApp;
private RecyclerView.Adapter appAdaptor;
private RecyclerView.LayoutManager eLayoutManager;
private EditText date;
private static final int DIALOG_DATE_PICKER = 100;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_app_list, container, false);
rvApp = (RecyclerView) rootView.findViewById(R.id.rvAppList);
rvApp.setHasFixedSize(true);
eLayoutManager = new LinearLayoutManager(getActivity());
rvApp.setLayoutManager(eLayoutManager);
appList = getAllAppRecords();
appAdaptor = new AppDetailsAdaptor(getActivity(), appList);
rvApp.setAdapter(appAdaptor);
return rootView;
}
public ArrayList<AppUsage> getAllAppRecords(){
DatabaseReference ref = database.getReference();
ref.child("AppUsage").addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
appObject.clear();
HashMap<String,Object> app = null;
Iterator<DataSnapshot> items = dataSnapshot.getChildren().iterator();
while(items.hasNext()){
DataSnapshot item = items.next();
Log.e("Listener",item.toString() );
app =(HashMap<String, Object>) item.getValue();
appObject.add(new AppUsage(app.get("appName").toString(),app.get("durationInMinutes").toString(),app.get("date").toString(),item.getKey()));
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
return appObject;
}
I have requirement to create nested recyclerView like
RecyclerView Contains-> CardView contains-> RecyclerView.
If I click on card I'm getting child position only but I also need parent card position. How can I handle click events of both Recyclerview and CardView at same time?
MainActivity.java:
public class MainActivity extends Activity {
LinearLayout linView;
private RecyclerView main_view;
RootAdapter adapter2;
private RelativeLayout mRelativeLayout;
private Context mContext;
private Activity mActivity;
int pos = 0;
int main_pos;
private static RecyclerView.Adapter adapter;
private static RecyclerView.Adapter adapterCo;
private RecyclerView.LayoutManager layoutManager;
private static ArrayList<DataModelMain> dataMain;
private static ArrayList<DataModel_CO> dataCo;
static View.OnClickListener myOnClickListener;
static View.OnClickListener onClickListener;
private static ArrayList<Integer> removedItems;
TestMain mainData = new TestMain();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
prof = (ImageView) findViewById(R.id.profile);
main_view = (RecyclerView) findViewById(R.id.recycler_main);
mRelativeLayout = (RelativeLayout) findViewById(R.id.partners);
cont = (TextView) findViewById(R.id.mobile);
iconType = (ImageView) findViewById(R.id.icon_type);
linView = (LinearLayout) findViewById(R.id.layout_view);
// Get the application context
mContext = getApplicationContext();
// Get the activity
mActivity = MainActivity.this;
myOnClickListener = new MyOnClickListener(this);
onClickListener = new OnClickListener(this);
main_view.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(
this,
LinearLayoutManager.VERTICAL,
false
) {
};
main_view.setLayoutManager(layoutManager);
main_view.setItemAnimator(new DefaultItemAnimator());
dataMain = new ArrayList<DataModelMain>();
getMainData();
setMainView();
}
}
private void getMainData() {
for (int i = 0; i < mainData.passengers.length; i++) {
dataMain.add(new DataModelMain(
mainData.type[i],
mainData.passengers[i],
mainData.p_name[i],
mainData.p_contact[i],
mainData.p_flight[i],
mainData.p_pnr[i],
mainData.air[i],
mainData.p_time[i]
));
}
adapter2 = new RootAdapter(this, dataMain);
main_view.setAdapter(adapter2);
}
private class MyOnClickListener implements View.OnClickListener {
private final Context context;
private MyOnClickListener(Context context) {
this.context = context;
}
#Override
public void onClick(View v) {
showIcons(v);
}
private void showIcons(View v) {
pos = main_view.getChildPosition(v);
setMainView();
}
}
private class OnClickListener implements View.OnClickListener {
private final Context context1;
private OnClickListener(Context context0) {
this.context1 = context0;
}
#Override
public void onClick(View v) {
showIcons(v);
}
private void showIcons(View v) {
pos = main_view.getChildPosition(v);
Log.e("CLICKED","pos: "+pos);
}
}
private class RootAdapter extends RecyclerView.Adapter<RootAdapter.RootViewHolder> {
private final LayoutInflater inflater;
String[] _items = new String[]{"GROUP 1", "GROUP 2", "GROUP 3", "GROUP 4"};
private ArrayList<DataModelMain> dataSet;
public RootAdapter(Context context,ArrayList<DataModelMain> data) {
inflater = LayoutInflater.from(context);
this.dataSet = data;
}
#Override
public RootViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = inflater.inflate(R.layout.card_main, viewGroup, false);
RootViewHolder rvi = new RootViewHolder(view);
view.setOnClickListener(MainActivity.onClickListener);
return rvi;
}
#Override
public void onBindViewHolder(RootViewHolder rootViewHolder, int i) {
rootViewHolder.recyclerViewChild.setLayoutManager(new LinearLayoutManager(inflater.getContext()));
rootViewHolder.recyclerViewChild.setAdapter(new ChildAdapter(inflater,dataSet));
}
#Override
public int getItemCount() {
return _items.length;
}
class RootViewHolder extends RecyclerView.ViewHolder {
RecyclerView recyclerViewChild;
public RootViewHolder(View itemView) {
super(itemView);
recyclerViewChild = (RecyclerView) itemView.findViewById(R.id.recycler_partners);
}
}
}
private class ChildAdapter extends RecyclerView.Adapter<ChildAdapter.ChildViewHolder> {
private LayoutInflater _inflater;
private ArrayList<DataModelMain> dataSet;
public ChildAdapter(LayoutInflater inflater,ArrayList<DataModelMain> data) {
_inflater = inflater;
this.dataSet = data;
}
#Override
public ChildViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = _inflater.inflate(R.layout.card_partner, viewGroup, false);
ChildViewHolder rvi = new ChildViewHolder(view);
view.setOnClickListener(MainActivity.myOnClickListener);
return rvi;
}
#Override
public void onBindViewHolder(ChildViewHolder holder, int listPosition) {
TextView name = holder.tvName;
TextView cont = holder.tvCont;
TextView flight = holder.tvFlight;
TextView pnr = holder.tvPNR;
TextView time = holder.tvTime;
ImageView type = holder.im_type;
ImageView pic = holder.im_pic;
ImageView logo = holder.im_logo;
CardView cardView = holder.card;
name.setText(dataSet.get(listPosition).getName());
cont.setText(dataSet.get(listPosition).getContact());
flight.setText(dataSet.get(listPosition).getFlight());
pnr.setText(dataSet.get(listPosition).getPnr());
time.setText(dataSet.get(listPosition).getTime());
type.setImageResource(dataSet.get(listPosition).getType());
pic.setImageResource(dataSet.get(listPosition).getImage());
logo.setImageResource(dataSet.get(listPosition).getLogo());
switch (dataSet.get(listPosition).getType()){
case R.mipmap.gov:
cardView.setCardBackgroundColor(Color.parseColor("#FAFAD2"));
break;
case R.mipmap.vip:
cardView.setCardBackgroundColor(Color.parseColor("#CDFFCD"));
break;
case R.mipmap.jail:
cardView.setCardBackgroundColor(Color.parseColor("#FFE4E1"));
break;
case R.mipmap.ban:
cardView.setCardBackgroundColor(Color.parseColor("#DCDCDC"));
break;
}
}
#Override
public int getItemCount() {
return dataSet.size();
}
public class ChildViewHolder extends RecyclerView.ViewHolder {
ImageView im_type,im_pic,im_logo;
TextView tvName,tvCont,tvFlight,tvPNR,tvTime;
CardView card;
public ChildViewHolder(View itemView) {
super(itemView);
this.card = (CardView) itemView.findViewById(R.id.card_view);
this.im_type = (ImageView) itemView.findViewById(R.id.p_type);
this.im_pic = (ImageView) itemView.findViewById(R.id.p_profile);
this.im_logo = (ImageView) itemView.findViewById(R.id.p_air_lines);
this.tvName = (TextView) itemView.findViewById(R.id.p_name);
this.tvCont = (TextView) itemView.findViewById(R.id.p_mob);
this.tvFlight = (TextView) itemView.findViewById(R.id.p_flight);
this.tvPNR = (TextView) itemView.findViewById(R.id.p_pnr);
this.tvTime = (TextView) itemView.findViewById(R.id.p_timer);
}
}
}
}
How about making the root/parent cardView clickable??
Give an id to the cardView item of the parent recyclerView. Place it in your ViewHolderClass and then assign a onCLickListener to the cardView in BindViewHolder like this:
rootViewHolder.rootCardView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// keep an track of the selected position of the cardView and store it in a global/local variable
}
});
you can track the adapter position of the selected cardView using
rootViewHolder.getAdapterPosition()
Note: rootViewHolder is written as per your ViewHolder class
I hope this helps !!
This is what i have tried, but my ListView doesn't get filled. I use a custom adapter because i wan't to change the background color of the items that't got a boolean value = true. I am using Android Studio.
Hope someone can help.
i'm new to android.
public class ShoppingListActivity extends ActionBarActivity {
private TextView header;
private ListView listView;
private CustomArrayAdapter arrayAdapter;
private ShoppingList shoppingList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shopping_list);
header = (TextView)findViewById(R.id.textView2);
//get reference to ListView
listView = (ListView)findViewById(R.id.shoppingListItemsView);
shoppingList = (ShoppingList) getIntent().getSerializableExtra(Globals.CHOSENLISTKEY);
arrayAdapter = new CustomArrayAdapter(this, R.layout.custom_list_view, shoppingList.getItemList());
listView.setAdapter(arrayAdapter);
header.setText(shoppingList.toString());
Button btnShop = (Button) findViewById(R.id.btnShop);
btnShop.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(ShoppingListActivity.this, SelectStoreActivity.class);
startActivity(intent);
}
});
}
public void setData() {
for(int i = 0; i < shoppingList.getItemList().size(); i++) {
arrayAdapter.add(shoppingList.getItemList().get(i));
}
}
}
public class CustomArrayAdapter extends ArrayAdapter<Entry> {
private int resource;
private ArrayList<Entry> list;
public CustomArrayAdapter(Context context, int resource, ArrayList<Entry> list) {
super(context, resource);
this.resource = resource;
this.list = list;
}
#Override
public View getView(int position, View convertView, ViewGroup parentGroup) {
View view = convertView;
Entry entry = list.get((position));
if(view == null) {
LayoutInflater layoutInflater = ((Activity) getContext()).getLayoutInflater();
view = layoutInflater.inflate(resource, parentGroup, false);
}
TextView textView = (TextView) view.findViewById(R.id.customName);
if(entry.getItem().isBought()) {
textView.setBackgroundColor(Color.BLUE);
}
return view;
}
}
To show data in ListView rows call setText method of TextView which is return from getView method:
TextView textView = (TextView) view.findViewById(R.id.customName);
// get Text from entry and pass it to setText method
String strTextViewData=entry.getItem()...;
textView.setText(strTextViewData);
if(entry.getItem().isBought()) {
textView.setBackgroundColor(Color.BLUE);
}
In my app ,i used view pager to show my image gallery .
after deleteing any image from my gallery , view pager should get updated.
My Activity
public class FullScreenViewActivityForSelfImage extends Activity{
#SuppressWarnings("unused")
private Utils utils;
private FullScreenImageAdapterForSelfImage adapter;
private static ViewPager viewPager;
DatabaseHelper db;
ArrayList<Image> aryimages;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fullscreen_view);
db=new DatabaseHelper(this);
viewPager = (ViewPager) findViewById(R.id.pager);
aryimages= db.getimages();
Intent i = getIntent();
int position = i.getIntExtra("position", 0);
adapter = new FullScreenImageAdapterForSelfImage(FullScreenViewActivityForSelfImage.this,
aryimages,this);
viewPager.setAdapter(adapter);
// displaying selected image first
viewPager.setCurrentItem(position);
}
public static ViewPager getAdapter() {
return viewPager;
}
}
My Adapter Class
public class FullScreenImageAdapterForSelfImage extends PagerAdapter
{
private Activity _activity;
private ArrayList<Image> _imagePaths;
ArrayList<Image> aryimages;
public ImageListAdapter ImageListAdapter;
private LayoutInflater inflater;
private Context context;
int columnWidth,width,cellWidth,padding,height;
DatabaseHelper db;
int i;
String id,name;
// constructor
public FullScreenImageAdapterForSelfImage(Activity activity,
ArrayList<Image> aryimages,Context context) {
this._activity = activity;
this._imagePaths = aryimages;
this.context = context;
db= new DatabaseHelper(_activity);
Display display = activity.getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
width = size.x;
height = size.y;
}
My Adapter where delete service is called
btndel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(v.getContext());
alertDialog.setTitle("Confirm Delete...");
alertDialog.setMessage("You are about to delete all images of this product continue");
alertDialog.setPositiveButton("YES",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
// Write your code here to execute after dialog
name = _imagePaths.get(position).Image_name.toString();
String imei=db.SelectImeiNo();
id=db.getidbyname(name);
System.out.println("selected imei = "+imei);
System.out.println("selected id = "+id);
dialog.cancel();
WebServiceTask wst = new WebServiceTask(WebServiceTask.POST_TASK, context , "Deleting Image...");
wst.execute(new String[] { Service_url });
}
});
In Adapter i tried to refresh
public void handleResponse(String response) {
System.out.println("handle REsponse...got result..."+response);
if(response.equalsIgnoreCase("0"))
{
System.out.println("!!!!!!!!!!Server Busy try again latter!!!!!!!!!!!!!!!!");
Toast.makeText(context.getApplicationContext(), "Server Busy try again latter", Toast.LENGTH_LONG).show();
}
else if(response.equalsIgnoreCase("1"))
{
System.out.println("!!!!!!!!!!!!Image deleted!!!!!!!!!!!!!!!!!!!");
Toast.makeText(context.getApplicationContext(), "Image deleted", Toast.LENGTH_LONG).show();
db.deleteImageSelected(id);
_imagePaths.remove(i);
ImageListAdapter.notifyDataSetChanged();
}
}
Image Adapter
public class ImageListAdapter extends BaseAdapter {
//private Context context;
private ArrayList<Image> Img;
private LayoutInflater inflater = null;
public ImageListAdapter(Context context, ArrayList<Image> Image) {
// this.context = context;
this.Img = Image;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return Img.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
#SuppressLint("InflateParams")
#SuppressWarnings("unused")
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
if (convertView == null) {
view = inflater.inflate(R.layout.imageshow, null);
} else {
view = convertView;
}
ImageView iv= (ImageView) view.findViewById(R.id.imageshowView1);
TextView name= (TextView) view.findViewById(R.id.Imageshownametv);
TextView id= (TextView) view.findViewById(R.id.Imageshowidtv);
TextView desc= (TextView) view.findViewById(R.id.Imageshowimagedesc);
id.setText(Img.get(position).Image_id);
byte[] I= Img.get(position).image;
System.out.println("Byte array of the image "+I);
Bitmap bp=BitmapFactory.decodeByteArray(I, 0, I.length);
iv.setImageBitmap(bp);
return view;
}
}
But it is not done
so please help me..