i have an ArrayList and one BaseAdapter to inflate my layout, when one array from arraylist is empty, the value on layout show text "NaN%" it is so annoying, i want handle an empty array from my arraylist with setText as (0%). so how to handle empty value from ArrayList in baseadapter android? this is my code :
public class TabelNABDetail extends BaseAdapter {
Context context;
ArrayList<NabDetail> listData;//stack
int count;
public TabelNABDetail(Context context, ArrayList<NabDetail>listData){
this.context=context;
this.listData=listData;
this.count=listData.size();
}
#Override
public int getCount() {
// TODO Auto-generated method stub
// return count;
return (listData==null) ? 0 : listData.size();
}
#Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(int position, View v, ViewGroup parent) {
// TODO Auto-generated method stub
View view= v;
ViewHolder holder= null;
if(view==null){
LayoutInflater inflater=(LayoutInflater)context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
view= inflater.inflate(R.layout.layout_tabel_detailnab, null);
holder=new ViewHolder();
holder.persen_hke1=(TextView)view.findViewById(R.id.varpersenke1);
view.setTag(holder);
}
else{
holder=(ViewHolder)view.getTag();
}
if (position==0) {
holder.persen_hke1.setText("0%");
} else {
holder.persen_hke1.setText(String.format("%.5f",listData.get(position).getpersen_hke1())+"%");
}
return view;
}
static class ViewHolder{
TextView ,persen_hke1;
}
i want to handle if listData.get(position).getpersen_hke1() is empty value, it gonna be holder.persen_hke1.setText("0%") how to solve it? thanks before
Change your getView() like this
#Override
public View getView(int position, View v, ViewGroup parent) {
// TODO Auto-generated method stub
View view= v;
ViewHolder holder= null;
if(view==null){
LayoutInflater inflater=(LayoutInflater)context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
view= inflater.inflate(R.layout.layout_tabel_detailnab, null);
holder=new ViewHolder();
holder.persen_hke1=(TextView)view.findViewById(R.id.varpersenke1);
view.setTag(holder);
} else {
holder=(ViewHolder)view.getTag();
}
if (position==0) {
holder.persen_hke1.setText("0%");
} else {
**/////// these lines are extra lines add into your code**
NabDetail nabDetail = listData.get(position)
if(!nabDetail.getpersen_hke1().equals(""))
holder.persen_hke1.setText(String.format("%.5f",listData.get(position).getpersen_hke1())+"%");
} else {
holder.persen_hke1.setText("0%");
}
}
return view;
}
Related
Hi guys I need your help. I want to set listner imageview and button in gridview and when I click to imageview app should show dialog with image and when I click to button app should do something.
like this:
enter image description here
You need to create Custom Adapter for your GridView with Custom UI with ImageView and Button, and then inside the getVieew(){} you need to make click listener for Image and button, and there you can do your action with the help of position with your array list.
here I am posting demo adapter for your reference:
here you can change TextView to Button.
public class CustomAdapter extends BaseAdapter{
String [] result;
Context context;
int [] imageId;
private static LayoutInflater inflater=null;
public CustomAdapter(MainActivity mainActivity, String[] prgmNameList, int[] prgmImages) {
// TODO Auto-generated constructor stub
result=prgmNameList;
context=mainActivity;
imageId=prgmImages;
inflater = ( LayoutInflater )context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return result.length;
}
#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;
}
public class Holder
{
TextView tv;
ImageView img;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Holder holder=new Holder();
View rowView;
rowView = inflater.inflate(R.layout.program_list, null);
holder.tv=(TextView) rowView.findViewById(R.id.textView1);
holder.img=(ImageView) rowView.findViewById(R.id.imageView1);
holder.tv.setText(result[position]);
holder.img.setImageResource(imageId[position]);
rowView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(context, "You Clicked "+result[position], Toast.LENGTH_LONG).show();
}
});
return rowView;
}
}
ref:-https://caveofprogramming.com/guest-posts/custom-gridview-with-imageview-and-textview-in-android.html
I have implemented a ListView. From ListView I am adding a products to cart. My problem is if i add a first product i am changing a button name Addtocart to Added if i scroll ListView 4th position product button name is changing to Added.
How can I resolve this ?
Here my code:
holderForGrid.AddtoCart.setTag(position);
holderForGrid.AddtoCart.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
holderForGrid.AddtoCart.setText("Added");
}
});
Adapter Class:
class ListAdapter extends BaseAdapter {
public Context context;
public ListAdapter(Context a,List<BusinessCatalogVariables> listDataHeader) {
this.listDataHeader = listDataHeader;
context = a;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return catalogList.size();
}
#Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
#Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
#TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
#Override
public View getView(final int position, View convertView, ViewGroup arg2) {
final ViewHolderGrid holderForGrid;
if (convertView == null) {
convertView = getLayoutInflater().inflate(R.layout.catalog_list_item, null);
holderForGrid = new ViewHolderGrid(convertView);
convertView.setTag(holderForGrid);
} else {
holderForGrid = (ViewHolderGrid) convertView.getTag();
}
finalCatalogVariables Catalog = catalogList.get(position);
holderForGrid.AddtoCart.setClickable(false);
holderForGrid.AddtoCart.setTag(position);
holderForGrid.AddtoCart.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
int position1=(Integer)arg0.getTag();
AddedProduct = (String) holderForGrid.CatalogHeader.getText();
holderForGrid.AddtoCart.setText("Added");
}
});
return convertView;
}
private class ViewHolderGrid {
Button AddtoCart = null;
ViewHolderGrid(View convertView) {
AddtoCart = (Button) convertView.findViewById(R.id.btn_AddtoCart);
}
}
}
create boolean variable added in finalCatalogVariables class.
and create getter,setter method for this variable.
like,
class finalCatalogVariables{
// other variable and methods
boolean added;
public boolean isAdded()
{
return this.added;
}
public void setAdded(boolean added){
this.added = added;
}
}
then in getView method on click of holderForGrid.AddtoCart
public View getView(final int position, View convertView, ViewGroup arg2) {
final ViewHolderGrid holderForGrid;
if (convertView == null) {
convertView = getLayoutInflater().inflate(R.layout.catalog_list_item, null);
holderForGrid = new ViewHolderGrid(convertView);
convertView.setTag(holderForGrid);
} else {
holderForGrid = (ViewHolderGrid) convertView.getTag();
}
finalCatalogVariables catalog = catalogList.get(position);
if(catalog.isAdded()){
holderForGrid.AddtoCart.setText("Added");
}else{
holderForGrid.AddtoCart.setText("Add to cart");
}
holderForGrid.AddtoCart.setClickable(false);
holderForGrid.AddtoCart.setTag(position);
holderForGrid.AddtoCart.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
catalog.setAdded(true);
int position1=(Integer)arg0.getTag();
AddedProduct = (String) holderForGrid.CatalogHeader.getText();
// holderForGrid.AddtoCart.setText("Added");
}
});
return convertView;
}
Override below mentioned two methods in your adapter class.
Override
public int getViewTypeCount() {
return getCount();
}
#Override
public int getItemViewType(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (inflater == null)
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
convertView = inflater.inflate(R.layout.rowlayout, null);
if (imageLoader == null)
imageLoader = Session.getInstance().getImageLoader();
//write your declaration code
List m = Yourarray.get(position);
// thumbnail image
convertView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//your code
int position1=(Integer)arg0.getTag();
AddedProduct = (String) holderForGrid.CatalogHeader.getText();
holderForGrid.AddtoCart.setText("Added");
}
});
return convertView;
}
I think the problem is that if you change the button on a view and then scroll far enough for the view to disappear, Android will assign the view position to another item in the listView.
You need to maintain somehow the status "added" or not of each product and let your adapter decide what label to display based on that.
I have a listview and I am populating that list view from a ArrayList of custom objects.
This is the custom adapter code.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
final int a=position;
FileHolder holder=null;
View row = convertView;
if(row==null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(textViewResourceId, parent, false);
holder=new FileHolder();
holder.file=files.get(position);
holder.deleteButton=(ImageButton) row.findViewById(R.id.deleteButton);
holder.downloadButton=(ImageButton) row.findViewById(R.id.downloadButton);
holder.deleteButton.setTag(holder.file);
holder.downloadButton.setTag(holder.file);
holder.fileName= (TextView) row.findViewById(R.id.fileName);
holder.fileName.setText(holder.file.getFileName());
}
else
{
holder=(FileHolder) row.getTag();
}
fPOJO=holder.file;
final AsyncCall call=new AsyncCall();
holder.downloadButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
MODE=0;
//call.execute(MODE);
}
});
holder.deleteButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getContext(), "Delete File "+(String.valueOf(a))+" "+fPOJO.getFileName(), Toast.LENGTH_LONG).show();
MODE=1;
//call.execute(MODE);
}
});
return row;
}
public static class FileHolder{
FilesPOJO file;
TextView fileName;
ImageButton downloadButton;
ImageButton deleteButton;
}
There are two buttons one for file delete and other for file download. I have implemented on click listeners for these two buttons. The problem is when I click any button the fileName in the Toast message is different from the file name that I see on the screen display. For ex: I have 5 files with names
delete.png
upload.png
share.png
referesh.png
copy.png
and now when I click delete button corresponding to referesh.png file the toast pop ups and in that the file name is different than referesh.png.
how to resolve this issue.
EDITS:
This is how I set up this adapter
FilesAdapter adapter=new FilesAdapter(rootView.getContext(), R.layout.file_list_view_item_row, Application.files);
View header = (View) View.inflate(rootView.getContext(), R.layout.files_list_view_header_row, null);
fileListHeading.inflate(rootView.getContext(), R.layout.files_list_view_header_row, null);
filesListView.addHeaderView(header);
filesListView.setAdapter(adapter);
I think the problem is with your fPOJO variable when onClick is called it may have different value because after every getView you're changing it's value.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
final int a=position;
final FileHolder holder;
View row = convertView;
if(row==null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(textViewResourceId, parent, false);
holder=new FileHolder();
holder.file=files.get(position);
holder.deleteButton=(ImageButton) row.findViewById(R.id.deleteButton);
holder.downloadButton=(ImageButton) row.findViewById(R.id.downloadButton);
holder.deleteButton.setTag(holder.file);
holder.downloadButton.setTag(holder.file);
holder.fileName= (TextView) row.findViewById(R.id.fileName);
holder.fileName.setText(holder.file.getFileName());
}
else
{
holder=(FileHolder) row.getTag();
}
final AsyncCall call=new AsyncCall();
holder.downloadButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
MODE=0;
//call.execute(MODE);
}
});
holder.deleteButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getContext(), "Delete File "+(String.valueOf(a))+" "+holder.fileName, Toast.LENGTH_LONG).show();
MODE=1;
//call.execute(MODE);
}
});
return row;
}
Just make holder final and get you're file name from it.
You store your list item in holder, this is your problem, ListView adapter creates as much convertViews as needed to fill the screen so if you have list of 10+ elements, only few will be added to row.
First of all don't store your list element in holder, you gave adapter method called getItem(positoin) to get it.
In your holder view store only references to layout views (TextView, Buttons etc) and (if(row == null) call findViewById on them and setTag.
Then set text, listeners outside the if statement, like so:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
final int a=position;
FileHolder holder=null;
View row = convertView;
if(row==null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(textViewResourceId, parent, false);
holder=new FileHolder();
holder.deleteButton=(ImageButton) row.findViewById(R.id.deleteButton);
holder.downloadButton=(ImageButton) row.findViewById(R.id.downloadButton);
holder.fileName= (TextView) row.findViewById(R.id.fileName);
row.setTag(holder);
}
else
{
holder=(FileHolder) row.getTag();
}
fPOJO= getItem(position);
final AsyncCall call=new AsyncCall();
holder.downloadButton.setOnClickListener(null);
holder.downloadButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
MODE=0;
//call.execute(MODE);
}
});
holder.deleteButton.setOnClickListener(null);
holder.deleteButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getContext(), "Delete File "+(String.valueOf(a))+" "+fPOJO.getFileName(), Toast.LENGTH_LONG).show();
MODE=1;
//call.execute(MODE);
}
});
return row;
}
public static class FileHolder{
TextView fileName;
ImageButton downloadButton;
ImageButton deleteButton;
}
Also you can do some code optimalization, for example don't initialize LayoutInflater in getView() method, you can do it in adapter Constructor
I want to create an ExpandableListView with custom adapter, however it only show my title group and when I click on the title (to view the children items) it doesnt do anything
(I want to have only one group item, therefore the item group count is 1).
Please help me to find where am I wrong, thanks alot.
This is my adapter:
class chatListAdapter extends BaseExpandableListAdapter {
private String objectsIdInsideAdapter;
private List<String> commentsInsideAdapter = new ArrayList<String>();
private List<String> FacebookIdInsideAdapter=new ArrayList<String>();
private List<String> NamesInsideAdapter =new ArrayList<String>();
private Context mContext;
chatListAdapter(String objectId,Context ctx,List<String> comments,List<String> face,List<String> names){
objectsIdInsideAdapter=objectId;
mContext=ctx;
commentsInsideAdapter=comments;
FacebookIdInsideAdapter=face;
NamesInsideAdapter=names;
}
#Override
public Object getChild(int arg0, int arg1) {
// TODO Auto-generated method stub
return NamesInsideAdapter.get(arg1);
}
#Override
public long getChildId(int arg0, int arg1) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View v,
ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater = getLayoutInflater();
if(v==null){
v = inflater.inflate(R.layout.chat_item, parent, false);
}
String comment =commentsInsideAdapter.get(position);
Button sendCommentBtn =(Button) v.findViewById(R.id.chatitemSendComment);
TextView commentView= (TextView) v.findViewById(R.id.chateitemViewComment);
commentView.setText(comment);
TextView commentName =(TextView) v.findViewById(R.id.chatitemfreindName);
commentName.setText(NamesInsideAdapter.get(position));
EditText commentText= (EditText) v.findViewById(R.id.chatitemEnterComment);
commentText.setVisibility(View.GONE);
sendCommentBtn.setVisibility(View.GONE);
return v;
}
#Override
public int getChildrenCount(int arg0) {
// TODO Auto-generated method stub
return NamesInsideAdapter.size();
}
#Override
public Object getGroup(int arg0) {
// TODO Auto-generated method stub
return null;
}
#Override
public int getGroupCount() {
// TODO Auto-generated method stub
return 1;
}
#Override
public long getGroupId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
ViewGroup parent) {
// TODO Auto-generated method stub
View v = convertView;
if (v == null) {
LayoutInflater inflater = getLayoutInflater();
v = inflater.inflate(R.layout.chat_item_group_layout,parent, false);
}
TextView groupName = (TextView) v.findViewById(R.id.groupName);
TextView groupDescr = (TextView) v.findViewById(R.id.groupDescr);
groupName.setText("view comments");
groupDescr.setText("groupDSCR");
return v;
}
#Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean isChildSelectable(int arg0, int arg1) {
// TODO Auto-generated method stub
return false;
}
and this is the code in the main Activity:
ExpandableListView chat =(ExpandableListView) v.findViewById(R.id.ExpList);
String faceids =facebookIdChatInside.get(position);
String comments = commentInside.get(position);
String namesChat =nameChatInside.get(position);
List<String> chatItems = new ArrayList<String>(Arrays.asList(comments.split(",")));
List<String> facebookids = new ArrayList<String>(Arrays.asList(faceids.split(",")));
List<String> namesList = new ArrayList<String>(Arrays.asList(namesChat.split(",")));
chatItems.add(" ");
facebookids.add(id);
namesList.add(name);
chatListAdapter chatA =new chatListAdapter(objectsId.get(position),profileContext,chatItems,facebookids,namesList);
chat.setAdapter(chatA);
I checked it, and those 3 Lists are not empty.
Reading your code i found 3 points that aren't so clear to me:
getGroup you return null? Why don't u return the real Object
getChildId you should return an unique id identifying your child
hasStableIds. You return false. You dont need to override this method.
If you want to have more information on ExpandableListView give a look at my blog here
You are using the ExpandableListView like a simple ListView why dont u consider to you a ListView?
I have three arrays and I want to populate list view with them, till now I managed to populate list view with one array and array adapter, but now I need to put all three arrays in every item of list view.
Simple code would help lots.
Thank you in advance.
Join the three array into a single one and Populate it as before
Refer this code to create Custom Adapter
public class ListViewAdapter extends BaseAdapter {
private ArrayList<TimeRecord> record = new ArrayList<TimeRecord>();
TextView txtTime, txtNote;
public ListViewAdapter() {
record.add(new TimeRecord("38:23", "Feeling good!"));
record.add(new TimeRecord("49:01", "Tired. Needed more caffeine"));
record.add(new TimeRecord("26:21", "I’m rocking it!"));
record.add(new TimeRecord("29:42",
"Lost some time on the hills, but pretty good."));
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return record.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return getItem(position);
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public View getView(int position , View convertView , ViewGroup parent) {
// TODO Auto-generated method stub
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
convertView = inflater.inflate(R.layout.row_xml, parent, false);
}
TimeRecord time = record.get(position);
txtTime = (TextView) convertView.findViewById(R.id.txtTime);
txtTime.setText(time.getTime());
txtNote = (TextView) convertView.findViewById(R.id.txtNote);
txtNote.setText(time.getNote());
return convertView;
}
}