I'm trying to change the color of buttons in the rows of listview by clicking a button. Here I've a listview composed of 25 rows and each rows contains a button. I've used custom adapter for listview to place button in each row. I like to know how to do following things:
Click on a button and its color is changed.
After step (1) the onClickListener also change the color of the left-over buttons in the listview to normal.
The above two steps should happen to all of the buttons means if any of the button is clicked then it should be colored and remaining will turn back to normal.
the listview is like
Button 1
Button 2
Button 3
Button 4
You have to access your Button from Adapter. Follow it -
public class TestAdapter extends BaseAdapter {
private Context ctx;
private List<TestList> list;
public TestAdapter(Context ctx, List<TestList> list){
this.ctx = ctx;
this.list = list;
}
#Override
public int getCount() {
return list.size();
}
#Override
public TestList getItem(int position) {
return list.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.layout_item, parent, false);
}
Button button = (Button) convertView.findViewById(R.id.button);
button.setText(list.get(position));
return convertView;
}
}
Use onclick listener for button in your custom adapter
public class CustomAdapter extends BaseAdapter {
private Context ctx;
private List<CustomList> list;
public CustomAdapter(Context ctx, List<CustomList> list){
this.ctx = ctx;
this.list = list;
}
#Override
public int getCount() {
return list.size();
}
#Override
public CustomList getItem(int position) {
return list.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.layout_item, parent, false);
}
Button button = (Button) convertView.findViewById(R.id.button);
button.setText(list.get(position));
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
\\change color here
}
});
return convertView;
}
}
Related
this is my Adapter class
public class GridViewCustomAdapter extends BaseAdapter {
private List<ProductParameterBO> availList;
private LayoutInflater inflater;
Context context;
public GridViewCustomAdapter(Context ctx,List<ProductParameterBO> list){
this.context = ctx;
this.availList = list;
}
#Override
public int getCount() {
return availList.size();
}
#Override
public Object getItem(int position) {
return availList.get(position);
}
#Override
public long getItemId(int position) {
ProductParameterBO c = availList.get(position);
// long id = c.getTimeId();
return 0;
}
#Override
public View getView(final int position,View convertView,final ViewGroup parent) {
View row = convertView;
final TeeTimeHolder holder;
if (row == null){
inflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.grid_row,parent,false);
holder = new TeeTimeHolder();
holder.myImage =(ImageView)row.findViewById(R.id.imageView10);
holder.imageflip =(ImageView)row.findViewById(R.id.img_flp);
holder.name =(TextView)row.findViewById(R.id.textView47);
holder.edit =(TextView)row.findViewById(R.id.textView49);
holder.rl =(RelativeLayout)row.findViewById(R.id.grid_img_ovrly);
// holder.row =(RelativeLayout)row.findViewById(R.id.row);
row.setTag(holder);
}
else {
holder =(TeeTimeHolder)row.getTag();
}
holder.name.setText(availList.get(position).getParameterName());
holder.myImage.setImageResource(R.drawable.toi);
holder.rl.setVisibility(View.GONE);
return row;
}
static class TeeTimeHolder {
ImageView myImage,imageflip;
TextView name,edit;
RelativeLayout rl;
}
}
i just want to show Relativelayout r1 when row is selected and when i select the next row ,previous row must hide Relativelayout r1 layout and show on row which is selected.. Now in my case when i select the next row ,then its not hiding from previous row ..layout still showing on all row which i select.
Please help me to sort out this problem..any help would be appreciated in advanced..
int priviousPosition=0;
ArrayList<View> viewList= new ArrayList<View>();
holder.rl.setTag(position);
viewList.add(holder.rl);
row.findViewById(R.id.grid_img_ovrly).setOnClickListener(new OnClickListener() {
public void onClick(View v1) {
View v=viewList.get(priviousPosition);
v.setVisibility(View.GONE);
int postition=(Integer) v1.getTag();
priviousPosition=postition;
notifyDataSetChanged();
}
});
I have created a standard listview and I have populated it with text. Is it possible to add one or two buttons to each row which will then perform some sort of function?
Yes, its possible you need to create custom layout for the row,
see this
example
You just use this custom adapter in your Listview creation and then you will write your code in this block...
use this code in your activity...
CustomAdapter mAdapter = new CustomAdapter(this, R.layout.listitem, mListItems);
mPullRefreshListView.setAdapter(mAdapter);
And then here you replace your Layout....
public class CustomAdapter extends ArrayAdapter<Sample> {
public ArrayList<Sample> mlist;
public Context context;
public LayoutInflater inflater;
public CustomAdapter(Context context, int resource, ArrayList<Sample> mlist) {
super(context, resource);
this.mlist = mlist;
this.context = context;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getPosition(Sample item) {
return super.getPosition(item);
}
#Override
public Sample getItem(int position) {
return mlist.get(position);
}
#Override
public int getCount() {
return mlist.size();
}
#Override
public long getItemId(int position) {
return super.getItemId(position);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = inflater.inflate(R.layout.listitem, null);// Replace your
// layout....
TextView text1 = (TextView) convertView.findViewById(R.id.item1);
TextView text2 = (TextView) convertView.findViewById(R.id.item2);
text1.setText(mlist.get(position).getListitem1());
text2.setText(mlist.get(position).getListitem2());
text2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// you just put your Logic here And use this custom adapter to
// load your Data By using this particular custom adapter to
// your listview
// Change your imageview here
}
});
return convertView;
}
}
I want to access a button from an activity in a lazyadapter's getview( that is to make clickable from lazyadapter) is there any method to do that? The button click brings the user to the next screen. Please if someone has any idea or code.
You just follow this code on get your Click function and call your next activity in the onclick listener....
CustomAdapter mAdapter = new CustomAdapter(this, R.layout.listitem, mListItems);
mPullRefreshListView.setAdapter(mAdapter);
public class CustomAdapter extends ArrayAdapter<Sample> {
public ArrayList<Sample> mlist;
public Context context;
public LayoutInflater inflater;
public CustomAdapter(Context context, int resource, ArrayList<Sample> mlist) {
super(context, resource);
this.mlist = mlist;
this.context = context;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getPosition(Sample item) {
return super.getPosition(item);
}
#Override
public Sample getItem(int position) {
return mlist.get(position);
}
#Override
public int getCount() {
return mlist.size();
}
#Override
public long getItemId(int position) {
return super.getItemId(position);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = inflater.inflate(R.layout.listitem, null);
TextView text1 = (TextView) convertView.findViewById(R.id.item1);
TextView text2 = (TextView) convertView.findViewById(R.id.item2);
text1.setText(mlist.get(position).getListitem1());
text2.setText(mlist.get(position).getListitem2());
text2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// you just put your Logic here And use this custom adapter to
// load your Data By using this particular custom adapter to
// your listview
}
});
return convertView;
}
}
i am displaying items in listview and when i click on list navigate to next activity but when i scroll down in list view and then click on list its not work means list is not clicking i am very wondering why this happening..pls some help me
thanks.
my code..for showing in list view is...
class MyAdapter extends BaseAdapter implements OnClickListener
{
private LayoutInflater inflater;
public MyAdapter(Context ctx) {
super();
this.inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return totalartist;
}
/* Not implemented but not really needed */
#Override
public Object getItem(int position) {
return null;
}
/* Not implemented but not really needed */
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View ConvertView, ViewGroup parent)
{
View v = inflater.inflate(R.layout.listitem_layout, parent, false);
// Log.i("array galoijewdh..",keywordresulttab.array_galleryname[position]);
// Variables.a=3;
try{
String gallerynames = keywordsearch.array_galleryname[position];
String addresses = keywordsearch.array_address[position];
TextView tv = (TextView) v.findViewById(R.id.barrio);
tv.setText(gallerynames);
tv = (TextView) v.findViewById(R.id.ciudad);
tv.setText(addresses);
((BaseAdapter)(getListAdapter())).notifyDataSetChanged();
// return v;
}catch(NullPointerException e){}
return v;
}
#Override
public void onClick(DialogInterface dialog, int which)
{
}
}
}
Refer this tutorial
problem seems to be in your view the way you are using.
I have in my app a ListView with adapter, I use this to show a list with a chechbox and a textView
public class MyListAdapter extends ArrayAdapter<Model> {
private LayoutInflater inflater;
private int position;
public int getPosition() {
return position;
}
public void setPosition(int position) {
this.position = position;
}
public MyListAdapter (Context context, List<Model> listMeasurement){
super(context, R.layout.simplerow, R.id.empty, listMeasurement);
inflater= LayoutInflater.from(context);
}
public View getView(int position, View convertView, ViewGroup parent){
Model model= (Model)this.getItem(position);
CheckBox checkBox;
TextView textView;
}
}
My question is:
I want to show another list in another activity, this will have an image, two textViews and a button. The image depends on the value of the textView.
The best way to do this is do other ArrayAdapter? or use other things?
Thanks in advance.
You need to extend the BaseAdapter and override the getView() method. Here is a sample.
private class CustomAdapter extends BaseAdapter {
private LayoutInflater inflater;
private ArrayList<Model> list;
public CustomAdapter(Context context, ArrayList<Model> list) {
this.inflater = LayoutInflater.from(context);
this.list = list;
}
#Override
public int getCount() {
return list.size();
}
#Override
public Object getItem(int position) {
return list.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View view, ViewGroup parent) {
// If the view is null inflate it from xml
if (view == null)
view = inflater.inflate(R.layout.list_row, null);
// Bind xml to java
ImageView icon = (ImageView) view
.findViewById(R.id.image);
TextView text = (TextView) view.findViewById(R.id.text);
text.setText(list.get(position).getText());
icon.setImageDrawable(list.get(position).getDrawable());
return view;
}
}