ListView items repeated issue - android

I have a multicolumn_listview. Some rows repeat in listview. i looked at all solution about this problem.
listview items image
Listview height not wrap_content
My holder class static
I created all views in if(convertView==null) block in getview method.
but the problem hasn't solved yet.. please help me..
here is my adapter class;
public class ListViewAdapterCurrentList extends BaseAdapter
{
public ArrayList<HashMap<String, String>> list;
boolean isDetail = false;
private String currentNo;
private String currentCode;
private String currentName;
private String date;
private String status;
private Activity activity;
private Boolean isPlan = true;
public ListViewAdapterCurrentList(Activity activity, ArrayList<HashMap<String, String>> list, boolean isPlan) {
super();
this.activity = activity;
this.list = list;
this.isPlan = isPlan;
}
#Override
public int getCount()
{
return list.size();
}
#Override
public Object getItem(int position)
{
return list.get(position);
}
#Override
public long getItemId(int position)
{
return 0;
}
static class ViewHolder
{
ImageView currentDetail;
TextView currentNo;
TextView currentCode;
TextView currentName;
TextView date;
TextView durum;
LinearLayout linearLayoutCurrentBase;
LinearLayout linearLayoutCurrentDetail;
TextView currentAddress;
TextView currentPhone;
TextView currentManager;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
final ViewHolder holder;
LayoutInflater inflater = activity.getLayoutInflater();
final HashMap<String, String> map = list.get(position);
if (convertView==null)
{
Log.d("CL:getView()", "Fetching Row: " + position);
convertView = inflater.inflate(R.layout.currentlistitems, parent, false);
holder = new ViewHolder();
holder.currentDetail = (ImageView) convertView.findViewById(R.id.imageViewCurrentListDetail);
holder.currentNo = (TextView) convertView.findViewById(R.id.textViewCurrentListNo);
holder.currentCode = (TextView) convertView.findViewById(R.id.textViewCurrentListCode);
holder.currentName = (TextView) convertView.findViewById(R.id.textViewCurrentListName);
holder.date = (TextView) convertView.findViewById(R.id.textViewCurrentListDate);
holder.durum = (TextView) convertView.findViewById(R.id.textViewCurrentListStatus);
holder.linearLayoutCurrentBase = (LinearLayout) convertView.findViewById(R.id.layoutBaseCurrentList);
holder.linearLayoutCurrentDetail = (LinearLayout) convertView.findViewById(R.id.layoutDetailCurrentList);
holder.currentAddress = (TextView) convertView.findViewById(R.id.textViewCurrentAddress);
holder.currentPhone = (TextView) convertView.findViewById(R.id.textViewCurrentPhone);
holder.currentManager = (TextView) convertView.findViewById(R.id.textViewCurrentManager);
convertView.setTag(holder);
} else
{
holder = (ViewHolder) convertView.getTag();
}
currentNo = map.get("CariNo");
currentCode = map.get("CariKod");
currentName = map.get("CariUnvan");
date = map.get("Tarih");
status = map.get("Durum");
holder.currentDetail.setBackgroundResource(R.drawable.box_plus);
holder.currentNo.setText(currentNo);
holder.currentCode.setText(currentCode);
holder.currentName.setText(currentName);
holder.date.setText(date);
holder.durum.setText(status);
if (!isPlan)
{
holder.durum.setVisibility(View.GONE);
holder.date.setVisibility(View.GONE);
holder.currentName.setWidth(300);
}
initDetailInfo(holder.currentAddress, holder.currentPhone, holder.currentManager, currentNo);
holder.currentDetail.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
if (!isDetail)
{
holder.linearLayoutCurrentDetail.setVisibility(View.VISIBLE);
holder.currentDetail.setBackgroundResource(R.drawable.box_delete_expand);
isDetail = true;
} else
{
holder.linearLayoutCurrentDetail.setVisibility(View.GONE);
holder.currentDetail.setBackgroundResource(R.drawable.box_plus);
isDetail = false;
}
}
});
return convertView;
}

Edited since it apparently wasn't clear:
if (convertView==null)
{
Log.d("CL:getView()", "Fetching Row: " + position);
convertView = inflater.inflate(R.layout.currentlistitems, parent, false);
holder = new ViewHolder();
holder.currentDetail = (ImageView) convertView.findViewById(R.id.imageViewCurrentListDetail);
holder.currentNo = (TextView) convertView.findViewById(R.id.textViewCurrentListNo);
holder.currentCode = (TextView) convertView.findViewById(R.id.textViewCurrentListCode);
holder.currentName = (TextView) convertView.findViewById(R.id.textViewCurrentListName);
holder.date = (TextView) convertView.findViewById(R.id.textViewCurrentListDate);
holder.durum = (TextView) convertView.findViewById(R.id.textViewCurrentListStatus);
holder.linearLayoutCurrentBase = (LinearLayout) convertView.findViewById(R.id.layoutBaseCurrentList);
holder.linearLayoutCurrentDetail = (LinearLayout) convertView.findViewById(R.id.layoutDetailCurrentList);
holder.currentAddress = (TextView) convertView.findViewById(R.id.textViewCurrentAddress);
holder.currentPhone = (TextView) convertView.findViewById(R.id.textViewCurrentPhone);
holder.currentManager = (TextView) convertView.findViewById(R.id.textViewCurrentManager);
convertView.setTag(holder);
} else
{
holder = (ViewHolder) convertView.getTag();
}
right under the above before any work is done to them like adding children to the LinearLayouts:
holder.linearLayoutCurrentBase.removeAllViews();
holder.linearLayoutCurrentDetail.removeAllViews();
My guess is since you aren't doing that with the Viewgroups it never replaces the children. Let me know if that helps.
EDIT:
I just edited the answer for hopefully the last time. I do apologize for using the wrong method for removing the children of the ViewGroups. Now it should make sense. Look at my comment to understand why it duplicates your Views.

Related

How to change my TextView value by clicking ImageButton in ListView

This is my listview, my problem is why I'm trying to click on my ImageButton(in listview) but the textview(in listview) never change the value that what I want. It's take my whole morning, can someone please help me, Thanks.
public class ViewAdapter extends BaseAdapter implements Filterable {
LayoutInflater mInflater;
public ViewAdapter() {
mInflater = LayoutInflater.from(getActivity());
}
#Override
public int getCount() {
return pl.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) {
if (convertView == null) {
convertView = mInflater.inflate(R.layout.listproduct, null);
}
final TextView ProductCode = (TextView) convertView.findViewById(R.id.productcode);
ProductCode.setText("" + pl.get(position).getProductCode());
final TextView Qty = (TextView) convertView.findViewById(R.id.qty);
Qty.setText("" + pl.get(position).getQty());
final TextView Description = (TextView) convertView.findViewById(R.id.description);
Description.setText("" + pl.get(position).getProductName());
final String p = Description.getText().toString();
final TextView Price = (TextView) convertView.findViewById(R.id.price);
Price.setText("" + pl.get(position).getProductPrice());
final ImageButton minus = (ImageButton) convertView.findViewById(R.id.minus);
minus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Qty.setText("testing")// textview never change to "testing"
}
});
notifyDataSetChanged();
return convertView;
}
Please try your code with holder just like follows
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_entry, null);
holder = new ViewHolder();
holder.nameTextView = (TextView) convertView.findViewById(R.id.person_name);
holder.surnameTextView = (TextView) convertView.findViewById(R.id.person_surname);
holder.personImageView = (ImageView) convertView.findViewById(R.id.person_image);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
Person person = getItem(position);
holder.nameTextView.setText(person.getName());
holder.surnameTextView.setText(person.getSurname());
//holder.personImageView.setImageBitmap(person.getImage());
return convertView;
}
Then click as follows
holder.personImageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
holder.nameTextView.setText("testing")// textview never change to "testing"
}
});
Use this code :
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = mInflater.inflate(R.layout.listproduct, null);
}
final TextView ProductCode = (TextView) convertView.findViewById(R.id.productcode);
ProductCode.setText("" + pl.get(position).getProductCode());
final TextView Qty = (TextView) convertView.findViewById(R.id.qty);
final TextView Description = (TextView) convertView.findViewById(R.id.description);
Description.setText("" + pl.get(position).getProductName());
final String p = Description.getText().toString();
final TextView Price = (TextView) convertView.findViewById(R.id.price);
Price.setText("" + pl.get(position).getProductPrice());
ImageButton minus = (ImageButton) convertView.findViewById(R.id.minus);
minus.setTag(position);
minus.setOnClickListener(clickButton);
return convertView;
}
private View.OnClickListener clickButton = new View.OnClickListener() {
#Override
public void onClick(View v) {
Object tag =v.getTag();
if(tag!=null)
{
int pos = (Integer)tag;
Qty.setText(pl.get(position).getQty());
notifyDataSetChanged();
}
}
} ;
As far as i understand from your question this cannot be done because once you populate the data in custom listView you have recreate it or call the custom listView again with text display you want to change!
Basically you should use RecyclerView,but if you still want to use the listView, use the view-holder approach as in this link below:
Optimize list view
And you should never ever call notifyDataSetChanged() in getView method as its called multiple time as your listview scrolls

Getting duplicate values in ListView android while scrolling

Whenever I scroll down, it shows me duplicate values.I have used setTag() method to avoid duplicate values but still duplicate values are there. Please provide help. I have added my Adapter class as follows :
CustomAdapter.java
#Override
public int getCount() {
return searchVedBeanList.size();
}
#Override
public Object getItem(int position) {
if (searchVedBeanList != null && searchVedBeanList.size() != 0 && searchVedBeanList.size() > position) {
return searchVedBeanList.get(position);
} else {
return null;
}
}
#Override
public long getItemId(int position) {
return position;
}
#SuppressWarnings("deprecation")
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
final VedBean vedBean = (VedBean) getItem(position);
if (convertView == null) {
LayoutInflater li = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
holder = new ViewHolder();
convertView = li.inflate(R.layout.search_ved_list_item, parent, false);
holder.mantraSansTextView = (TextView) convertView.findViewById(R.id.mantraSansTextView);
holder.vedTextView = (TextView) convertView.findViewById(R.id.vedTextView);
holder.commentratorTextView = (TextView) convertView.findViewById(R.id.commentratorTextView);
holder.rishiDevtaLinearLayout = (LinearLayout) convertView.findViewById(R.id.rishiDevtaLinearLayout);
holder.rishiTextView = (TextView) convertView.findViewById(R.id.rishiTextView);
holder.devtaTextView = (TextView) convertView.findViewById(R.id.devtaTextView);
// initialize Atharvaved components
holder.atharvavedVedLinearLayoutSearch = (LinearLayout) convertView.findViewById(R.id.atharvavedVedLinearLayoutSearch);
holder.kandTextViewAtharvaSearch = (TextView) convertView.findViewById(R.id.kandTextViewAtharvaSearch);
holder.suktaTextViewAtharvaSearch = (TextView) convertView.findViewById(R.id.suktaTextViewAtharvaSearch);
holder.mantraNumTextViewAtharvaSearch = (TextView) convertView.findViewById(R.id.mantraNumTextViewAtharvaSearch);
// initialize Yajurved components
holder.yajurVedLinearLayoutSearch = (LinearLayout) convertView.findViewById(R.id.yajurVedLinearLayoutSearch);
holder.adhyayTextViewYajurSearch = (TextView) convertView.findViewById(R.id.adhyayTextViewYajurSearch);
holder.mantraNumTextViewYajurSearch = (TextView) convertView.findViewById(R.id.mantraNumTextViewYajurSearch);
// initialize Samved components
holder.samVedLinearLayoutSearch = (LinearLayout) convertView.findViewById(R.id.samVedLinearLayoutSearch);
holder.archikTextViewSamSearch = (TextView) convertView.findViewById(R.id.archikTextViewSamSearch);
holder.adhyayTextViewSamSearch = (TextView) convertView.findViewById(R.id.adhyayTextViewSamSearch);
holder.dashatiTextViewSamSearch = (TextView) convertView.findViewById(R.id.dashatiTextViewSamSearch);
holder.mantraNumTextViewSamSearch = (TextView) convertView.findViewById(R.id.mantraNumTextViewSamSearch);
// initialize Rigved components
holder.rigVedLinearLayoutSearch = (LinearLayout) convertView.findViewById(R.id.rigVedLinearLayoutSearch);
holder.mandalTextViewRigSearch = (TextView) convertView.findViewById(R.id.mandalTextViewRigSearch);
holder.suktaTextViewRigSearch = (TextView) convertView.findViewById(R.id.suktaTextViewRigSearch);
holder.mantraNumTextViewRigSearch = (TextView) convertView.findViewById(R.id.mantraNumTextViewRigSearch);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
char charArr[] = vedBean.getMantraSans().toCharArray();
for (int i = 0; i < charArr.length; i++) {
holder.mantraSansTextView.setText(holder.mantraSansTextView.getText().toString() + charArr[i]);
}
String vedStr = "<b>"+UtilityConstant.STR_VED+" : "+"</b>"+UtilityConstant.getVedValueFromId(vedBean.getVedId());
holder.vedTextView.setText(Html.fromHtml(vedStr));
String commentatorStr = "<b>"+UtilityConstant.STR_COMMENTATOR+" : "+"</b>"+UtilityConstant.getCommentratorValueFromId(vedBean.getCommentratorId());
holder.commentratorTextView.setText(Html.fromHtml(commentatorStr));
holder.rishiTextView.setVisibility(View.GONE);
return convertView;
}

ListView with section headers repeats items when scrolling

I have finally had some luck getting section headers to appear by creating a switch block in my getView(). In doing so I have created a problem with my adapter because now my ListView repeats the top items over and over again. I have found other people with a similar problems, but they were solved by adding convertView.setTag(holder); I already have this, so I believe my problem is something with the way I have set up my switch block. Maybe a syntax problem that is causing things not to line up correctly.
Any help would be appreciated. Here is my adapter:
public class PlayerAdapter extends BaseAdapter {
private Context mContext;
private static final int TYPE_ITEM = 0;
private static final int TYPE_SEPARATOR = 1;
private ArrayList<Player> mPlayers = new ArrayList<>();
private TreeSet<Integer> sectionHeader = new TreeSet<>();
public PlayerAdapter(Context context, ArrayList<Player> players) {
mContext = context;
mPlayers = players;
}
public void addItem(final Player player) {
mPlayers.add(player);
notifyDataSetChanged();
}
public void addSectionHeaderItem(final Player player) {
mPlayers.add(player);
sectionHeader.add(mPlayers.size() -1);
notifyDataSetChanged();
}
#Override
public int getItemViewType(int position) {
return sectionHeader.contains(position) ? TYPE_SEPARATOR : TYPE_ITEM;
}
#Override
public int getViewTypeCount() {
return 2;
}
#Override
public int getCount() {
return mPlayers.size();
}
#Override
public Object getItem(int position) {
return mPlayers.get(position);
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public boolean isEnabled(int position) {
int rowType = getItemViewType(position);
if(rowType == TYPE_SEPARATOR) {
return false;
}
return true;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = new ViewHolder();
int rowType = getItemViewType(position);
if (convertView == null) {
Player player = mPlayers.get(position);
switch (rowType){
case TYPE_ITEM:
convertView = LayoutInflater.from(mContext).inflate(R.layout.player_list_item_layout, null);
holder.playerNameTextView = (TextView) convertView.findViewById(R.id.playerNameTextView);
holder.playerValueTextView = (TextView) convertView.findViewById(R.id.playerValueTextView);
holder.remainingCapTextView = (TextView) convertView.findViewById(R.id.remainingCapTextView);
holder.username = (TextView) convertView.findViewById(R.id.opponentUsername);
holder.status = (TextView) convertView.findViewById(R.id.statusTextView);
holder.vsTeamAbbrev = (TextView) convertView.findViewById(R.id.vsTeam);
holder.resultsTextView = (TextView) convertView.findViewById(R.id.resultsTextView);
DecimalFormat formatter = new DecimalFormat("$#,###");
holder.playerNameTextView.setText(player.getName());
holder.playerValueTextView.setText(formatter.format(Double.parseDouble(player.getValue())));
holder.remainingCapTextView.setText(formatter.format(Double.parseDouble(player.getCap())));
holder.username.setText(player.getUsername());
holder.status.setText(player.getStatus());
holder.vsTeamAbbrev.setText(player.getVsTeamAbbrev());
holder.resultsTextView.setText(player.getResultsTextView());
if (player.isMatchMade()) {
holder.status.setVisibility(View.VISIBLE);
}
convertView.setTag(holder);
break;
case TYPE_SEPARATOR:
convertView = LayoutInflater.from(mContext).inflate(R.layout.player_section_header, null);
holder.playerNameTextView = (TextView) convertView.findViewById(R.id.lastPlayer);
holder.lastPlayerDate = (TextView) convertView.findViewById(R.id.lastPlayerDate);
holder.playerNameTextView.setText(player.getName());
holder.lastPlayerDate.setText(player.getLastPlayerDate());
convertView.setTag(holder);
break;
}
} else {
holder = (ViewHolder) convertView.getTag();
}
return convertView;
}
private static class ViewHolder {
//item views
TextView playerValueTextView;
TextView playerNameTextView;
TextView remainingCapTextView;
TextView username;
TextView status;
TextView vsTeamAbbrev;
TextView resultsTextView;
//section header views
TextView lastPlayer;
TextView lastPlayerDate;
}
}
This turned out to be a pretty simple mistake. I had to move the code that populates my views down below the else clause. This way if convert view is not null, it can still gettag() and populate the data.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = new ViewHolder();
Player player = mPlayers.get(position);
int rowType = getItemViewType(position);
if (convertView == null) {
switch (rowType){
case TYPE_ITEM:
convertView = LayoutInflater.from(mContext).inflate(R.layout.player_list_item_layout, null);
holder.playerNameTextView = (TextView) convertView.findViewById(R.id.playerNameTextView);
holder.playerValueTextView = (TextView) convertView.findViewById(R.id.playerValueTextView);
holder.remainingCapTextView = (TextView) convertView.findViewById(R.id.remainingCapTextView);
holder.username = (TextView) convertView.findViewById(R.id.opponentUsername);
holder.status = (TextView) convertView.findViewById(R.id.statusTextView);
holder.vsTeamAbbrev = (TextView) convertView.findViewById(R.id.vsTeam);
holder.resultsTextView = (TextView) convertView.findViewById(R.id.resultsTextView);
convertView.setTag(holder);
break;
case TYPE_SEPARATOR:
convertView = LayoutInflater.from(mContext).inflate(R.layout.player_section_header, null);
holder.playerNameTextView = (TextView) convertView.findViewById(R.id.lastPlayer);
holder.lastPlayerDate = (TextView) convertView.findViewById(R.id.lastPlayerDate);
convertView.setTag(holder);
break;
}
//convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
switch (rowType){
case TYPE_ITEM:
DecimalFormat formatter = new DecimalFormat("$#,###");
holder.playerNameTextView.setText(player.getName());
holder.playerValueTextView.setText(formatter.format(Double.parseDouble(player.getValue())));
holder.remainingCapTextView.setText(formatter.format(Double.parseDouble(player.getCap())));
holder.username.setText(player.getUsername());
holder.status.setText(player.getStatus());
holder.vsTeamAbbrev.setText(player.getVsTeamAbbrev());
holder.resultsTextView.setText(player.getResultsTextView());
if (player.isMatchMade()) {
holder.status.setVisibility(View.VISIBLE);
}
break;
case TYPE_SEPARATOR:
holder.playerNameTextView.setText(player.getName());
holder.lastPlayerDate.setText(player.getLastPlayerDate());
break;
}
return convertView;
}
private static class ViewHolder {
//item views
TextView playerValueTextView;
TextView playerNameTextView;
TextView remainingCapTextView;
TextView username;
TextView status;
TextView vsTeamAbbrev;
TextView resultsTextView;
//section header views
TextView lastPlayer;
TextView lastPlayerDate;
}
}

When i was click one button of listview another button also click?how can i stop it

I have a listview with so much button.When i was click one button of listview another button also click?how can i stop it.
please solve this problem.
Give me any trick how can i stop another button in listview.
this is my code
private class MenuItemsAdapter extends ArrayAdapter<MenuItem> {
private static final String TAG = "MenuItemsAdapter";
public MenuItemsAdapter(Context context, List<MenuItem> menuItems) {
super(context, 0, menuItems);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final MenuItem menuItem = getItem(position);
View view = convertView;
final ViewHolder viewHolder;
LayoutInflater inflater;
inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.menu_item, parent, false);
viewHolder = new ViewHolder();
// viewHolder.half = (TextView) view.findViewById(R.id.half);
viewHolder.name = (TextView) view.findViewById(R.id.name);
viewHolder.description = (TextView) view.findViewById(R.id.description);
viewHolder.price = (TextView) view.findViewById(R.id.price);
viewHolder.add = (Button) view.findViewById(R.id.add);
viewHolder.selectedView = view.findViewById(R.id.selectedView);
viewHolder.remove = (Button) view.findViewById(R.id.remove);
viewHolder.total = (TextView) view.findViewById(R.id.itemTotal);
viewHolder.quantity = (TextView) view.findViewById(R.id.quantity);
view.setTag(viewHolder);
try
{
viewHolder.name.setText(menuItem.name);
viewHolder.description.setText(menuItem.description);
viewHolder.price.setText(String.valueOf(menuItem.price));
viewHolder.add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mApplication.createNewCartIfPossibleAndAskIfNot(
mActivity, mRestaurant,
new MainApplication.OnCreateCartListener() {
#Override
public void onCreateCart(Cart cart) {
cart.addOne(menuItem);
updateItemFromCart(menuItem, viewHolder);
updateCart();
}
});
}
});
viewHolder.remove.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!mApplication.isCartCreated()) {
return;
}
mApplication.getCart().removeOne(menuItem);
updateItemFromCart(menuItem, viewHolder);
updateCart();
}
});
}catch(NullPointerException e){e.printStackTrace();}
return view;
}
private void updateItemFromCart(MenuItem menuItem, ViewHolder viewHolder) {
if (!mApplication.isCartCreated()) {
return;
}
int quantity = mApplication.getCart().getNOfItemsOfType(menuItem);
if (quantity > 0) {
viewHolder.selectedView.setVisibility(View.VISIBLE);
} else {
viewHolder.selectedView.setVisibility(View.GONE);
}
viewHolder.quantity.setText(String.valueOf(quantity));
viewHolder.total.setText(String.valueOf(quantity
* menuItem.price));
}
class ViewHolder {
TextView name;
TextView description;
TextView price;
Button add;
View selectedView;
Button remove;
TextView total;
TextView quantity;
TextView half;
}
}
I can think of you are missing recycle adapter implementation with static view holder inside adapter. Again, this is hypothetical answer based on text and my past experience. More detailed investigation requires code study.
You can keep null check for your view
final MenuItem menuItem = getItem(position);
View view = convertView;
final ViewHolder viewHolder;
if (convertView == null) {
LayoutInflater inflater;
inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.menu_item, parent, false);
viewHolder = new ViewHolder();
// viewHolder.half = (TextView) view.findViewById(R.id.half);
viewHolder.name = (TextView) view.findViewById(R.id.name);
viewHolder.description = (TextView) view.findViewById(R.id.description);
viewHolder.price = (TextView) view.findViewById(R.id.price);
viewHolder.add = (Button) view.findViewById(R.id.add);
viewHolder.selectedView = view.findViewById(R.id.selectedView);
viewHolder.remove = (Button) view.findViewById(R.id.remove);
viewHolder.total = (TextView) view.findViewById(R.id.itemTotal);
viewHolder.quantity = (TextView) view.findViewById(R.id.quantity);
view.setTag(viewHolder);
}else{
viewHolder= (ViewHolder)convertView.getTag();
}

adding separators in a listView

I'm trying to add separators between my list view items, I have sorted them into date order and added list entries where the separator needs to go but as soon as it gets to a seperator it stops, no error message or anything it just doesn't add the seperator. I've added breakpoints and it definitely runs the code to add it but it doesn't show up. Even if there are other items to add after the separator it still stops at the separator.
code:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater itemInflater = LayoutInflater.from(getContext());
JourneyItem singleItem = list.get(position);
if(singleItem.isSeperator()){
//set customRow to seperator layout
View customRow = itemInflater.inflate(R.layout.journey_list_seperator, parent, false);
TextView monthText = (TextView) customRow.findViewById(R.id.seperatorMonthText);
TextView yearText = (TextView) customRow.findViewById(R.id.seperatorYearText);
Date current = new Date();
SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
String dtp = GeneralUtil.SQLDateFormatToHuman(list.get(position).getDepartDateTime());
try {
current = df.parse(dtp);
} catch (ParseException e) {
e.printStackTrace();
}
SimpleDateFormat sdfDate = new SimpleDateFormat("MMMM");
monthText.setText(sdfDate.format(current));
yearText.setText(list.get(position).getDepartDateTime().substring(6,10));
return customRow;
}else {
View customRow = itemInflater.inflate(R.layout.custom_journeyitem_row, parent, false);
TextView titleText = (TextView) customRow.findViewById(R.id.titleDisplay);
TextView fromText = (TextView) customRow.findViewById(R.id.fromLocationDisplay);
TextView departText = (TextView) customRow.findViewById(R.id.departDateTimeDisplay);
TextView toText = (TextView) customRow.findViewById(R.id.toLocationDisplay);
TextView colourLbl = (TextView) customRow.findViewById(R.id.colourDisplay);
titleText.setText(singleItem.getTitle());
fromText.setText("From: " + singleItem.getFromLocation());
departText.setText(singleItem.getDepartDateTime());
toText.setText("To: " + singleItem.getToLocation());
colourLbl.setBackgroundColor(singleItem.getColourCode());
return customRow;
}
Create Coustom adapter like this.
class CustomAdapter extends BaseAdapter {
private static final int TYPE_ITEM = 0;
private static final int TYPE_SEPARATOR = 1;
private ArrayList<String> mData = new ArrayList<String>();
private TreeSet<Integer> sectionHeader = new TreeSet<Integer>();
private LayoutInflater mInflater;
public CustomAdapter(Context context) {
mInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void addItem(final String item) {
mData.add(item);
notifyDataSetChanged();
}
public void addSectionHeaderItem(final String item) {
mData.add(item);
sectionHeader.add(mData.size() - 1);
notifyDataSetChanged();
}
#Override
public int getItemViewType(int position) {
return sectionHeader.contains(position) ? TYPE_SEPARATOR : TYPE_ITEM;
}
#Override
public int getViewTypeCount() {
return 2;
}
#Override
public int getCount() {
return mData.size();
}
#Override
public String getItem(int position) {
return mData.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
int rowType = getItemViewType(position);
if (convertView == null) {
holder = new ViewHolder();
switch (rowType) {
case TYPE_ITEM:
convertView = mInflater.inflate(R.layout.snippet_item1, null);
holder.textView = (TextView) convertView.findViewById(R.id.text);
break;
case TYPE_SEPARATOR:
convertView = mInflater.inflate(R.layout.snippet_item2, null);
holder.textView = (TextView) convertView.findViewById(R.id.textSeparator);
break;
}
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.textView.setText(mData.get(position));
return convertView;
}
public static class ViewHolder {
public TextView textView;
}
}
Complete link here.

Categories

Resources