Getting Adapter information of BaseAdapter Before Deletion - android

I have a CustomBase adapter with a button attached to it,when I click on the button the row is to be deleted, which is working perfectly fine, but my problem is, that data which is populated in my adapter is from a remote server, I want to be getting the information contained by the adapter before deleting it and giving it to my MainActivity, in my case, I would like to be getting the title before the view is removed and sending it to the server, telling it to delete it from storage, the big issue is all code to do with the button is in my baseAdapter class, I do not know if I can give my MainActivity the data before the row is Deleted, I cannot think of a way of achieving that, if anyone has an idea please share, below is my Base Adapter.
public class CartAdapter extends BaseAdapter{
private ArrayList<ListItem> listData;
private LayoutInflater layoutInflater;
ViewHolder holder;
public CartAdapter(Context context, ArrayList<ListItem> listData) {
this.listData = listData;
notifyDataSetChanged();
layoutInflater = LayoutInflater.from(context);
}
#Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
}
#Override
public int getCount() {
return listData.size();
}
#Override
public Object getItem(int position) {
return listData.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.singlecartitem, null);
holder = new ViewHolder();
holder.title = (TextView) convertView.findViewById(R.id.title);
final ImageButton delete = (ImageButton) convertView.findViewById(R.id.delete);
convertView.findViewById(R.id.postCommentBox);
delete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
listData.remove(position);
notifyDataSetChanged();
}
});
convertView.setTag(holder);
delete.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
final ListItem newsItem = listData.get(position);
holder.title.setText(newsItem.getTitle());
return convertView;
}
static class ViewHolder {
TextView title;
int position;
}
}

Are you using a ListFragment? If you do, check
this out.
If you do not, make your activity implement an interface e.g. HandleDeletion with a method e.g. handleTitle(String title). In your clickListener cast the context object to HandleDeletion and call the method handleTitle(String title) on it. This is a so called callback.
[EDIT]
Example:
public interface HandleDeletion {
handleTitle(String title);
}
public class MyActivity implements HandleDeletion {
...
handleTitle(String title) {
//do whatever you want
}
...
}
public class CartAdapter extends BaseAdapter{
...
private Context context;
public CartAdapter(Context context, ArrayList<ListItem> listData) {
this.listData = listData;
this.context = context;
notifyDataSetChanged();
layoutInflater = LayoutInflater.from(context);
}
public View getView(final int position, View convertView, ViewGroup parent) {
...
delete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
((HandleDeletion)context).handleTitle(listData.get(position).getTitle());
listData.remove(position);
notifyDataSetChanged();
}
});
...
}
...
}

Related

Removing Values inside BaseAdapter removed always last Value

I have created a ListView for My Model class and displayed it successfully.
I have successfully done removing value/raw from my ListView onItemClickListener of ListView.
It's Ok, But, my ListView also contains an ImageView and I have to delete a row on click of that ImageView.
So, I have taken a BaseAdapter and handle click of my that ImageView inside my BaseAdapter as below :
My Adapter class is as below :
public class MyAdapter extends BaseAdapter {
private ArrayList<ModelClass> mArrayListModel;
private LayoutInflater mInflator;
private Context mContext;
public MyAdapter(Context mContext, ArrayList<ModelClass> mArrayListModel) {
this.mContext = mContext;
this.mArrayListModel = mArrayListModel;
}
#Override
public int getCount() {
return mArrayListModel.size();
}
#Override
public Object getItem(int position) {
return mArrayListModel.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder view = null;
if (convertView == null) {
view = new ViewHolder();
mInflator = LayoutInflater.from(mContext);
convertView = mInflator.inflate(R.layout.custom_layout, parent, false);
view.textName = (TextView) convertView.findViewById(R.id.txtName);
view.textAddress = (TextView) convertView.findViewById(R.id.txtAddress);
// view.btnShow = (Button) convertView.findViewById(R.id.btnShow);
view.imgDelete=(ImageView) convertView.findViewById(R.id.imgDelete);
view.textName.setText("" + mArrayListModel.get(position).getStrName());
view.textAddress.setText("" + mArrayListModel.get(position).getStrAddress());
convertView.setTag(view);
view.imgDelete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(mContext, ""+mArrayListModel.get(position).getStrName(), Toast.LENGTH_SHORT).show();
mArrayListModel.remove(position);
notifyDataSetChanged();
}
});
} else {
view = (ViewHolder) convertView.getTag();
}
return convertView;
}
class ViewHolder {
TextView textName, textAddress;
//Button btnShow;
ImageView imgDelete;
}
}
I am deleting as below :
view.imgDelete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(mContext, ""+mArrayListModel.get(position).getStrName(), Toast.LENGTH_SHORT).show();
mArrayListModel.remove(position);
notifyDataSetChanged();
}
});
The Problem is with deleting item, When I click on ImageView it deletes only last value.. last position from listview. I think its issue with my position.
Will you pls. check it and let me know what might be the issue ?
Thanks.

Repeated value of position - GridView

I am working on an Android Application , here I need to display data in the GridView , The problem I am facing now is that value of position variable is repeating inside the getView(int position, View convertView, ViewGroup parent) method of the subclass of BaseAdapter . I tried to find out the issue after removing additional code from the subclass , still result is the same . I visited similar posts on stackoverflow but no luck .
Here is the code of LazyAdapter (subclass of BaseAdapter)
public class LazyAdapter extends BaseAdapter {
private Context ctx;
private List<ItemDetails> items;
BooksViewHolder booksViewHolder = null ;
public LazyAdapter(Context context, List<ItemDetails> arrayList) {
super();
ctx = context;
this.items = arrayList;
}
public int getCount() {
return items.size();
}
public Object getItem(int position) {
return items.get(position);
}
public long getItemId(int position) {
return items.indexOf(getItem(position));
}
class BooksViewHolder{
RelativeLayout containerLayout , playout , prelayout , offlinetext , thumblayout ;
TextView text , priceText;
ImageView image ;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(convertView==null) {
booksViewHolder = new BooksViewHolder();
booksViewHolder.prelayout = (RelativeLayout) convertView.findViewById(R.id.prelayout);
convertView = inflater.inflate(R.layout.bookcontent, null);
booksViewHolder.text = (TextView) convertView.findViewById(R.id.bookname);
booksViewHolder.priceText = (TextView) convertView.findViewById(R.id.pricetext);
convertView.setTag(booksViewHolder);
} else {
booksViewHolder = (BooksViewHolder) convertView.getTag();
}
prelayout.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
bookClickListener.onBookClick(position);
}});
return convertView;
}
interface OnBookClickListener{
void onBookClick(int bookPosition);
}
public void setOnBookClickListenre(OnBookClickListener clickListener){
bookClickListener = clickListener ;
}
}
MyActivity.java :
public class MyActivity extends Activity implements LazyAdapter.OnBookListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.info);
//Code to get the reference of Gridview .
// set the listener on Gridview
}
#Override
public void onBookClick(int position){
BookModel book= lazyadapter.getItems().get(position);
//Here due to incorrect position I am not able to get particular book .
}
}
Help me please , I am not able to figure out the issue .

How to change row view in listview when clicking on item

Is it possible to change row view when I'm clicking on it? For example I have listView with rows where some short information shown, and when after clicking on row detailed view will show instead of old row.
I tried to use addView in my listView, but "listview addView(View, int) is not supported in AdapterView" error appeared.
try something like that:
public class MyAdapter extends BaseAdapter {
private Context mContext;
private List<UserPojo> mList;
public MyAdapter(Context context, List<UserPojo> users) {
this.mContext = context;
this.mList = users;
}
#Override
public int getCount() {
return mList.size();
}
#Override
public UserPojo getItem(int position) {
return mList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final UserPojo user = mList.get(position);
final ViewHolder holder;
if (convertView == null) {
convertView = View.inflate(mContext, R.layout.item_ad, null);
holder = new ViewHolder();
holder.pic = (CircleImageView) convertView.findViewById(R.id.profile_imageView);
holder.name = (TextView) convertView.findViewById(R.id.name_textView);
convertView.setTag(holder);
} else{
holder = (ViewHolder) convertView.getTag();
}
if (user.isClicked) {
holder.pic.setVisibility(View.VISIBLE);
holder.name.setVisibility(View.GONE);
} else {
holder.pic.setVisibility(View.GONE);
holder.name.setVisibility(View.VISIBLE);
}
holder.name.setText("text");
holder.name.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
user.setClicked(true);
notifyDataSetChanged();
}
});
return convertView;
}
private static class ViewHolder {
CircleImageView pic;
TextView name;
}
}

android accessing button in custom adapter

I want to access a button from an activity in a lazyadapter's getview( that is to make clickable from lazyadapter) is there any method to do that? The button click brings the user to the next screen. Please if someone has any idea or code.
You just follow this code on get your Click function and call your next activity in the onclick listener....
CustomAdapter mAdapter = new CustomAdapter(this, R.layout.listitem, mListItems);
mPullRefreshListView.setAdapter(mAdapter);
public class CustomAdapter extends ArrayAdapter<Sample> {
public ArrayList<Sample> mlist;
public Context context;
public LayoutInflater inflater;
public CustomAdapter(Context context, int resource, ArrayList<Sample> mlist) {
super(context, resource);
this.mlist = mlist;
this.context = context;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getPosition(Sample item) {
return super.getPosition(item);
}
#Override
public Sample getItem(int position) {
return mlist.get(position);
}
#Override
public int getCount() {
return mlist.size();
}
#Override
public long getItemId(int position) {
return super.getItemId(position);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = inflater.inflate(R.layout.listitem, null);
TextView text1 = (TextView) convertView.findViewById(R.id.item1);
TextView text2 = (TextView) convertView.findViewById(R.id.item2);
text1.setText(mlist.get(position).getListitem1());
text2.setText(mlist.get(position).getListitem2());
text2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// you just put your Logic here And use this custom adapter to
// load your Data By using this particular custom adapter to
// your listview
}
});
return convertView;
}
}

how to create trackballevent in android custom adapter?

i am using following code to create custom adapter for listview.
now i want to use trackball click event in it but i dont know how to do that can any one help me out in creating ontracballevent in custom adapter?
i have tried writing few lines but not able to solve it.
public class EfficientAdapter extends BaseAdapter implements Filterable {
private LayoutInflater mInflater;
private Context context;
int pos;
public EfficientAdapter(Context context) {
mInflater = LayoutInflater.from(context);
this.context = context;
}
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
convertView = mInflater.inflate(R.layout.adaptor_contentposts, null);
convertView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//click functionality
}
});
MotionEvent event= MotionEvent.CREATOR.createFromParcel(null);
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
//display click message
}
convertView.onTrackballEvent(event);
return convertView;
}
class ViewHolder {
TextView textLine;
TextView textLine2;
TextView PostedByAndPostedOn;
ImageButton ImgButton;
}
#Override
public Filter getFilter() {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public int getCount() {
return ad_id.length;
}
#Override
public Object getItem(int position) {
return ad_id[position];
}
}
Adapters have nothing to do with "ontracballevent". You either need to subclass ListView or handle it in your activity.

Categories

Resources