I have ListView with check boxes the app delete the checked items ;; and should delet teh items that have the same id with the checked items :
here's my code
private class CAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private ArrayList<Entity> list;
private Context context;
String Status;
CAdapter(Context context,
ArrayList<Entity> getC) {
this.context = context;
this.list = getC;
Status="";
mInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
class ViewHolder {
TextView Name;
TextView Desc;
Button deleteBtn;
CheckBox CBox;
}
public int getCount() {
return list.size();
}
public Object getItem(int position) {
return list.get(position);
}
public long getItemId(int position) {
return position;
}
#SuppressLint("NewApi")
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
final CEntity CObj = list.get(position);
if (convertView == null) {
convertView = mInflater.inflate(
R.layout.custom_list_view_confirmed, parent,
false);
holder = new ViewHolder();
holder.Name = (TextView) convertView
.findViewById(R.id.Name);
holder.Desc = (TextView) convertView
.findViewById(R.id.activity1);
holder.deleteBtn = (Button) convertView
.findViewById(R.id.deleteBtn);
holder.CBox=(CheckBox) convertView.findViewById(R.id.isCheck);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
if (CObj.getMystatus().equals(
context.getResources().getString(R.string.course_status_delete))) {
holder.status.setTextColor(Color.RED);
} else if (attemptedCourseObj.getMystatus().equals(
context.getResources().getString(R.string.course_status_pending))) {
holder.status.setTextColor(Color.GREEN);
} else if (attemptedCourseObj.getMystatus().equals(
context.getResources().getString(R.string.course_status_update))) {
holder.status.setTextColor(Color.BLUE);
}
holder.Name.setText(attemptedCourseObj.getCourseName());
holder.CBox.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(holder.CBox.isChecked()){
if(list.contains(getItem(position))){
list.remove(getItem(position));
}
}
}
});
//
return convertView;
}
}
the problem is when delete the checked it dosent delete the item that have the same id .
int pos=Data.CList.size();
SparseBooleanArray checked=CListView.getCheckedItemPositions();
for (int n = pos; n > 0; n--){
if (checked.get(n)){
code=Data.inList.get(n).getCCode();
Data.inList.remove(n);
}else if(CList.get(n).equal(code){
Data.inList.remove(n);
}
try to refresh the list with notifydatasetchanged and be sure that you delete the entire object from the list
You need to inform your adapter with the new modifications by giving the adapter the new dataset after removing the deleted elements from it and notify with the changes. You can do this as the following:
1)Add method into your adapter to set the new data as following:
public void setNewData(ArrayList<Entity> newEntities){
this.list = newEntities;
}
2)From the activity or the fragment call the previous method with the new data and call this line to notify the adapter with the changes
myAdapter.setNewData(myNewEntities);
myAdapter.notifyDataSetChanges();
Read this answer for more info about NotifyDataSetChanges() method
Related
Hi I m using below code where when user touches the textview , list will be updated and touched list row will be in front of user.
public class AppAdapter extends BaseAdapter {
private LayoutInflater layoutInflater;
private List<AppList> listStorage;
private Context context;
SharedPreferences.Editor editor;
SharedPreferences sharedPreferences;
int pos = 0;
public AppAdapter(Context context, List<AppList> customizedListView) {
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
listStorage = customizedListView;
this.context = context;
}
#Override
public int getCount() {
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
pos = sharedPreferences.getInt("pos", 0);
Log.e("POS",pos+"At Get cpiunt");
return listStorage.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
editor = PreferenceManager.getDefaultSharedPreferences(context).edit();
ViewHolder listViewHolder;
if (convertView == null) {
listViewHolder = new ViewHolder();
convertView = layoutInflater.inflate(R.layout.installed_app_list, parent, false);
listViewHolder.textInListView = (TextView) convertView.findViewById(R.id.list_app_name);
listViewHolder.imageInListView = (ImageView) convertView.findViewById(R.id.app_icon);
convertView.setTag(listViewHolder);
} else {
listViewHolder = (ViewHolder) convertView.getTag();
}
listViewHolder.textInListView.setText(listStorage.get(position).getName());
listViewHolder.imageInListView.setImageDrawable(listStorage.get(position).getIcon());
listViewHolder.textInListView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editor.putInt("pos", position).apply();
Log.e("Pressed", position + "");
MainActivity.installedAppAdapter.notifyDataSetChanged();
Log.e("Saved", sharedPreferences.getInt("pos", 0) + "value");
}
});
if (pos != 0) {
MainActivity.userInstalledApps.setSelection(pos);
editor.putInt("pos",0).apply();
}
return convertView;
}
static class ViewHolder {
TextView textInListView;
ImageView imageInListView;
}
}
I read many questions but did not get clear picture about how to use SmoothScrolltopostion. Though I as per info got, I captured click position and retrieved during list load to set saved position of list to user.
If I use smoothscrolltoposition, there is no action, but if I use setselection as shown in above code, I could able to see the clicked item in top of view after reloading.
Why smoothscroll is not working and m I doing correct coding for required action. pls guide.
What is want is to update total number in textview in one activity. i have a custom adapter which calls Arraylist in this activity then populates in listview, this adapter also has image view which removes the list item and does notifydatasetchanged().
this is my customadapter
private ArrayList<DataModel> dataSet;
Context mContext;
private static class ViewHolder {
TextView txtName;
TextView txtType;
Button remove;
}
public CustomAdapterForData(ArrayList<DataModel> data, Context context) {
super(context, R.layout.fields, data);
this.dataSet = data;
this.mContext=context;
}
#Override
public void onClick(View view) {
int position = (Integer) view.getTag();
Object object = getItem(position);
DataModel dataModel = (DataModel) object;
switch (view.getId())
{
case R.id.btnRemove:
remove(dataModel);
// dataSet.remove(position);
//dataSet.remove(position);
notifyDataSetChanged();
break;
}
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
DataModel dataModel = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
ViewHolder viewHolder; // view lookup cache stored in tag
final View result;
if (convertView == null) {
viewHolder = new ViewHolder();
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.fields, parent, false);
viewHolder.txtName = (TextView) convertView.findViewById(R.id.fieldName);
viewHolder.txtType = (TextView) convertView.findViewById(R.id.tvPrize);
viewHolder.remove = (Button) convertView.findViewById(R.id.btnRemove);
result=convertView;
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
result=convertView;
}
viewHolder.txtName.setText(dataModel.getFieldName());
viewHolder.txtType.setText(dataModel.getType());
viewHolder.remove.setOnClickListener(this);
viewHolder.remove.setTag(position);
return convertView;
}
after that this is my onstart()
protected void onStart() {
//if ((dataModels.size()!=0)){
ListView listView = (ListView) findViewById(R.id.listView);
adapter = new CustomAdapterForData(dataModels, getApplicationContext());
listView.setAdapter(adapter);
dataModelsnew = dataModels;
TextView tv = (TextView) findViewById(R.id.textViewTotal);
double sum = 0;
for (int i = 0 ; i < dataModelsnew.size(); i++){
sum = sum + dataModelsnew.get(i).getPrize();
}
tv.setText("Total : " + String.valueOf(Math.round(sum)*100d/100d));
//}
super.onStart();
}
what i want is populate latest value in textview which is sum of double (one element of ArrayList).Please help me to resolve this.
Thanks in advance. The variable i want to update after i remove something from list view otherwise it is working fine.
You can use DataSetObservers to detect any changes in your data:
final TextView tv = (TextView) findViewById(R.id.textViewTotal);
adapter.registerDataSetObserver(new DataSetObserver() {
#Override
public void onChanged() {
super.onChanged();
double sum = 0;
for (int i = 0 ; i < dataModels.size(); i++){
sum = sum + dataModels.get(i).getPrize();
}
tv.setText("Total : " + String.valueOf(Math.round(sum)*100d/100d));
}
});
I know questions like this one was already asked but I went through all the solutions I saw and non has worked.
This is my adapter
class Adapter extends BaseAdapter {
Activity context;
public Adapter(Activity context) {
this.context = context;
}
#Override
public int getCount() {
return Constants.CHARACTERS_NAMES.length;
}
#Override
public Object getItem(int i) {
return null;
}
#Override
public long getItemId(int i) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = context.getLayoutInflater().inflate(R.layout.list_charecter_item, null);
holder = new ViewHolder();
holder.PlayerName = (TextView) convertView.findViewById(R.id.name);
holder.PlayerName.setText(Constants.CHARACTERS_NAMES[position]);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
return convertView;
}
private class ViewHolder {
TextView PlayerName;
}
}
This is a standard adapter but when I debug it with my listview Instead of getting All my 7 items which are in Constants.CHARACTERS_NAMES I get the 4 first items right, but the I get the first item twice and then the second item.
Please help.
I have arraylist.Size of arraylist is 156.when I am running for loop it's running 156 times and it's adding all values into another arraylist.But when I am setting this arraylist into adapter adapter taking 156 items but when I am setting adapter into listview that time Listview taking only 98 items only.
When I am debugging I seen arralist size 156.and arraylist deviding into sets like
0-99
100-199
200-210
see below image :
Here my adapter taking only 0-99 items.How I can add 100-199 set Items into Listview.
Edit #1:
Adapter class :
public class AllserversListAdapter extends BaseAdapter implements OnClickListener {
Activity mActivity;
ArrayList<AllServers> mAllServersArray;
private static LayoutInflater mInflater = null;
ArrayList<MyFavorites> mFavoriteServers;
public AllserversListAdapter(Activity activity,
ArrayList<AllServers> allServers,
ArrayList<MyFavorites> favoriteServers) {
// TODO Auto-generated constructor stub
mActivity = activity;
mAllServersArray = allServers;
mFavoriteServers = favoriteServers;
mInflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return mAllServersArray.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
ViewHolder holder;
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View vi = convertView;
if (vi == null) {
/****** Inflate list items layout file for each row ( Defined below ) *******/
vi = mInflater.inflate(R.layout.servers_list_items, null);
/****** View Holder Object to contain list items file elements ******/
holder = new ViewHolder();
holder.serverName = (TextView) vi
.findViewById(R.id.server_items_country_name_textview);
holder.speed = (TextView) vi
.findViewById(R.id.server_items_speed_textview);
holder.speedTitleTextView = (TextView) vi
.findViewById(R.id.speed_title_textview);
holder.flag = (ImageView) vi.findViewById(R.id.server_flag_image);
holder.star = (ImageView) vi.findViewById(R.id.server_items_star);
holder.bodyLayout = (LinearLayout) vi
.findViewById(R.id.servere_items_boady_layout);
holder.speedBackgroundLayout = (LinearLayout) vi
.findViewById(R.id.speed_backround_layout);
holder.itemsParentLayout = (LinearLayout) vi
.findViewById(R.id.server_list_items_parentlayout);
holder.itemsParentLayout.setBackgroundColor(mActivity
.getResources().getColor(R.color.light_gray));
holder.bodyLayout.setBackgroundColor(mActivity.getResources()
.getColor(R.color.dark_gray_black));
holder.star.setOnClickListener(this);
/************ Set holder with LayoutInflater ************/
vi.setTag(holder);
holder.star.setTag(position);
holder.flag.setTag(position);
} else {
holder = (ViewHolder) vi.getTag();
}
String ip = mAllServersArray.get(position).getIp();
String country = mAllServersArray.get(position).getCountry();
String region = mAllServersArray.get(position).getRegion();
String speed = mAllServersArray.get(position).getSpeed();
String flagCountryCode = mAllServersArray.get(position)
.getCountrycode();
int ipSpeed = Integer.valueOf(speed);
holder.speedTitleTextView.setVisibility(View.GONE);
holder.speed.setText("TimeOut");
holder.speed.setTypeface(Typeface.DEFAULT);
holder.speed.setPadding(26, 0, 26, 0);
holder.speedBackgroundLayout.setBackgroundColor(mActivity
.getResources().getColor(R.color.time_out_color));
holder.serverName.setText(country + "\n" + region + "\n" + ip);
return vi;
}
}
Adding items into arraylist :
Any one give me suggest.
Try this way,hope this will help you to solve your problem.
public class AllserversListAdapter extends BaseAdapter{
Context context;
ArrayList<AllServers> allServers;
ArrayList<MyFavorites> favoriteServers;
public AllserversListAdapter(Context context,ArrayList<AllServers> allServers,ArrayList<MyFavorites> favoriteServers) {
this.context = context;
this.allServers = allServers;
this.favoriteServers = favoriteServers;
}
#Override
public int getCount() {
return mAllServersArray.size();
}
#Override
public Object getItem(int position) {
return mAllServersArray.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(R.layout.servers_list_items, null);
holder.serverName = (TextView) convertView.findViewById(R.id.server_items_country_name_textview);
holder.speed = (TextView) convertView.findViewById(R.id.server_items_speed_textview);
holder.speedTitleTextView = (TextView) convertView.findViewById(R.id.speed_title_textview);
holder.flag = (ImageView) convertView.findViewById(R.id.server_flag_image);
holder.star = (ImageView) convertView.findViewById(R.id.server_items_star);
holder.bodyLayout = (LinearLayout) convertView.findViewById(R.id.servere_items_boady_layout);
holder.speedBackgroundLayout = (LinearLayout) convertView.findViewById(R.id.speed_backround_layout);
holder.itemsParentLayout = (LinearLayout) convertView.findViewById(R.id.server_list_items_parentlayout);
holder.itemsParentLayout.setBackgroundColor(mActivity.getResources().getColor(R.color.light_gray));
holder.bodyLayout.setBackgroundColor(mActivity.getResources().getColor(R.color.dark_gray_black));
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
String ip = mAllServersArray.get(position).getIp();
holder.speedTitleTextView.setVisibility(View.GONE);
holder.speed.setText("TimeOut");
holder.speed.setTypeface(Typeface.DEFAULT);
holder.speed.setPadding(26, 0, 26, 0);
holder.speedBackgroundLayout.setBackgroundColor(mActivity
.getResources().getColor(R.color.time_out_color));
holder.serverName.setText(mAllServersArray.get(position).getCountry() + "\n" + mAllServersArray.get(position).getRegion() + "\n" + ip);
holder.flag.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// try use (position) value to check which list item click.
}
});
holder.star.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// try use (position) value to check which list item click.
}
});
return convertView;
}
class ViewHolder {
TextView serverName;
TextView speed;
TextView speedTitleTextView;
ImageView flag;
ImageView star;
LinearLayout bodyLayout;
LinearLayout speedBackgroundLayout;
LinearLayout itemsParentLayout;
}
}
I have a custom ListView with a TextView and a CheckBox. I also have a custom SimpleAdapter in which I override the getView() method and am able to retrieve clicks on the TextView and CheckBox changes. My problem is that I don't know how to get the correct clicked ListItem or CheckBox inside the OnCheckedChanged or OnClick.
UPDATE added whole CustomAdapter class:
public class CustomAdapter extends SimpleAdapter {
private LayoutInflater mInflater;
private List<HashMap<String, String>> mItems = null;
private Context mContext;
private int mPosicion;
public CustomAdapter(Context context, List<HashMap<String, String>> items, int resource, String[] from, int[] to) {
super(context, items, resource, from, to);
mInflater = LayoutInflater.from(context);
mItems = items;
mContext = context;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
mPosicion = position;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.custom_row_view, null);
holder = new ViewHolder();
holder.chkbxEstado = (CheckBox) convertView.findViewById(R.id.chkbxCompletado);
holder.txtTextoAgenda = (TextView) convertView.findViewById(R.id.txtTextoLista);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.txtTextoAgenda.setText(mItems.get(position).get("descripcion"));
convertView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Log.i("Posicion",""+mPosicion);//I don't know how to retrieve clicked position
}
});
holder.chkbxEstado.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Log.i("Posicion",""+mPosicion);//Or checked
}
});
return convertView;
}
private static class ViewHolder {
TextView txtTextoAgenda;
CheckBox chkbxEstado;
}
}
Any help is appreciated.
I found a solution. If anybody knows a better one, please let me know. Working CustomAdapter class:
public class CustomAdapter extends SimpleAdapter {
private LayoutInflater mInflater;
private List<HashMap<String, String>> mItems = null;
private Context mContext;
private OnClickListener mClick;
private OnCheckedChangeListener mChecked;
public CustomAdapter(Context context, List<HashMap<String, String>> items, int resource, String[] from, int[] to) {
super(context, items, resource, from, to);
mInflater = LayoutInflater.from(context);
mItems = items;
mContext = context;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.custom_row_view, null);
holder = new ViewHolder();
holder.chkbxEstado = (CheckBox) convertView.findViewById(R.id.chkbxCompletado);
holder.txtTextoAgenda = (TextView) convertView.findViewById(R.id.txtTextoLista);
holder.posicion = position; //Add the new position into the holder for each row.
convertView.setTag(holder);
mClick = new OnClickListener() {
#Override
public void onClick(View v) {
ViewHolder viewHolder = getViewHolder(v); //Get the ViewHolder for the clicked row.
Log.i("Posicion",""+v.posicion);
}
};
mChecked = new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
ViewHolder viewHolder = getViewHolder(buttonView); //Get the ViewHolder for the clicked CheckBox
Log.i("Posicion",""+viewHolder.posicion);
}
};
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.txtTextoAgenda.setText(mItems.get(position).get("descripcion"));
convertView.setOnClickListener(mClick);
holder.chkbxEstado.setOnCheckedChangeListener(mChecked);
return convertView;
}
public ViewHolder getViewHolder(View v){ //This method returns the ViewHolder stored in the tag if available, if not it recursively checks the parent's tag.
if(v.getTag() == null){
return getViewHolder((View)v.getParent());
}
return (ViewHolder)v.getTag();
}
private static class ViewHolder {
TextView txtTextoAgenda;
CheckBox chkbxEstado;
int posicion; //Added position attribute to ViewHolder class.
}
}
Edited to re-use onClickListener() and onCheckedChangedListener() instances.
The OP's solution works. But if you are extending a CursorAdapter, the position parameter does not exist in newView() and bindView(). What to do?
They do supply a Cursor. Use cursor.getPosition(). It does the same thing.