I have use this class extends base adapter display values. Below code working eclipse correctly. but me newly created project using Androi Studio there this code getview function not call or working.
i also check getcount . getcount values available.
public class CustomAdapterChatActivity extends BaseAdapter {
ArrayList<ChatUsersDetailsBean> mBeans = new ArrayList<ChatUsersDetailsBean>();
Context mcontext;
LayoutInflater inflater;
CommonUtil commonUtil;
public CustomAdapterChatActivity(Context context,
ArrayList<ChatUsersDetailsBean> mBeans) {
// TODO Auto-generated constructor stub
mcontext = context;
this.mBeans = mBeans;
commonUtil = new CommonUtil(context);
inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return mBeans.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public class Holder {
TextView name;
TextView content;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Log.e("getView",""+"getView");
if (convertView == null) {
convertView = inflater.inflate(
R.layout.chat_listview_layout_screen, null);
}
setAttributes(position, convertView);
convertView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
click(position);
}
});
return convertView;
}
public void click(int position) {
Intent mintent = new Intent(mcontext, ChatViewBackgroundActivity.class);
mintent.putExtra("name", mBeans.get(position).getOpponent_name());
((ChatActivity) mcontext).startActivityForResult(mintent, 1);
}
public void setAttributes(final int position, View convertView) {
Holder holder = new Holder();
holder.imgdispatcher = (ImageView) convertView
.findViewById(R.id.chat_list_iv_profilepic);
}
Please help me. i struggle this part.
No error will be displayed.
i check getview run or not that time getview not working anytime.
this code any changes needed
Change your getView() method in your adapter class like below code:
public View getView (final int position, View convertView, ViewGroup parent){
if( convertView == null ){
//We must create a View:
convertView = inflater.inflate(R.layout.my_list_item, parent, false);
}
//Here we can do changes to the convertView
return convertView;
}
While adding newly inflated view, try to avoid passing null as view root. Lint will now warn you not to pass in null for root. Your app won’t crash in this scenario, but it can misbehave. When your child View doesn’t know the correct LayoutParams for its root ViewGroup, it will try to determine them on its own using generateDefaultLayoutParams.
#AbiK If you already done like I mentioned on above comment means, use below code of modified getView() method and setAttributes() method. I hope It works for you.
public class CustomAdapterChatActivity extends BaseAdapter {
ArrayList<ChatUsersDetailsBean> mBeans;
Context mcontext;
LayoutInflater inflater;
CommonUtil commonUtil;
public CustomAdapterChatActivity(Context context,
ArrayList<ChatUsersDetailsBean> mBeans) {
mcontext = context;
this.mBeans = mBeans;
commonUtil = new CommonUtil(context);
}
#Override
public int getCount() {
return mBeans.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public class Holder {
TextView name;
TextView content;
ImageView imgdispatcher;
}
public Holder setAttributes(final int position, View convertView) {
Holder holder = new Holder();
holder.imgdispatcher = (ImageView) convertView.findViewById(R.id.chat_list_iv_profilepic);
// holder.name=(TextView)convertView.findViewById(R.id.); // declare Id
// holder.content=(TextView)convertView.findViewById(R.id.); // declare Id
return holder;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
Holder holder = null;
LayoutInflater inflater = (LayoutInflater)
mcontext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.chat_listview_layout_screen, parent, false);
holder = setAttributes(position, convertView);
convertView.setTag(holder);
} else {
holder = (Holder) convertView.getTag();
}
convertView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
click(position);
}
});
return convertView;
}
public void click(int position) {
Intent mintent = new Intent(mcontext, ChatViewBackgroundActivity.class);
mintent.putExtra("name", mBeans.get(position).getOpponent_name());
((ChatActivity) mcontext).startActivityForResult(mintent, 1);
}
}
Related
I have an expandable list view. In the child item of that is a gridview. Inside gridview there are custom checkboxes.
Now the problem is when I check a checkbox and scroll down, or even close the parent view, the check is gone.
Something like this:
Screen checked before scroll:
Screen checked after scroll:
Here is my ExpandableListView Adapter:
public class CreateProfileExpandableAdapter extends BaseExpandableListAdapter {
ArrayList<CreateProfileFecetsHeaderModel> al;
Activity activity;
public CreateProfileExpandableAdapter(Activity activity, ArrayList<CreateProfileFecetsHeaderModel> al) {
this.activity = activity;
this.al = al;
}
#Override
public Object getChild(int groupPosition, int childPosition) {
return al.get(groupPosition).getChildren();
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return al.get(groupPosition).hashCode();
}
#Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater inflater = (LayoutInflater) activity.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.create_profile_child, parent, false);
}
ExpandableHeightGridView gridViewChildren = (ExpandableHeightGridView) v.findViewById(R.id.gridViewChildren);
gridViewChildren.setExpanded(true);
CreateProfileChildAdapter createProfileChildAdapter = new CreateProfileChildAdapter(activity, al.get(groupPosition).getChildren());
gridViewChildren.setAdapter(createProfileChildAdapter);
return v;
}
#Override
public int getChildrenCount(int groupPosition) {
return 1;
}
#Override
public Object getGroup(int groupPosition) {
return al.get(groupPosition);
}
#Override
public int getGroupCount() {
return al.size();
}
#Override
public long getGroupId(int groupPosition) {
return al.get(groupPosition).hashCode();
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater inflater = (LayoutInflater) activity.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.create_profile_group, parent, false);
}
TextView textViewGroup = (TextView) v.findViewById(R.id.textViewGroup);
textViewGroup.setText(al.get(groupPosition).getText());
return v;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
Gridview Adapter:
public class CreateProfileChildAdapter extends BaseAdapter {
Activity activity;
ArrayList<CreateProfileFecetsChildModel> al;
int index = -1;
public CreateProfileChildAdapter(Activity activity, ArrayList<CreateProfileFecetsChildModel> al) {
// TODO Auto-generated constructor stub
this.activity = activity;
this.al = al;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return al.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return al.get(position);
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
private static class ViewHolder {
CheckBox checkBoxChild;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
final ViewHolder viewHolder;
final int pos = position;
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(activity);
convertView = inflater.inflate(R.layout.create_profile_grid_child, parent, false);
viewHolder = new ViewHolder();
viewHolder.checkBoxChild = (CheckBox) convertView.findViewById(R.id.checkBoxChild);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.checkBoxChild.setText(al.get(position).getTitle());
viewHolder.checkBoxChild.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (b) {
viewHolder.checkBoxChild.setTextColor(activity.getResources().getColor(R.color.white));
} else {
viewHolder.checkBoxChild.setTextColor(activity.getResources().getColor(R.color.black));
}
}
});
return convertView;
}
}
The problem is that the Views get recycled without having their state reset. Imagine displaying 20 items on a screen which only shows four items at once, meaning that the GridViewAdapter uses four, maybe five views. Those views take turns displaying your item when scrolling trough the list:
Solution:
You have to store everything about your view's state (in your case: what is marked as checked) in your Adapter's child list entries of the type CreateProfileFecetsHeaderModel.
In your Adapter's getChildView() fetch the checked state from your Adapter's children using the position and set the holder's CheckBox accordingly.
As it is Expandable adapter, u need to know the groupid,selectedchild,id.
try as below...
#Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final Pair<Long, Long> tag = new Pair<Long, Long>(
getGroupId(groupPosition),
getChildId(groupPosition, childPosition));
ViewHolder viewHolder = null;
if (convertView == null) {
convertView = inflater.inflate(R.layout.item_countryoforigin, null);
viewHolder = new ViewHolder(convertView);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.checkBox.setTag(tag);
viewHolder.checkBox.setChecked(mCheckedItems.contains(tag));
viewHolder.textview.setText((CharSequence) getText(groupPosition, childPosition));
viewHolder.checkBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final CheckBox cb = (CheckBox) v;
final Pair<Long, Long> tag = (Pair<Long, Long>) v.getTag();
if (cb.isChecked()) {
mCheckedItems.add(tag);
} else {
mCheckedItems.remove(tag);
}
}
});
return convertView;
}
public Set<Pair<Long, Long>> getCheckedItems() {
return mCheckedItems;
}
u can call getcheckitems .....
mycustom adapter which extends from baseadapter but when i run my main java file it shows main activity no
list view item
public class MyCustomadapter extends BaseAdapter{
private Context mycontext;
String[] mystringlist;
public MyCustomadapter(Context context,String[] strText) {
this.mycontext=context;
this.mystringlist = strText;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return mystringlist.length;
}
#Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
LayoutInflater inflater = (LayoutInflater) mycontext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.each_row, parent,false);
TextView tv = (TextView) rowView.findViewById(R.id.rowtxt);
tv.setText(mystringlist[position]);
return rowView;
}
}
mycustom adapter which extends from baseadapter but when i run my main java file it shows main activity no
list view item
problem:
return 0;
You are return 0 means there would be no list items to be displayed in your ListView thus giving you 0 result.
You need to return the number of item in your array or the length of it.
sample:
#Override
public int getCount() {
// TODO Auto-generated method stub
return mystringlist.length;
}
#Override
public int getCount()
{
// TODO Auto-generated method stub
return mystringlist.length();
}
My idea is extending ArrayAdapter, Have a look at the following codes
public class MyAdapter extends ArrayAdapter<String>{
public MyAdapter(Context context, String[] items) {
super(context, 0, items);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// your codes for generating rows
String thisItem = getItem(position);
}
}
You're done, also you're no longer need to override other methods!
Just as an addendum your getItem() method is incorrect. It should read as follows:
#Override
public Object getItem(int arg0) {
return mystringlist[position];
}
I have a Button inside a ListView.
I want to go to another Activity when it is clicked.
But I get this error:
No enclosing instance of the type Appointment is accessible in scope
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.item_row, null);
TextView txtsection = (TextView)vi.findViewById(R.id.section);
TextView txtdoctor = (TextView)vi.findViewById(R.id.doctor);
TextView txtdate = (TextView)vi.findViewById(R.id.date);
TextView txttime = (TextView)vi.findViewById(R.id.time);
btchange = (Button)vi.findViewById(R.id.change);
btchange.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(context, Available.class);
startActivity(i);
}
});
btdelete = (Button)vi.findViewById(R.id.delete);
txtsection.setText(item.section);
txtdoctor.setText(item.doctor);
txtdate.setText(item.date);
txttime.setText(item.time);
return vi; }}
At line
Intent i = new Intent(Appointment.this,Available.class);
Appointment.this is valid only if the adaptar is an inner class of Appointment.
If is not the case, use the Context passed to the adapter.
Intent i = new Intent(context, Available.class);
Declare a private field, named context, in your adapter:
private Context context;
In the adapter constructor, assign the context passed to it:
this.context = context;
Change the getView method to this:
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if( convertView == null ){
convertView = inflater.inflate(R.layout.yourListLayout, parent, false);
}
Button btchange = (Button)convertView.findViewById(R.id.yourbuttonid);
btchange.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(context, Available.class);
startActivity(i);
}
});
return convertView;
}
EDIT
btChange also need to point to the button that has clicked as playmaker420 said.
I edited the code to accomplish that.
EDIT 2
Change your code adapter to this:
package com.example.clinic;
public class CustomListViewAdapter extends BaseAdapter
{
private Context context;
Button btchange,btdelete;
LayoutInflater inflater;
List<ListViewItem> items;
public CustomListViewAdapter(Activity context, List<ListViewItem> items) {
super();
this.context = context;
this.items = items;
this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return items.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.item_row, null);
//add
ListViewItem item = items.get(position);
TextView txtsection = (TextView)vi.findViewById(R.id.section);
TextView txtdoctor = (TextView)vi.findViewById(R.id.doctor);
TextView txtdate = (TextView)vi.findViewById(R.id.date);
TextView txttime = (TextView)vi.findViewById(R.id.time);
btchange = (Button)vi.findViewById(R.id.change);
btchange.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(context, Available.class);
//updated
context.startActivity(i);
}
});
btdelete = (Button)vi.findViewById(R.id.delete);
txtsection.setText(item.section);
txtdoctor.setText(item.doctor);
txtdate.setText(item.date);
txttime.setText(item.time);
return vi;
}
Intent i = new Intent(Appointment.this,Available.class);
startActivity(i);
I'd like to create a spinner with a list of names, and a list of ids from many .jpg's
I have a class with a String[] wich contains the names, and a Integer[] with the ids of the drawables.
My main class is the next
public class Settings extends Activity{
private Button btnGuardar, btnCerrar;
private Spinner spOperadoras, spTarifas;
private Operadoras op = new Operadoras();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
btnGuardar = (Button)findViewById(R.id.buttonSave);
btnCerrar = (Button)findViewById(R.id.buttonClose);
spOperadoras = (Spinner)findViewById(R.id.spinnerOperador);
spTarifas = (Spinner)findViewById(R.id.spinnerTarifas);
OperadorAdapter adapter = new OperadorAdapter(this, R.layout.custom_spinner, op);
spOperadoras.setAdapter(adapter);
}
}
Then my adapter is:
public class OperadorAdapter extends BaseAdapter implements SpinnerAdapter{
Activity context;
int spLayoutId;
Operadoras operadoras;
public OperadorAdapter(Activity context, int id, Operadoras op){
//super(context, id, op);
this.context = context;
this.operadoras = op;
this.spLayoutId = id;
}
#Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
public View getCustomView(int position, View convertView, ViewGroup parent) {
View item = convertView;
TextView opName;
ImageView opIcon;
LayoutInflater inflater = context.getLayoutInflater();
item = inflater.inflate(spLayoutId, null);
opName = (TextView) item.findViewById(R.id.nombreOperador);
opIcon = (ImageView) item.findViewById(R.id.imageOperador);
opName.setText(operadoras.getOperador(position).toString());
opIcon.setImageResource(operadoras.getLogo(position));
return item;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return 0;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
But I have no results in my spinner.
What's the problem???
Thanks.
#Override
public int getCount() {
// TODO Auto-generated method stub
return 0;
}
Return your actual count.
I created custom adapter for listview which contain text and images, on click of particular list item I have to open another activity, but I am not able to fire listview click event, below is my code.
Thanks.
Adapter
public class ImageAndTextAdapter extends ArrayAdapter<String> {
private LayoutInflater mInflater;
private Context myCtx;
private String[] mStrings;
private TypedArray mIcons;
private int mViewResourceId;
public ImageAndTextAdapter(Context ctx, int viewResourceId,
String[] strings, TypedArray icons) {
super(ctx, viewResourceId, strings);
myCtx = ctx;
mInflater = (LayoutInflater)ctx.getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
mStrings = strings;
mIcons = icons;
mViewResourceId = viewResourceId;
}
#Override
public int getCount() {
return mStrings.length;
}
#Override
public String getItem(int position) {
return mStrings[position];
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = mInflater.inflate(mViewResourceId, null);
ImageView iv = (ImageView)convertView.findViewById(R.id.option_icon);
iv.setImageDrawable(mIcons.getDrawable(position));
TextView tv = (TextView)convertView.findViewById(R.id.option_text);
tv.setText(mStrings[position]);
return convertView;
}
}
Main acitivity
public class OurWishingWellActivity extends ListActivity implements OnClickListener{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.list_view_image);
Context ctx = getApplicationContext();
Resources res = ctx.getResources();
String[] options = res.getStringArray(R.array.reg_item_names);
TypedArray icons = res.obtainTypedArray(R.array.reg_icons);
setListAdapter(new ImageAndTextAdapter(ctx, R.layout.list_item,
options, icons));
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
Respected Aafag#
You have write onclick of listview like below.
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
Toast.makeText(AdvancedListViewActivity.this, "working", Toast.LENGTH_SHORT).show();
System.out.println("working!!!!!!!!!");
}
Add listener
#Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = mInflater.inflate(mViewResourceId, null);
ImageView iv = (ImageView)convertView.findViewById(R.id.option_icon);
iv.setImageDrawable(mIcons.getDrawable(position));
TextView tv = (TextView)convertView.findViewById(R.id.option_text);
tv.setText(mStrings[position]);
convertView .setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity();
}
});
return convertView;
}