Android - Pull to refresh on custom adapter - android

I'm developing an Android Application and I have a custom adapter for my ListView. I want to use a Pull to refresh library, such as this one. This works fine with an ArrayAdapter, but not with a custom adapter.
How can I get my pull to refresh working with a custom adapter?
Here my adapter code:
public class AdvertListAdapter extends BaseAdapter {
private AdvertListActivity activity;
private int currentFirstVisibleItem=0;
private int currentLastVisibleItem=8;
public AdvertListAdapter(AdvertListActivity activity) {
this.activity = activity;
}
public void setFirstVisibleItem(int currentFirstVisibleItem) {
this.currentFirstVisibleItem=currentFirstVisibleItem;
}
public void setLastVisibleItem(int currentLastVisibleItem) {
this.currentLastVisibleItem=currentLastVisibleItem;
}
#Override
public int getCount() {
return activity.getAdvertList().size();
}
#Override
public Advert getItem(int pos) {
return activity.getAdvertList().get(pos);
}
#Override
public long getItemId(int pos) {
return activity.getAdvertList().get(pos).getId();
}
private class ViewHolder {
public ImageView image;
public TextView title;
public TextView price;
public TextView size;
public TextView city;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder view;
LayoutInflater inflator = activity.getLayoutInflater();
if(convertView == null) {
view = new ViewHolder();
convertView = inflator.inflate(R.layout.listview_advertlist, null);
view.image = (ImageView) convertView.findViewById(R.id.advertImage);
view.title = (TextView) convertView.findViewById(R.id.advertTitleText);
view.price = (TextView) convertView.findViewById(R.id.advertPriceText);
view.size = (TextView) convertView.findViewById(R.id.advertSizeText);
view.city = (TextView) convertView.findViewById(R.id.advertCityText);
convertView.setTag(view);
}else {
view = (ViewHolder) convertView.getTag();
}
Advert advert = activity.getAdvertList().get(position);
view.title.setText(advert.getTitle());
view.price.setText(String.valueOf(advert.getPrice()+"€"));
view.size.setText("Taille: "+advert.getSize());
view.city.setText(advert.getCity());
if(position <= currentFirstVisibleItem || position >= currentLastVisibleItem) {
view.image.setImageResource(R.drawable.defaultadvertimage);
ServiceHelper.loadAsyncImage(view.image, activity.getApplicationContext(), String.valueOf(advert.getId()+"-mini"), "advert");
}
return convertView;
}
}
Here my activity code:
list = (PullToRefreshListView) findViewById(R.id.advertList);
advertList = getAdvertList();
((PullToRefreshListView) list).setOnRefreshListener(new OnRefreshListener() {
#Override
public void onRefresh() {
// Do work to refresh the list here.
new GetDataTask().execute();
}
});
list.setAdapter(adapter);

Related

Update Listitem Textview on click of Listitem Button in Listview custom adapter

I have a listview with textview and button.
I need to update a textview when a button is clicked in a listview, and hide the button.
When I click the button, it updates the textview value. But when I scroll down and move back up, the textview content is changed back to the previous value and the button is visible.
Customadapter.java
public class CustomAdapter extends BaseAdapter {
ArrayList cusList = new ArrayList();
LayoutInflater inflater;
Context context;
public BookAdapter(Context context, ArrayList cusList) {
this.cusList = cusList;
this.context = context;
inflater = LayoutInflater.from(this.context);
}
#Override
public int getCount() {
return cusList.size();
}
#Override
public Object getItem(int position) {
return cusList.get(position);
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder viewHolder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.list, parent, false);
viewHolder = new ViewHolder(convertView);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
Bookdata data = (Bookdata) getItem(position);
viewHolder.mytv.setText(data.getName());
viewHolder.mybtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
v.setVisibility(View.GONE);
viewHolder.setText("Clicked");
}
});
return convertView;
}
private class ViewHolder {
TextView mytv;
Button mybtn;
public ViewHolder(View item) {
mytv = (TextView)item.findViewById(R.id.mytv);
mybtn = (Button)item.findViewById(R.id.mybtn);
}
}
}
Bookdata.java
public class Bookdata {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Listview Code:
ListView books= (ListView) findViewById(R.id.books);
ArrayList bookList = new ArrayList();
for (int i = 0; i < 10; i++) {
Bookdata data = new Bookdata();
data.setName("position "+i);
bookList.add(data);
}
CustomAdapter allbookAda= new CustomAdapter(context, bookList);
books.setAdapter(allbookAda);
allbookAda.notifyDataSetChanged();
Maintain a flag for each listitem to check which list item has been updated or not.
Basically take one flag value in your model class.
public class Bookdata {
private String name;
private boolean flag;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isFlag() {
return flag;
}
public void setFlag(boolean flag) {
this.flag = flag;
}
}
add following changes in your getview method
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder viewHolder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.list, parent, false);
viewHolder = new ViewHolder(convertView);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
Bookdata data = (Bookdata) getItem(position);
viewHolder.mytv.setText(data.getName());
if(data.getFlag){
viewHolder.mybtn.setVisibility(View.GONE);
}
viewHolder.mybtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
v.setVisibility(View.GONE);
viewHolder.setText("Clicked");
cusList.get(position).setFlag(true);
}
});
return convertView;
}
This is a common behavior as listview uses recycled views, the above answers are correct but I prefer a more cleaner approach using DataBinding

Android getView() in BaseAdapter not called (but getCount() is called)

There no errors but the ListView is empty. I have implemented getCount() which returns right number of items in my ArrayList. And the ListView is visibility。
I had call setListAdapter on the ListView,and the xml is match_parent.
This is my Fragment.
mNewsList = infoBean.getNews();
if (mNewsList != null) {
mNewsAdapter = new NewsAdapter();
mListView.setAdapter(mNewsAdapter);
mNewsAdapter.notifyDataSetChanged();
}
} else {//load more
List<HomeBean.NewsBean> news = infoBean.getNews();
if (mNewsList != null) {
mNewsList.addAll(news);
mNewsAdapter.notifyDataSetChanged();
}
This is my Adapter:
private class NewsAdapter extends BaseAdapter {
#Override
public int getCount() {
return mNewsList.size();
}
#Override
public HomeBean.NewsBean getItem(int position) {
return mNewsList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = View.inflate(MyApplication.getmContext(), R.layout.item_list, null);
holder = new ViewHolder();
holder.ivIcon = convertView.findViewById(R.id.iv_item);
holder.tvTitle = convertView.findViewById(R.id.tv_item_list_title);
holder.content = convertView.findViewById(R.id.tv_item_list_content);
holder.tvTime = convertView.findViewById(R.id.tv_time);
holder.author = convertView.findViewById(R.id.tv_author);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.tvTitle.setText(getItem(position).getTitle());
holder.content.setText(getItem(position).getContent());
SimpleDateFormat dataFormat = new SimpleDateFormat("yyyy-MM-dd");
String itemTime = dataFormat.format(getItem(position).getCreate_at() * 1000);
holder.tvTime.setText(itemTime);
holder.author.setText(getItem(position).getSection_name());
if (getItem(position).getCover_pic().startsWith("http:")) {
picUrl = getItem(position).getCover_pic();
} else {
picUrl = RBConstants.SERVER_PIC + getItem(position).getCover_pic();
}
HttpLoader.getInstance(MyApplication.getmContext()).display(holder.ivIcon, picUrl);
return convertView;
}
}
This is my holder.
class ViewHolder {
public ImageView ivIcon;
public TextView tvTitle;
public TextView tvTime;
public TextView content;
public TextView author;
}
this is because mListView.setAdapter(mNewsAdapter); is never executed and of cource there is no need to call mNewsAdapter.notifyDataSetChanged(); immediatelly after calling mListView.setAdapter(mNewsAdapter);
ListView is old, please switch to RecyclerView.

Updating views within a single row of a ListView when clicked

I was wondering how to update views/UI (say, making a TextView invisible) within a single row of a ListView when clicking on the row because every other rows' views would be affected as well. A good example would be playing a song in Google Play Music, and the equalizer animation would be displayed accordingly. Here are my snippets:
MainActivity's onCreate():
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); // Renders only a ListView
ListView songListView = (ListView) findViewById(R.id.list);
List<Song> songList = new ArrayList<>();
for (int i = 1; i <= 200; i++) songList.add(new Song("Song" + i, "Artist" + i));
SongAdapter songAdapter = new SongAdapter(this, songList);
songListView.setAdapter(songAdapter);
songListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long l) {
if (position == view.getId()) {
TextView titleText = (TextView) view.findViewById(R.id.song_title);
titleText.setVisibility(View.INVISIBLE);
}
}
});
}
... and my ArrayAdapter's getView method and ViewHolder class:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent,
false);
holder = new ViewHolder();
holder.song = (TextView) convertView.findViewById(R.id.song_title);
holder.artist = (TextView) convertView.findViewById(R.id.song_artist);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
convertView.setId(position);
Song currentSong = getItem(position);
if (currentSong != null) {
holder.song.setText(currentSong.getTitle());
holder.artist.setText(currentSong.getArtist());
}
return convertView;
}
private static class ViewHolder {
TextView song;
TextView artist;
}
Add one more variable in your song model class to make visibility for each row, please try this below code :
class Song{
private boolean isShowing =false;
public boolean isShowing() {
return isShowing;
}
public void setShowing(boolean showing) {
isShowing = showing;
}
}
SongsAdapter.class
class SongAdapter extends BaseAdapter {
private Context mContext;
private List<Song> mSongList;
public SongAdapter(Context context, List<Song> songs) {
mContext = context;
mSongList = songs;
}
#Override
public int getCount() {
return mSongList.size();
}
#Override
public Object getItem(int i) {
return null;
}
#Override
public long getItemId(int i) {
return 0;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(R.layout.list_item, parent,
false);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
final Song songModel = mSongList.get(position);
holder.song.setText(songModel.getSongTitle());
holder.artist.setText(songModel.getSongArtist());
if(songModel.isShowing()){
holder.song.setVisibility(View.VISIBLE);
}else{
holder.song.setVisibility(View.GONE);
}
convertView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(songModel.isShowing()){
songModel.setShowing(false);
}else{
songModel.setShowing(true);
}
notifyDataSetChanged();
}
});
return convertView;
}
public class ViewHolder{
TextView song;
TextView artist;
public ViewHolder(View itemView) {
song = (TextView)itemView.findViewById(R.id.song_title);
artist = (TextView)itemView.findViewById(R.id.artist);
}
}
}
Hope this helps:
holder.song.setId(position);
Then your code looks like this:
mSongView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long l) {
Log.d(LOG_TAG, "Song item clicked");
TextView titleText = (TextView) view.findViewById(position);
titleText.setVisibility(View.INVISIBLE);
}
});
Add the below code in your adapter class
public interface OnItemClickListener {
void onClick(View view);}
private final OnItemClickListener listener;
public YourAdapter(Context mContext, OnItemClickListener listener) {
this.mContext = mContext;
this.listener = listener;}
Modify your viewholder class
class ViewHolder implements View.OnClickListener {
TextView song;
TextView artist;
ViewHolder(View itemView) {
super(itemView);
song = (TextView)itemView.findViewById(R.id.txt1);
artist = (TextView)itemView.findViewById(R.id.txt2);
itemView.setOnClickListener(this);
}
# Override
public void onClick(View v) {
listener.onClick(v);
}
And lastly implement the interface in your Activity class
class MainActivity extends AppCompatActivity implements YourAdapter.OnItemClickListener{
#Override
public void onClick(View view) {
TextView titleText = (TextView) view.findViewById(R.id.song_title);
titleText.setVisibility(View.INVISIBLE);
}
}
I finally figured this out after getting help from my mentor. Initializing an int field in the adapter to keep record of previous positions in the list did the trick as follows:
MainActivity:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView songListView = (ListView) findViewById(R.id.list);
List<Song> songList = new ArrayList<>();
for (int i = 1; i <= 200; i++) songList.add(new Song("Song" + i, "Artist" + i));
final SongAdapter songAdapter = new SongAdapter(this, songList);
songListView.setAdapter(songAdapter);
songListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long l) {
songAdapter.setSongPosition(position);
songAdapter.notifyDataSetChanged();
}
});
}
}
Custom Song class:
public class Song {
private String mSongTitle, mSongArtist;
public Song(String songTitle, String songArtist) {
mSongTitle = songTitle;
mSongArtist = songArtist;
}
public String getSongTitle() {
return mSongTitle;
}
public String getSongArtist() {
return mSongArtist;
}
}
Custom adapter:
public class SongAdapter extends ArrayAdapter<Song> {
private int currentlyPlaying = -1;
public SongAdapter(Context context, List<Song> songList) {
super(context, 0, songList);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent,
false);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Song song = getItem(position);
holder.song.setText(song.getSongTitle());
holder.artist.setText(song.getSongArtist());
// Hides the row, so both TextViews. Otherwise, sets each row visible.
if (position == currentlyPlaying) convertView.setVisibility(View.INVISIBLE);
else convertView.setVisibility(View.VISIBLE);
return convertView;
}
private class ViewHolder {
private TextView song;
private TextView artist;
private ViewHolder(View itemView) {
song = (TextView) itemView.findViewById(R.id.song_title);
artist = (TextView) itemView.findViewById(R.id.artist);
}
}
public void setSongPosition(int position) {
currentlyPlaying = position;
}
}

How to implement onClick() method on ImageView

I have dynamically populated ImageViews and Textviews.
Now I have small problem with implementing onClick method on ImageView. On some ImageView click it should switch to fragment and for some click onto activity.
here is my code - my main fragment:
public class MainFragment extends Fragment {
public static String[] gridViewStrings = {
"string1",
"string2",
"string3",
"string4",
"string5",
"string6"
};
public static int[] gridViewImages = {
R.drawable.delivery,
R.drawable.shipping_logs,
R.drawable.meassurement,
R.drawable.takeovers,
R.drawable.settings,
R.drawable.download_data
};
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.main_fragment, parent, false);
gridView = (GridView) view.findViewById(R.id.grid);
gridView.setAdapter(new CustomAndroidGridViewAdapter(getActivity(), gridViewStrings, gridViewImages));
imageView = (ImageView) view.findViewById(R.id.gridview_image);
}
my custom gridview adapter:
public class CustomAndroidGridViewAdapter extends BaseAdapter {
private Context mContext;
private final String[] string;
private final int[] Imageid;
public CustomAndroidGridViewAdapter(Context c,String[] string,int[] Imageid ) {
mContext = c;
this.Imageid = Imageid;
this.string = string;
}
#Override
public int getCount() {
return string.length;
}
#Override
public Object getItem(int p) {
return null;
}
#Override
public long getItemId(int p) {
return 0;
}
#Override
public View getView(final int p, final View convertView, ViewGroup parent) {
View grid;
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
grid = new View(mContext);
grid = inflater.inflate(R.layout.grid, null);
TextView textView = (TextView) grid.findViewById(R.id.gridview_text);
ImageView imageView = (ImageView)grid.findViewById(R.id.gridview_image);
textView.setText(string[p]);
imageView.setImageResource(Imageid[p]);
} else {
grid = (View) convertView;
}
return grid;
}
}
QUESTION: How to implement onClick() on ImageView and switch to fragment and activity?
public DlgOutletListAdapter(Context context, List<WrapperOutlet> wrapperOutletList, View.OnClickListener listener) {
this.context = context;
this.wrapperOutletList = wrapperOutletList;
this.listener = listener; //---> pass in listener....
}
#Override
public int getCount() {
return wrapperOutletList.size();
}
#Override
public Object getItem(int position) {
return wrapperOutletList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.item_dlg_outlet_list, parent, false);
viewHolder = new ViewHolder(convertView);
viewHolder.ll_dlg_outlet_list_container.setOnClickListener(listener);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
if (!wrapperOutletList.isEmpty()) {
WrapperOutlet wrapperOutlet = wrapperOutletList.get(position);
Outlet outlet = wrapperOutlet.getOutlet();
if (outlet != null) {
if (TextUtils.isEmpty(outlet.getTel())){
viewHolder.ivCall.setImageResource(R.drawable.icon_call2);
viewHolder.tvCall.setTextColor(context.getResources().getColorStateList(R.color.medium_gray));
viewHolder.ll_dlg_outlet_list_dialer.setOnClickListener(null);
} else {
viewHolder.ivCall.setImageResource(R.drawable.icon_call);
viewHolder.tvCall.setTextColor(context.getResources().getColorStateList(R.color.fresh_teal));
viewHolder.ll_dlg_outlet_list_dialer.setOnClickListener(listener);
}
}
}
return convertView;
}
Pass in listener. Give you an example above. code below is where you call the adapter in fragment. My fragment implements View.OnClickListener
lv_dlg_outlet_list.setAdapter(new DlgOutletListAdapter(activity, wrapperOutletList, this));
Interface Callback:
public interface LaunchFragmentWithImage{
void launchFragmentWithImage(int imageID);
}
Give the adapter a reference of this class below so it can call the method. Wouldn't recommend giving the adapter itself a reference to the class because its not the adapters responsibility to know who recieves the image, thus violating the Single responsibility principle of OOD, but for the sake of learning it's okay.
public class ClassToLaunchNewFragment extends AppCompatActivity implements
LaunchFragmentWithImage{
#Override //Method from interface we've defined.
public void launchFragmentWithImage(int imageID){
FragmentToLaunch fragmentToLaunch = FragmentToLaunch.newInstance();
Bundle imageData = new Bundle();
imageData.putInt(imageID, "key");
fragmentToLaunch.setArguments(imageData);
getSupportFragmentManager().beginTransaction() //Call replace not add
.replace(containerID, fragmentToLaunch)
.commit();
}
}
In AdapterClass
imageViewVar.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
fragmentToLaunchInstanceThatWasPassedIn.launchFragmentWithImage(theImageResource);
}
});
grid = new View(mContext);
grid = inflater.inflate(R.layout.grid, null);
why are you pointing the grid variable to two separate View objects?
if (convertView == null) {
grid = inflater.inflate(R.layout.grid, null);
TextView textView = (TextView) grid.findViewById(R.id.gridview_text);
ImageView imageView = (ImageView)grid.findViewById(R.id.gridview_image);
textView.setText(string[p]);
imageView.setImageResource(Imageid[p]);
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
switch(choosedPosition){
case 0:do something;break;
case 1:do something;break;
...
}
}
});
}
It's just an example and can't running.

Listview items background

How to change the background image for list items, i am able to change only 1 item background at a time.
If there are 6 items on the list and if click on 3 items those 3 items background images should be changed, how it is possible
Below is my code
public class Places extends Activity {
private ListView listView;
private int selectedListItem = -1;
private Handler mHandler = new Handler();
private Vector<String> data;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.places_custom_list_view);
data = new Vector<String>();
// Add data as per your requirement
data.add("one");
data.add("two");
data.add("three");
data.add("four");
data.add("five");
listView = (ListView)findViewById(R.id.ListView01);
listView.setDivider(null);
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
selectedListItem = position;
((EfficientAdapter)listView.getAdapter()).notifyDataSetChanged();
mHandler.postDelayed(new Runnable() {
#Override
public void run() {
// call any new activity here or do any thing you want here
}
}, 200L);
}
});
listView.setAdapter(new EfficientAdapter(getApplicationContext()));
}
private class EfficientAdapter extends BaseAdapter {
private LayoutInflater mInflater;
public EfficientAdapter(Context context) {
mInflater = LayoutInflater.from(context);
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null || convertView.getTag() == null) {
convertView = mInflater.inflate(R.layout.places_custom_row_view, null);
holder = new ViewHolder();
holder.txtName = (TextView) convertView
.findViewById(R.id.name);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
if(position == selectedListItem) {
holder.txtName.setBackgroundResource(R.drawable.cellbghover);
} else {
holder.txtName.setBackgroundResource(R.drawable.cellbgnew);
}
holder.txtName.setText(data.get(position));
return convertView;
}
}
static class ViewHolder {
TextView txtName;
}
Try this,it should work logically.(I didn't try it,btw! :P)
...
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
EfficientAdapter.saveState.put(position,"selected");
mHandler.postDelayed(new Runnable() {
#Override
public void run() {
// call any new activity here or do any thing you want here
}
}, 200L);
}
});
...
private class EfficientAdapter extends BaseAdapter {
public static HashMap<Integer,String> saveState=new HashMap<Integer,String>();
private LayoutInflater mInflater;
public EfficientAdapter(Context context)
{
mInflater = LayoutInflater.from(context);
for(int i=0;i<5;i++)
{
saveState.put(i,"unselected");
}
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null || convertView.getTag() == null) {
convertView = mInflater.inflate(R.layout.places_custom_row_view, null);
holder = new ViewHolder();
holder.txtName = (TextView) convertView
.findViewById(R.id.name);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
if(saveState.get(position).equals("selected"))
holder.txtName.setBackgroundResource(R.drawable.cellbghover);
else
holder.txtName.setBackgroundResource(R.drawable.cellbgnew);
holder.txtName.setText(data.get(position));
return convertView;
}
}
static class ViewHolder {
TextView txtName;
}
Every time you click on listview item whole listview is getting refreshed.
So if you want to show previously selected items also then need to keep record of all the item selected. and when listview refreshed you need to check that this positions are previously selected or not according to that set your color.
Hope this help you
try this
android:background="#drawable/img_list_background_repeater"
if(clickWord.size()!=0)
{
for(int i = 0;i<clickWord.size();i++){
if(clickWord.get(i).equalsIgnoreCase(adListText[position])&&clickIndex.get(i)==position){
wordChk.setBackgroundResource(R.drawable.star_1);
}
}
}
Here clickWord is an arraylist of items selected. so when items get selected it will be added in this arraylist and when arraylist is refreshed i check all this using this loop.

Categories

Resources