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.
Related
I have a ListView in AlertDialog which contains more button on each row & it will popup a list with delete option. When I click on delete option, its deleting the last item. In CustomAdaptor I tried a notifyDataSetChanged() after deleting item from ArrayList. Here it always deletes the last item irrespective of item position.
CustomAdaptor Code
public class CustomAdapter extends BaseAdapter {
Context context;
List<String> lrowItems ;
ArrayList<String> listItems;
String className;
CalHelper calHelper;
Settings settings;
LayoutInflater inflter;
public CustomAdapter(Context applicationContext, ArrayList<String> listItems) {
this.context = applicationContext;
this.listItems = listItems;
this.className = context.getClass().getSimpleName();
this.calHelper = new CalHelper(applicationContext);
this.settings = new Settings(applicationContext);
inflter = (LayoutInflater.from(applicationContext));
}
#Override
public int getCount() {
if(listItems != null) {
return listItems.size();
}else{
return 0;
}
}
#Override
public Object getItem(int position) {
return listItems.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
//With following overrides Prevent listview item will not mix randomly
//and convertView provided is of the appropriate type.
#Override
public int getViewTypeCount() {
if(getCount() > 0){ //for no records in returning view
return getCount();
}else{
return super.getViewTypeCount();
}
}
#Override
public int getItemViewType(int position) {
return position;
}
/* private view holder class for holding calculation history*/
private class HistoryViewHolder {
TextView more;
TextView expression;
TextView result;
TextView shortNote;
TextView expTime;
}
/* private view holder class for holding GST calculations history*/
private class GstHistoryViewHolder {
TextView amount;
TextView gst;
TextView total;
TextView expTime;
ImageView share;
}
/* private view holder class */
private class BMIViewHolder {
TextView bmiScore;
TextView bmiDate;
TextView bmiWeight;
}
#Override
public View getView(final int position, View convertView, ViewGroup viewGroup) {
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
//final int pos = position;
if(className.equals("MainActivity")) {
final HistoryViewHolder holder = new HistoryViewHolder();
final String expString = listItems.get(position);
final String expre = expString.substring(0, expString.indexOf(":"));
final String expreTime = expString.substring(expString.indexOf(":") + 1, expString.length());
final String result = expre.substring(expre.indexOf("=")+1);
String timePattern = calHelper.calHistoryTimePattern(expreTime);
SimpleDateFormat localDateFormat = new SimpleDateFormat(timePattern);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.listview_row, null);
holder.more = (TextView) convertView
.findViewById(R.id.more);
holder.expression = (TextView) convertView.findViewById(R.id.expression);
holder.result = (TextView) convertView.findViewById(R.id.result);
holder.shortNote = (TextView) convertView.findViewById(R.id.shortNote);
holder.expTime = (TextView) convertView.findViewById(R.id.expTime);
holder.expression.setText(expre.substring(0, expre.indexOf("=")));
holder.result.setText(result);
String time = localDateFormat.format(new Date(expreTime));
holder.expTime.setText(time);
holder.shortNote.setText(settings.getHistoryNote(context,expString));
convertView.setTag(holder);
final TextView btnMore = (TextView) holder.more;
btnMore.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showPopup(v, position, expString, expre, expreTime, holder.shortNote);
}
});
}
}
return convertView;
}
public void showPopup(View v, final int position, final String expString, final String expression, final String expTime, final TextView shortNote) {
final PopupMenu popup = new PopupMenu(context, v);
//truncate title with ellipsis for more characters
final String noteTitle = expression.length()>10? expression.substring(0, 10) + "..." : expression;
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.history_menu, popup.getMenu());
//registering popup with OnMenuItemClickListener
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
Intent intent = new Intent(context, MainActivity.class);
int itenId = item.getItemId();
switch (itenId){
case R.id.mnuInsertExpression:
//some code here
break;
case R.id.mnuHisNote:
//some code here
break;
case R.id.mnuCopyHisRecord:
calHelper.copyToClipboard(expression);
break;
case R.id.mnuShareHisRecord:
calHelper.shareText(expression);
break;
case R.id.mnuDeleteHisRecord: //This is where I am deleting item
System.out.println("Pos:"+position);
listItems.remove(position);
notifyDataSetChanged();
Collections.reverse(listItems); //sort descending by time again
settings.clearSharedPre(context,expString); //remove note attached to this expression from ShredPref
settings.saveArrayList(context, listItems, "EXP_HISTORY");
intent.putExtra("HISTORY_RECORD_DELETED", true);
Toast.makeText(context,"Record removed from history", Toast.LENGTH_SHORT).show();
context.startActivity(intent);
break;
}
return true;
}
});
popup.show();
}
Any help is greatly appreciated.
Try this getView():
#Override
public View getView(int position, View convertView, ViewGroup viewGroup) {
HistoryViewHolder holder;
if(className.equals("MainActivity")) {
final String expString = listItems.get(position);
final String expre = expString.substring(0, expString.indexOf(":"));
final String expreTime = expString.substring(expString.indexOf(":") + 1, expString.length());
final String result = expre.substring(expre.indexOf("=")+1);
String timePattern = calHelper.calHistoryTimePattern(expreTime);
SimpleDateFormat localDateFormat = new SimpleDateFormat(timePattern);
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.listview_row, null);
holder = new HistoryViewHolder();
holder.more = (TextView) convertView.findViewById(R.id.more);
holder.expression = (TextView) convertView.findViewById(R.id.expression);
holder.result = (TextView) convertView.findViewById(R.id.result);
holder.shortNote = (TextView) convertView.findViewById(R.id.shortNote);
holder.expTime = (TextView) convertView.findViewById(R.id.expTime);
holder.more.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int pos = (int) v.getTag();
showPopup(v, pos, expString, expre, expreTime, holder.shortNote);
}
});
}else{
holder = (HistoryViewHolder)convertView.getTag();
}
holder.expression.setText(expre.substring(0, expre.indexOf("=")));
holder.result.setText(result);
String time = localDateFormat.format(new Date(expreTime));
holder.expTime.setText(time);
holder.shortNote.setText(settings.getHistoryNote(context, expString));
holder.more.setTag(position);
convertView.setTag(holder);
}
return convertView;
}
Hope that helps!
Its because you are using the position of the getView parameter, that will always be whatever the last index is at the time you go to use it.
Instead you need to add that position to the view, possibly in your convertView is what I usually do (the tag can be anything you want), do convertView.setTag(position) in your getView method as you are creating each items view, and then in your delete method within showpopup you can do (int)view.getTag() to get the position to remove from your dataset.
You should add the postion to the button using setTag() and get the position when passing it to showPopup() using getTag(). You can do the following.
// ....
final TextView btnMore = (TextView) holder.more;
btnMore.setTag(position); // here add the position to the button using setTag().
btnMore.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// here you can get position your button using v.getTag().
int pos = (int) v.getTag();
showPopup(v, pos, expString, expre, expreTime, holder.shortNote);
}
});
Hello I realize there are multiple people with a similar issue but the reason for me adding this as a new question is because this is an adapter with two holders. I have a listview that has a header cell and the information cell. the error that I am encountering its
java.lang.NullPointerException: Attempt to read from field 'android.widget.TextView.
Which only happens when I scroll the list down and then go back up. I am assuming the holder is getting destroyed and when I call the line to add text the holder is null but I am not sure. Thank you for your help in advance!
Custom Adapter:
private class MyCustomAdapter extends ArrayAdapter<sources> {
private ArrayList<sources> sources_list = new ArrayList<sources>();
private TreeSet<Integer> sectionHeader = new TreeSet<Integer>();
private static final int TYPE_ITEM = 0;
private static final int TYPE_SEPARATOR = 1;
private LayoutInflater vi;
public MyCustomAdapter(Context context, int textViewResourceId, ArrayList<sources> sources_list) {
super(context, textViewResourceId, sources_list);
vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void addItem(final sources item) {
sources_list.add(item);
notifyDataSetChanged();
}
public void addSectionHeaderItem(final sources item) {
sources_list.add(item);
sectionHeader.add(sources_list.size() - 1);
notifyDataSetChanged();
}
public int getItemViewType(int position) {
return sectionHeader.contains(position) ? TYPE_SEPARATOR : TYPE_ITEM;
}
public int getCount() {
return sources_list.size();
}
public int getViewTypeCount() {
return 2;
}
public sources getItem(int position) {
return sources_list.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
public class ViewHolder {
TextView code;
CheckBox name;
}
public class ViewHolder2 {
TextView separator;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
ViewHolder2 holder2 = null;
Log.v("ConvertView", String.valueOf(position));
int rowType = getItemViewType(position);
if (convertView == null) {
switch (rowType) {
case TYPE_ITEM:
convertView = vi.inflate(R.layout.source_cell, null);
holder = new ViewHolder();
holder.code = (TextView)
convertView.findViewById(R.id.code);
holder.name = (CheckBox)
convertView.findViewById(R.id.checkBox1);
break;
case TYPE_SEPARATOR:
holder2 = new ViewHolder2();
convertView = vi.inflate(R.layout.source_header, null);
holder2.separator = (TextView)
convertView.findViewById(R.id.separator);
break;
}
convertView.setTag(holder);
} else {
if (rowType == TYPE_ITEM) {
holder = (ViewHolder)
convertView.getTag(R.layout.source_cell);
} else {
holder2 = (ViewHolder2) convertView.getTag(R.layout.source_header);
}
}
if (rowType == TYPE_ITEM) {
sources n_source = sources_list.get(position);
holder.code.setText(n_source.getCode().toUpperCase());
holder.name.setTag(n_source);
} else {
sources n_source = sources_list.get(position);
holder2.separator.setText(n_source.getCode().toUpperCase());
}
return convertView;
}
}
Your setTag/getTag appears to be wrong. You need to use setTag(r.layout.blah, holder) to match your getTag(r.layout.blah).
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;
}
}
I have some trouble with layout in my list activity.
My list contain separators and text rows
SetupActivity extend ListActivity
private MyCustomAdapter mAdapter;
TextView selection;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
mAdapter = new MyCustomAdapter();
mAdapter.addItem("Help/FAQ");
mAdapter.addSeparatorItem("Connection to Server");
// mAdapter.addItem("Connection");
// mAdapter.addItem("Network");
// mAdapter.addItem("config");
// mAdapter.addItem("User");
// mAdapter.addItem("pass");
// mAdapter.addItem("Email");
// mAdapter.addItem("PlatForm");
mAdapter.addSeparatorItem("Consumption");
// mAdapter.addItem("100%");
mAdapter.addSeparatorItem("Map");
// mAdapter.addItem("Map rotation");
// mAdapter.addItem("auto Zoom");
// mAdapter.addItem("Measure Units");
// mAdapter.addItem("Show Heading");
// mAdapter.addItem("Compass North");*/
mAdapter.addFooterItem(getString(R.string.setup_note_map));
mAdapter.addSeparatorItem("Support");
mAdapter.addItem("About");
/*
* mAdapter.addItem("Contact Us"); mAdapter.addItem("Tutorial");
* mAdapter.addItem("Setup Wizard");
*/
mAdapter.addSeparatorItem("Blogs");
mAdapter.addFooterItem(getString(R.string.setup_note_blogs));
setListAdapter(mAdapter);
// selection = (TextView) findViewById(R.id.text);
}
public void onListItemClick(ListView parent, View view, int position,
long id) {
parent.getChildAt(position).setBackgroundColor(position);
if (position == 0) {
Intent myIntent = new Intent(SetupActivity.this,
WebviewHandlerActivity.class);
myIntent.putExtra("ressource", "help");
SetupActivity.this.startActivity(myIntent);
} else if (position == 6) {
Intent myIntent = new Intent(SetupActivity.this,
AboutActivity.class);
SetupActivity.this.startActivity(myIntent);
}
}
// Adapter Class
private class MyCustomAdapter extends BaseAdapter {
private static final int TYPE_ITEM = 2;
private static final int TYPE_SEPARATOR = 0;
private static final int TYPE_FOOTER = 1;
private static final int TYPE_MAX_COUNT = TYPE_ITEM + 1;
private ArrayList<String> mData = new ArrayList<String>();
private LayoutInflater mInflater;
private TreeSet<Integer> mSeparatorsSet = new TreeSet<Integer>();
private TreeSet<Integer> mFooterSet = new TreeSet<Integer>();
public MyCustomAdapter() {
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void addItem(final String item) {
mData.add(item);
notifyDataSetChanged();
}
public void addSeparatorItem(final String item) {
mData.add(item);
// save separator position
mSeparatorsSet.add(mData.size() - 1);
notifyDataSetChanged();
}
public void addFooterItem(final String item) {
mData.add(item);
// save separator position
mFooterSet.add(mData.size() - 1);
notifyDataSetChanged();
}
#Override
public int getItemViewType(int position) {
if (mSeparatorsSet.contains(position))
return TYPE_SEPARATOR;
else if (mFooterSet.contains(position))
return TYPE_FOOTER;
return TYPE_ITEM;
}
#Override
public int getViewTypeCount() {
return TYPE_MAX_COUNT;
}
public int getCount() {
return mData.size();
}
public String getItem(int position) {
return mData.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
int type = getItemViewType(position);
if (convertView == null) {
holder = new ViewHolder();
switch (type) {
case TYPE_ITEM:
convertView = mInflater.inflate(R.layout.item1, null);
holder.textView = (TextView) convertView
.findViewById(R.id.text);
break;
case TYPE_SEPARATOR:
convertView = mInflater.inflate(R.layout.item2, null);
holder.textView = (TextView) convertView
.findViewById(R.id.textSeparator);
break;
case TYPE_FOOTER:
convertView = mInflater.inflate(R.layout.footer, null);
holder.textView = (TextView) convertView
.findViewById(R.id.note);
break;
}
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.textView.setText(mData.get(position));
return convertView;
}
}
public static class ViewHolder {
public TextView textView;
}
my xml item1 & item2 contain a LinearLayout with a TextView inside
and my footer.xml only a textView
My problem is when i click on a row it doesn't get orange to say that i clicked on it except my footer...(the one i don't want)
So i figure out it's because it's not in a LinearLayout so i tried to put off the LinearLayout of item1.xml but i can't compile anymore.
Does someone can explain to me how to get my row with the click Animation and not on my footers ?
Cheers
You are not getting clicked color because you have set a background color `
setBackgroundColor
You need to set a statelistDrawable as background
Edit: You need to set a drwable something like this
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/pressed_img" android:state_pressed="true"/>
<item android:drawable="#drawable/default_img"/>
</selector>
I want to include a play icon on a list view when ever user clicks on a row.
I am using a custom adapter (Lazy Load with separators)... I had issue with separator being overlapped with other rows so i implemented getViewTypeCount() method to resolve it.
Now when i am including a play icon on onItemClickListener of the list view, icon is getting added fine but its overlapping with other rows(except separator)...
Heres my code :
OnItemCLickListener :
listChannels.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
if (oldView != null) {
oldView.setDrawingCacheEnabled(true);
ImageView icon = (ImageView) oldView.findViewById(R.id.imageViewPlay);
icon.setImageBitmap(null);
icon.setVisibility(ImageView.GONE);
icon.invalidate();
}
oldView = arg1;
selectedItem = arg2;
ImageView icon = (ImageView) arg1.findViewById(R.id.imageViewPlay);
icon.setImageResource(R.drawable.play_small);
icon.setVisibility(ImageView.VISIBLE);
Intent intent = new Intent(StationList2.this,
ServiceLauncher.class);
intent.putExtra("objectPassed", feeds.get(arg2));
startActivity(intent);
}
});
My Custom Adapter :
private class MyCustomAdapter extends BaseAdapter {
private static final int TYPE_ITEM = 0;
private static final int TYPE_SEPARATOR = 1;
private static final int TYPE_MAX_COUNT = TYPE_SEPARATOR + 1;
private ArrayList<BeanChannelList> mData = new ArrayList<BeanChannelList>();
private LayoutInflater mInflater;
private TreeSet<Integer> mSeparatorsSet = new TreeSet<Integer>();
public MyCustomAdapter() {
mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader = new ImageLoader(activity.getApplicationContext());
}
public void addItem(final BeanChannelList beanChannelList) {
mData.add(beanChannelList);
notifyDataSetChanged();
}
public void addSeparatorItem(final BeanChannelList beanChannelList) {
mData.add(beanChannelList);
// save separator position
mSeparatorsSet.add(mData.size() - 1);
notifyDataSetChanged();
}
#Override
public int getItemViewType(int position) {
return mSeparatorsSet.contains(position) ? TYPE_SEPARATOR
: TYPE_ITEM;
}
#Override
public int getViewTypeCount() {
return TYPE_MAX_COUNT;
}
#Override
public int getCount() {
return mData.size();
}
#Override
public String getItem(int position) {
return mData.get(position).getStrTitle();
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
int type = getItemViewType(position);
if (convertView == null) {
holder = new ViewHolder();
switch (type) {
case TYPE_ITEM:
convertView = mInflater
.inflate(R.layout.listitem_row, null);
holder.textView = (TextView) convertView
.findViewById(R.id.textView1);
holder.textview2 = (TextView) convertView
.findViewById(R.id.textView2);
holder.image = (ImageView) convertView
.findViewById(R.id.imageView1);
holder.imagePlay = (ImageView) convertView
.findViewById(R.id.imageViewPlay);
break;
case TYPE_SEPARATOR:
convertView = mInflater.inflate(R.layout.item2, null);
holder.textView = (TextView) convertView
.findViewById(R.id.textView1);
holder.textview2 = (TextView) convertView
.findViewById(R.id.textView2);
holder.image = (ImageView) convertView
.findViewById(R.id.imageView1);
holder.imagePlay = (ImageView) convertView
.findViewById(R.id.imageViewPlay);
convertView.setClickable(false);
convertView.setFocusable(false);
convertView.setFocusableInTouchMode(false);
convertView.setEnabled(false);
break;
}
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
try {
if (mData.get(position).getStrChannelNo().equals("")) {
holder.textView.setText(mData.get(position)
.getStrChannelNo()
+ " "
+ mData.get(position).getStrTitle());
}
else {
holder.textView.setText(mData.get(position)
.getStrChannelNo()
+ ": "
+ mData.get(position).getStrTitle());
}
if (selectedItem == position) {
holder.imagePlay.setImageResource(R.drawable.play_small);
holder.imagePlay.setVisibility(ImageView.VISIBLE);
}
holder.textview2.setText(mData.get(position).getStrLocation());
imageLoader.DisplayImage(mData.get(position).getStrImage()
.toString(), activity, holder.image);
} catch (Exception e) {
e.printStackTrace();
}
return convertView;
}
}
I guess problem is in ViewHolder. Once I had also same kind of problem. Do not use View Holder.