I have this on my main activity:
listView.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> av, View v, int position,
long id) {
TextView wrapper = (TextView) v.findViewById(R.id.title);
VideoModel o = (VideoModel) videoAdapter.getItem(position);
wrapper.setText(o.title);
}
});
This code (as you would guess) changes the textview of a list view row (which is a layout).
The problem is that for some reason when I click on the first row, the text changes, but it also changes on the third row (with the same text).
Any toughts?
If you want to see the whole code, refer to How to call method from activity to change layout?
Don't change Views in setOnItemClickListener instead change your data and notify adapter so that adapter will change your UI.
Example:
Model
class VideoModel {
public String title;
public String videoUrl;
public String imageUrl;
public boolean isTitleVisible;
}
Adapter
public class VideoAdapter extends BaseAdapter{
#Override
public View getView(final int position, View convertView, ViewGroup parent)
{
VideoLayout view;
if (convertView == null) {
view = (VideoLayout) LayoutInflater.from(context).inflate(R.layout.video, parent, false);
}else{
view = (VideoLayout) convertView;
}
VideoModel item = (VideoModel) getItem(position);
if (item != null) {
view.prepare(item);
if(item.isTitleVisible){
view.changeTitle("");
}
}
return view;
}
}
On click listener:
#Override
public void onItemClick(AdapterView<?> av, View v, int position,
long id) {
videoItems.get(position).isTitleVisible=! videoItems.get(position).isTitleVisible;
videoAdapter.notifyDatasetChange();
}
This is just an example in which you change your view model and just notify adapter to change the view in onITemClick
Related
I have a custom adapter which is supposed to contain image buttons. However, I am bit confused on the implementation of the override for the getView() method. Since my image buttons are defined dynamically, I am able to recover an image button by using the code
#Override
public View getView(int i, View view, ViewGroup viewGroup){
ImageButton ibutton = (ImageButton) getItem(i);
How do I return its view? I have not specifically created an xml file for it since it is just an ImageButton (not in combination with anything else), but is it necessary to create an xml for it? Or is there a way to easily get the view from the imagebutton itself.
When I try this for getView(), the imagebutton is not clickable for some reason.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageButton imageButton = getItem(position);
return imageButton ;
}
Try to build you adapter something like this:
public class ImageButtonAdapter extends BaseAdapter {
private Context mContext;
// Constructor
public ImageButtonAdapter(Context c) {
mContext = c;
}
public int getCount() {
return listCount;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageButton for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageButton imageButton ;
if (convertView == null) {
imageButton = new ImageButton (mContext);
imageButton.setLayoutParams(lp);
}
else
{
imageButton = (ImageButton ) convertView;
}
imageButton.setBackgroundColor(Color.TRANSPARENT)
return imageButton ;
}
}
public class SingleListAdapter extends BaseAdapter {
public Context ctx;
LayoutInflater lInflater;
List<String> data;
public SingleListAdapter(Context context, List<String> data) {
ctx = context;
this.data = data;
lInflater = (LayoutInflater) ctx
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return data.size();
}
#Override
public Object getItem(int position) {
return data.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
view = lInflater.inflate(R.layout.single_choice_items, parent, false);
}
((TextView) view.findViewById(R.id.singleitemId)).setText(data.get(position));
return view;
}
}
This is my adapter class :
String[] data = {"10-11 Am", "11-12Am", "12-2Pm", "2-4PM", "4-6Pm"};
List<String> list = new ArrayList<String>();
for (int i = 0; i < data.length; i++) {
list.add(data[i]);
}
SingleListAdapter adapter = new SingleListAdapter(this, list);
listview.setAdapter(adapter);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
valueoflist= data[i];
}
});
this is my lsitview itemclick function i have textview and checkbox in listview adapter i am printing value in a textview and i am able to get data of Particular item on listview item click i want when i click on item list then that particular check box should enable please suggest me how we can do this like and i want single choice checked item click please help me suggest me how we can do this .
In onItemClick method you can call your checkBox object and you can work with it.
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
valueoflist= data[i];
CheckBox chk = (CheckBox)view.findViewById(R.id.your_check_box_id);
chk.setChecked(true);
}
});
I hope it will help you
Try this:
listview.setOnItemClickListener(new OnMyItemClickListener());
and declare:
private class OnMyItemClickListener implements
ListView.OnItemClickListener {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
valueoflist= data[i];
CheckBox chk = (CheckBox)view.findViewById(R.id.your_check_box_id);
chk.setChecked(true);
}
}
But.. a question.. can you show the listview initialize?
I have an Activity with a ListView. This ListView is set with the Class GetAllEntrysListViewAdapter:
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ListCell cell;
if (convertView == null) {
convertView = inflater.inflate(R.layout.get_all_entry_list_view_cell, null);
cell = new ListCell();
cell.note = (TextView) convertView.findViewById(R.id.listViewNote);
cell.img = (ImageView) convertView.findViewById(R.id.listViewImg);
cell.likeButton = (ImageButton) convertView.findViewById(R.id.heartImage);
convertView.setTag(cell);
}
else {
cell = (ListCell)convertView.getTag();
}
public static class ListCell {
private TextView note;
private ImageView img;
public ImageButton likeButton;
}
In the Activity I now want to change the Image of likeButton on the certain list entry that is clicked, but how can I reach this certain element? Her is what I have until now:
GetAllEntrysListViewAdapter.ListCell listCell;
listCell = new GetAllEntrysListViewAdapter.ListCell();
getALlEntrysListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
// Get the likeButton of this entry and change the Image
Try this code. Replace your
getALlEntrysListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
// Get the likeButton of this entry and change the Image
}
with
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
//first get the elements by doing like
TextView note = (TextView)view.findViewById(R.id.listViewNote);
ImageView img = (ImageView) view.findViewById(R.id.listViewImg);
ImageButton likeButton = (ImageButton) convertView.findViewById(R.id.heartImage);
likeButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
likeButton.setBackgroundResource(R.id.icon);
}
});
}
This will be helpful ...thanks
That part should be done in getView() method itself. It should work like this.
Set data in your Adapter
Display data in getView for each item
Once you Like any item, update data with new Like status
Now call notifyDatasetChanged() on your adapter, getView() will update item view accordingly.
You should read ListAdapter first, and see some examples how it works.
I have a listview with a textview and spinner which has same values for every spinner in the list.both values for textview and spinner are separately in xml files. how can i get selected spinner value for each row in the spinner..according to the relative textview..i m totally beginner to android ..
In your WhateverAdapter add these methods:
public void setOnSpinnerItemSelectedListener(OnItemSelectedListener listener){
this.mSpinnerItemSelectedListener = listener;
}
In your getView() method:
public View getView(int position, View convertView, ViewGroup parent){
//...
viewHolder.spinner.setOnItemSelectedListener(mSpinnerItemClickListener);
//.....
return convertView;
}
Then in your Activity or wherever:
MyAdapter adapter = new MyAdapter(Context....);
adapter.setOnSpinnerItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
// your code here
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
myListView.setAdapter(adapter)
I am using a Listview. before implementing OnLongClick, my onListItemClick was working perfectly, however now, after implementing OnLongClick the long clicks work and normal list clicks don't do anything. It seems to hide exposure to the onListItemClick() function you already have working
can anyone see why/ suggest a solution?
public class CombChange extends ListActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ListEdit(this, symbols));
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
String selectedValue = (String) getListAdapter().getItem(position);
if (lastPressed.equals(selectedValue) ){
count++;}
}
public class ListEdit extends ArrayAdapter<String> implements OnLongClickListener{
private final Context context;
private final String[] values;
public ListEdit(Context context, String[] values) {
super(context, R.layout.activity_comb_change, values);
this.context = context;
this.values = values;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.activity_comb_change, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.label);
ImageView imageView = (ImageView) rowView.findViewById(R.id.logo);
textView.setText(values[position]);
rowView.setOnLongClickListener(new OnLongClickListener(){
public boolean onLongClick(View arg0) {
context.startActivity(new Intent(context,RestoreOriginal.class));
return false;
}
});
// Change icon based on name
String s = values[position];
if (s.equals("a")) {
imageView.setImageResource(R.drawable.a);
return rowView;
}
}
I think you shouldn't do rowView.setOnLongClickListener.
Try something likes this:
this.getListView().setLongClickable(true);
this.getListView().setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> parent, View v, int position, long id) {
// whatever you wanna do
return true;
}
});
I took the code from how to capture long press event for Listeview item of a ListActivity?
Hope this helps.