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.
Related
Level Selection GridView Image
I need to make Level 1 clickable and rest non clickable and when user clicks Level 1 it should make Level 2 clickable and rest non-clickable and so on so forth. Also if user is at Level 5 gridview should be clickable from level 1 to 4
myAdapter = new MyCustomAdapter(getActivity());
gridView.setAdapter(myAdapter);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
//do something
}
private class MyCustomAdapter extends BaseAdapter {
private LayoutInflater mInflater;
public MyCustomAdapter(Context context) {
mInflater = LayoutInflater.from(context);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
GridObject object = my.get(position);
GridObject revers=reverseobj.get(position);
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item_icon_set, null);
holder = new ViewHolder();
holder.text = (ImageView) convertView.findViewById(R.id.text);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
if (check.get(position).equals("true"))
{
holder.text.setImageResource(revers.getName());
}
else {
holder.text.setImageResource(object.getName());
}
return convertView;
}
#Override
public int getCount() {
return my.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
}
public class GridObject extends ArrayList<GridObject> {
private int image;
private int state;
public GridObject(int name, int state) {
super();
this.image = name;
this.state = state;
}
public int getName() {
return image;
}
public void setName(int name) {
this.image = name;
}
public int getState() {
return state;
}
public void setState(int state) {
this.state = state;
}
}
If I read it correctly, you want a "current level" that allows you to check/uncheck any grid item up to that level and allow you also to click on the item for the next level to make that the new current level.
If so, then it is simple to modify your adapter by adding a currentLevel variable and adjusting getView() to enable clicking on items up to and including this level.
Here is an updated version of your adapter showing this:
private class MyCustomAdapter extends BaseAdapter implements AdapterView.OnItemClickListener {
private LayoutInflater mInflater;
// implement check as boolean array rather than what appears to be string array
private final SparseBooleanArray check = new SparseBooleanArray();
// keep track of the current level
private int currentLevel = 0;
public MyCustomAdapter(Context context) {
mInflater = LayoutInflater.from(context);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
GridObject object = my.get(position);
GridObject revers = reverseobj.get(position);
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item_icon_set, null);
holder = new ViewHolder();
holder.text = (ImageView) convertView.findViewById(R.id.text);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
if (check.get(position)) // used to be ...equals("true")
{
holder.text.setImageResource(revers.getName());
}
else {
holder.text.setImageResource(object.getName());
}
// make items up to current level clickable
convertView.setClickable(currentLevel < position);
// show unclickable items as disabled
convertView.setEnabled(currentLevel >= position);
return convertView;
}
#Override
public int getCount() {
return my.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// increase the current level if the item for the current level is clicked
currentLevel = Math.max(currentLevel, position + 1);
// toggle the checked state for the clicked item
check.put(position, !check.get(position, false));
// refresh the views
notifyDataSetChanged();
}
}
You can then enable this on your GridView in a similar way as before:
myAdapter = new MyCustomAdapter(getActivity());
gridView.setAdapter(myAdapter);
gridView.setOnItemClickListener(myAdapter);
Note that I have implemented the missing check list as a SparseBooleanArray rather than what looks like an array of strings with "true" and "false" values in your version. You can change it back if you prefer.
I have Used a Simple Check list boolean = gridview items and It Works....By Default I have set the first value true and the rest value false.......
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long
id)
{
if (clickcheck.get(position).equals(true)) {
//complete some task then
position=position+1;
clickcheck.remove(position);
clickcheck.add(position,true);
}
else
{
Toast.makeText(getActivity(), "Click On Level "+position, Toast.LENGTH_SHORT).show();
}
`
Is it possible to change row view when I'm clicking on it? For example I have listView with rows where some short information shown, and when after clicking on row detailed view will show instead of old row.
I tried to use addView in my listView, but "listview addView(View, int) is not supported in AdapterView" error appeared.
try something like that:
public class MyAdapter extends BaseAdapter {
private Context mContext;
private List<UserPojo> mList;
public MyAdapter(Context context, List<UserPojo> users) {
this.mContext = context;
this.mList = users;
}
#Override
public int getCount() {
return mList.size();
}
#Override
public UserPojo getItem(int position) {
return mList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final UserPojo user = mList.get(position);
final ViewHolder holder;
if (convertView == null) {
convertView = View.inflate(mContext, R.layout.item_ad, null);
holder = new ViewHolder();
holder.pic = (CircleImageView) convertView.findViewById(R.id.profile_imageView);
holder.name = (TextView) convertView.findViewById(R.id.name_textView);
convertView.setTag(holder);
} else{
holder = (ViewHolder) convertView.getTag();
}
if (user.isClicked) {
holder.pic.setVisibility(View.VISIBLE);
holder.name.setVisibility(View.GONE);
} else {
holder.pic.setVisibility(View.GONE);
holder.name.setVisibility(View.VISIBLE);
}
holder.name.setText("text");
holder.name.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
user.setClicked(true);
notifyDataSetChanged();
}
});
return convertView;
}
private static class ViewHolder {
CircleImageView pic;
TextView name;
}
}
I am using a custom Listview with Custom views as list elements, each list item is a separate Custom view. The problem which I am facing is , click event on button in 1st item of ListView is not getting fired when we click on it. After clicking on it if we click somewhere else in the screen the click event get fires. I am not able to find the solutions for it and I am struggling with it. Any help will be highly appreciated.
Updated with code: Here is the getview method
public override View GetView(int position, View convertView, ViewGroup parent)
{
if (position == 0)
{
convertView = this.context.LayoutInflater.Inflate(Resource.Layout.home_hero_container, null);
this.heroSection = convertView.FindViewById<FrameLayout>(Resource.Id.heroContainer);
this.setHeroCard();
}
else
{
convertView = (View)GetItem(position - 1);
}
return convertView;
}
GetItem returns CustomView. 1st item will be the hero layout, after that all the Customviews will be added to Convertview. Click event is not working on the 1st item after hero.
Update with my answer:
Instead of assigning Customview directly on to Convertview I inflated a FrameLayout and add Customview to FrameLayout. Now I don't have click issue.
try this it will work
public class ContactsAdapter extends BaseAdapter {
ArrayList<ContactInfo> mlist;
Context mcontext;
public BluetoothChatadpter(Context context,ArrayList<ChatInfo> mchtlist) {
mlist = mchtlist;
mcontext = context;
}
#Override
public int getCount() {
return mlist.size();
}
#Override
public Object getItem(int postion) {
return mlist.get(postion);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertview, ViewGroup viewgroup){
View view = null;
if(convertview == null){
LayoutInflater inflater = context.getLayoutInflater();
view = inflater.inflate(R.layout.contactrow, null);
ContactHolder holder = new ContactHolder();
holder.txtviewfirstname = (TextView)view.findViewById(R.id.firstname);
holder.txtviewphone = (TextView)view.findViewById(R.id.phone);
holder.chkselected = (CheckBox)view.findViewById(R.id.check);
setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// to open the selected file in resp
// do your work here
}});
chkselected .setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// Toast.makeText(context,// "checked is clicke="+pos, 12).show();
if (chkselected.isChecked())
{
// do your work here
} else {
// do your work here
}
}
});
view.setTag(holder);
}
else{
view = convertview;
}
ContactHolder holder2 = (ContactHolder) view.getTag();
holder2.txtviewfirstname.setText(list.get(position).firstname);
holder2.txtviewphone.setText(list.get(position).phonenumber);
holder2.chkselected.setChecked(list.get(position).selected);
return view;
}
}
public class ContactsAdapter extends BaseAdapter {
ArrayList<ContactInfo> mlist;
Context mcontext;
public BluetoothChatadpter(Context context,ArrayList<ChatInfo> mchtlist) {
mlist = mchtlist;
mcontext = context;
}
#Override
public int getCount() {
return mlist.size();
}
#Override
public Object getItem(int postion) {
return mlist.get(postion);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertview, ViewGroup viewgroup){
View view = null;
if(convertview == null){
LayoutInflater inflater = context.getLayoutInflater();
view = inflater.inflate(R.layout.contactrow, null);
ContactHolder holder = new ContactHolder();
holder.txtviewfirstname = (TextView)view.findViewById(R.id.firstname);
holder.txtviewphone = (TextView)view.findViewById(R.id.phone);
holder.chkselected = (CheckBox)view.findViewById(R.id.check);
holder.chkselected.setOnCheckChangeListener(new CheckchangeListener() );
view.setTag(holder);
}
else{
view = convertview;
}
ContactHolder holder2 = (ContactHolder) view.getTag();
holder2.txtviewfirstname.setText(list.get(position).firstname);
holder2.txtviewphone.setText(list.get(position).phonenumber);
holder2.chkselected.setChecked(list.get(position).selected);
return view;
}
class CheckchangeListener implements OnCheckedChangeListener {
public CheckchangeListener() {
// TODO Auto-generated constructor stub
}
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
if (isChecked) {
// do your work here
} else {
// do your work here
}
}
}
}
You can try to set onClick event in your Custom adapter and if you have time then check this tutorial for reference - http://androidforbeginners.blogspot.it/2010/03/clicking-buttons-in-listview-row.html
I have a custom listview and i am selecting multiple items in the list, now i need to get those item names when clicking on a button the button will calls listfunction() method, you can see this in the below code.
public class Places extends Activity {
private ListView listView;
private static int selectedListItem = -1;
private Handler mHandler = new Handler();
private static 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");
data.add("six");
listView = (ListView)findViewById(R.id.ListView01);
listView.setDivider(null);
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//System.out.println(EfficientAdapter.saveState.get(position));
selectedListItem = position;
((EfficientAdapter)listView.getAdapter()).notifyDataSetChanged();
if(EfficientAdapter.saveState.get(position)=="selected"){
EfficientAdapter.saveState.put(position,"unselected");
}else
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);
}
});
listView.setAdapter(new EfficientAdapter(getApplicationContext()));
}
private static class EfficientAdapter extends BaseAdapter {
private LayoutInflater mInflater;
static HashMap<Integer,String> saveState=new HashMap<Integer,String>();
public EfficientAdapter(Context context) {
mInflater = LayoutInflater.from(context);
for(int i=0;i<data.size();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;
}
public void listfunction(View button) {
System.out.println("listfunction items are::::"+selectedListItem);
}
}
Custom listview should have custom adapter. In custom adapter you can find method named getView() along with view, position and parentgroup. When you are attempting click event, position will represent the index of the currently selected item.
public class MyArrayAdapter extends ArrayAdapter<String> {
#Override
public View getView(final int position, View convertView,ViewGroup parent) {
}
}
Use a global array to store the index of all the items clicked or touched in the list view by using the following:
listview.setOnItemClickListener(
new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> p, View view, int position,long id) {}
}
);
where position will be the index of the item starting from 0.then after clicking on button use the array to retrieve the positions of the items clicked
i use the following code to list few items from array , while scrolling the list view items is append more with same data exit in array i don`t know what mistake i made.
Anyone pointed out where i made the mistake.
private static String array_spinner_subcategories[];
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.sub_categories);
setSubCat();
ListView sub_categories=(ListView)findViewById(R.id.sub_catlist);
sub_categories.setAdapter(new EfficientAdapter(this));
sub_categories.setAdapter(adapter);
sub_categories.setOnItemClickListener(subcatlistener);
cr=getContentResolver();
}
public String[] setSubCat(){
recordDB=new Viddatabase(this);
db=recordDB.getWritableDatabase();
array_spinner_subcategories=recordDB.subcategoriesList(db);
recordDB.close();
return array_spinner_subcategories;
}
private OnItemClickListener subcatlistener = new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position,
long id) {
System.gc();
Toast.makeText(SubCategories.this,array_spinner_subcategories[position],
Toast.LENGTH_LONG).show();
}
};
private static class EfficientAdapter extends BaseAdapter {
private LayoutInflater mInflater;
public EfficientAdapter(Context context) {
mInflater = LayoutInflater.from(context);
}
public int getCount() {
return array_spinner_subcategories.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
System.gc();
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.albumlist, null);
holder = new ViewHolder();
holder.subCategories = (TextView) convertView.findViewById(R.id.albumDetails);
holder.subCategories.setText(array_spinner_subcategories[position]);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
return convertView;
}
static class ViewHolder {
TextView subCategories;
}
}
Move the holder.subCategories.setText(array_spinner_subcategories[position]); line in getView() method below the else block
In your getView method, you are only setting data in the view when you create a new view.
The ListView recycles views, so you will very likely be passed in a a view to reuse, which is why it is referred to as a convertview.
You need to be calling setText on the view every time this is called, otherwise you are just handing back the convertview unchanged and thus you are getting the same values repeated.