i have fragment in which i have a listview , i was trying to show some data in the list . it is silly but its not working i have no idea what is wrong with it.
in my fragmnet in oncreateview im doing this
mContactsHomeAdapter = new ContactsHomeAdapter(getActivity());
mListView.setAdapter(mContactsHomeAdapter);
mContactsHomeAdapter.notifyDataSetChanged();
this is my adapter code
public class ContactsHomeAdapter extends BaseAdapter {
ExecutorService executorService;
private Context mContext;
LayoutInflater mInflater;
public ContactsHomeAdapter(Context pContext) {
mContext = pContext;
//executorService=Executors.newFixedThreadPool(10);
mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return 100;
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null){
convertView = mInflater.inflate(R.layout.contacts_adapter_item, null);
}
CheckBox lCheckbox = (CheckBox) convertView.findViewById(R.id.check_box);
TextView lContactTitle = (TextView) convertView.findViewById(R.id.contact_title);
TextView lContactEmail = (TextView) convertView.findViewById(R.id.contact_email);
ImageView lContactPhoto = (ImageView) convertView.findViewById(R.id.contact_photo);
TextView lContactNumber = (TextView) convertView.findViewById(R.id.contact_phonenumber);
lContactTitle.setText("hello");
// QueueItem lItem = new QueueItem() ;
// lItem.mId = ""+position;
// lItem.mName = lContactTitle;
// lItem.mNumber = lContactNumber;
// lItem.mEmail = lContactEmail;
// executorService.submit(new QueueRunner(lItem));
return convertView;
}
}
Related
I have looked at other threads and I cannot see what is wrong with my listadapter. I have the view holder pattern and I am setting all the values for the list item outside of the convertview null check statement. Im not sure what other things would cause it to repeat the items.
public class ScheduledJobListAdapter extends BaseAdapter {
private static LayoutInflater inflater = null;
private ArrayList<Job> jobList;
private Context context;
public ScheduledJobListAdapter(Context context, ArrayList<Job> jobList) {
this.context = context;
this.jobList = jobList;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() { return jobList.size(); }
#Override
public Job getItem(int position) { return jobList.get(position); }
#Override
public long getItemId(int position) { return 0; }
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ScheduledViewHolder holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_item_scheduled_job,null);
holder = new ScheduledViewHolder();
holder.job = getItem(position);
holder.container = convertView.findViewById(R.id.scheduled_job_layout);
holder.routeOrder = convertView.findViewById(R.id.route_order_textview);
holder.location = convertView.findViewById(R.id.location_textview);
holder.jobRef = convertView.findViewById(R.id.job_ref_textview);
holder.statusIndicator = convertView.findViewById(R.id.status_indicator);
convertView.setTag(holder);
} else {
holder = (ScheduledViewHolder) convertView.getTag();
}
holder.routeOrder.setText(holder.job.getRouteOrder() + "");
holder.location.setText(holder.job.getLocation());
holder.jobRef.setText(holder.job.getJobReference());
return convertView;
}
}
class ScheduledViewHolder {
Job job;
LinearLayout container;
TextView routeOrder;
TextView location;
TextView jobRef;
ImageView statusIndicator;
}
Here's the problem:
holder.job = getItem(position);
Remember the views may be recycled as you scroll, and the job assigned maybe used unintentionally for other positions if you assigned it that way. To fix it, simply assign the job after the if-else condition:
if (convertView == null) {
// ...
} else {
// ...
}
holder.job = getItem(position); // Update the job by position
holder.routeOrder.setText(holder.job.getRouteOrder() + "");
holder.location.setText(holder.job.getLocation());
holder.jobRef.setText(holder.job.getJobReference());
Hello my friends I have a problem. When I scroll down in my GridView and scroll up the order of the items changes. I want all itmes to stay on the same place. Below you can find my code. It would be nice if someone can help me out as this is a very irritating bug.
public class GridAdapter extends BaseAdapter {
Context context;
private final String [] data;
View view;
LayoutInflater layoutInflater;
int[] i = {
R.drawable.blue,
R.drawable.green,
R.drawable.yellow,
};
public GridAdapter(Context context, String[] data) {
this.context = context;
this.data = data;
}
#Override
public int getCount() {
return data.length;
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView==null){
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = new View(context);
view = layoutInflater.inflate(R.layout.single_item,null);
ImageView imageView = (ImageView) view.findViewById(R.id.imageView);
TextView textView = (TextView) view.findViewById(R.id.textView);
Random rand = new Random();
int n = rand.nextInt(i.length);
imageView.setImageResource(i[n]);
textView.setText(data[position]);
}else{
view = (View)convertView;
}
return view;
} }
to faster the performance use ViewHolder like below
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null) {
final LayoutInflater layoutInflater = LayoutInflater.from(mContext);
convertView = layoutInflater.inflate(R.layout.single_item, null);
final ImageView imageView = (ImageView)convertView.findViewById(R.id.imageView);
final TextView textView = (TextView)convertView.findViewById(R.id.textView);
final ViewHolder viewHolder = new ViewHolder(imageView, textView);
convertView.setTag(viewHolder);
}
Random rand = new Random();
int n = rand.nextInt(i.length);
final ViewHolder viewHolder = (ViewHolder)convertView.getTag();
viewHolder.imageView.setImageResource(i[n]);
viewHolder.textView.setText(data[position]);
return convertView;
}
private class ViewHolder {
private final TextView imageView;
private final TextView textView;
public ViewHolder(TextView imageView, TextView textView) {
this.imageView = imageView;
this.textView = textView;
}
}
It worked for me after adding those 2 lines:
#Override
public int getViewTypeCount() {
return getCount();
}
#Override
public int getItemViewType(int position) {
return position;
}
I am displaying a listview in which, the row item has two textview. The second textview is for the name of companies and the first textview is for the starting letter of the companies. How this can be achieved ? Need help !!
public class ExampleAdapter extends BaseAdapter {
Context context;
ArrayList<ExamplePojo> items = new ArrayList<>();
public ExampleAdapter(Context context, ArrayList<ExamplePojo> items) {
this.context = context;
this.items = items;
}
#Override
public int getCount() {
return items.size();
}
#Override
public Object getItem(int position) {
return items.get(position);
}
#Override
public long getItemId(int position) {
return items.indexOf(items.get(position));
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null){
convertView = inflater.inflate(R.layout.example_row_item, null);
holder = new ViewHolder();
holder.txtSentence = (TextView) convertView.findViewById(R.id.txtSentence);
holder.txtInitialLetter = (TextView) convertView.findViewById(R.id.txtInitialLetter);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
final ExamplePojo pojo = items.get(position);
holder.txtSentence.setText(pojo.getSentence());
holder.txtInitialLetter.setText(pojo.getSentence().charAt(0));
return convertView;
}
public class ViewHolder{
TextView txtSentence, txtInitialLetter;
}
}
Here is code for showing first character in first textview and complete name in other textview.Also put below lines in if(convertView== null) block
final ExamplePojo pojo = items.get(position);
holder.txtSentence.setText(pojo.getSentence());
holder.txtInitialLetter.setText(pojo.getSentence().substring(0, 1));
I am having a listview in my App. Each row has an button. I'm hiding the button for some rows with setVisibility. But the buttons visibility changes after scrolling the list. How can i stop this change ?
I already saw a question with the checkbox with Listview. But i don't know how to implement it for the buttons. So please guide me!
ADAPTER
public class published_adapter extends BaseAdapter {
Context con;
ArrayList<HashMap<String, String>> class_list;
LayoutInflater inflater;
public class ViewHolder
{
TextView title,description,class_section,date;
ImageButton download;
Button viewasgn;
}
public published_adapter(Context co, ArrayList<HashMap<String, String>> list1) {
class_list = list1;
con = co;
inflater = (LayoutInflater) con.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return class_list.size();
}
#Override
public Object getItem(int arg0) {
return class_list.get(arg0).get("class_name");
}
#Override
public long getItemId(int arg0) {
return 0;
}
#Override
public View getView(final int arg0, View arg1, ViewGroup arg2) {
View row = arg1;
ViewHolder holder = new ViewHolder();
if(row == null)
{
row = inflater.inflate(
R.layout.assignment_adapter_layout, arg2, false);
// initialize the elements
holder.download = (ImageButton) row.findViewById(R.id.download);
holder.title = (TextView) row.findViewById(R.id.title);
holder.description = (TextView) row.findViewById(R.id.description);
holder.class_section = (TextView) row.findViewById(R.id.class_section);
holder.date = (TextView) row.findViewById(R.id.date);
holder.viewasgn = (Button) row.findViewById(R.id.attend);
row.setTag(holder);
}
else
{
holder = (ViewHolder)row.getTag();
}
String type = class_list.get(arg0).get("ASSIGNMENT_TYPE");
if (class_list.get(arg0).get("TOTAL_SUBMISSION").equals("0")) {
Log.e("TITLE", class_list.get(arg0).get("TOTAL_SUBMISSION"));
}
else{
Log.e("TITLE", class_list.get(arg0).get("TOTAL_SUBMISSION"));
holder.viewasgn.setVisibility(View.VISIBLE);
holder.viewasgn.setText("VIEW");
}
return row;
}
}
Update your code like this
if (class_list.get(arg0).get("TOTAL_SUBMISSION").equals("0")) {
Log.e("TITLE", class_list.get(arg0).get("TOTAL_SUBMISSION"));
holder.viewasgn.setVisibility(View.INVISIBLE);
holder.viewasgn.setText("");
}
else{
Log.e("TITLE", class_list.get(arg0).get("TOTAL_SUBMISSION"));
holder.viewasgn.setVisibility(View.VISIBLE);
holder.viewasgn.setText("VIEW");
}
Either add the setVisibility(View.INVISIBLE) like #Solhail suggested or hide those buttons by default in your assignment_adapter_layout.xml by adding android:visibility="invisible" in your Button's properties, and don't change a thing in your current adapter code.
In your case Views will be recreated when listView is scroll.
To prevent this, you should not use ViewHolder.
You need to user Adapter like this:
(Only if you have not a lot elements)
public class SampleAdapter extends BaseAdapter {
private ArrayList<String> arrayList;
private Activity activity;
public SampleAdapter(ArrayList<String> arrayList, Activity activity) {
this.arrayList = arrayList;
this.activity = activity;
}
#Override
public int getCount() {
return arrayList.size();
}
#Override
public Object getItem(int position) {
return arrayList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater layoutInflater = activity.getLayoutInflater();
View view = layoutInflater.inflate(R.layout.sample_element, parent, false);
TextView textViewTitle = (TextView) view.findViewById(R.id.text_view_title);
textViewTitle.setText(arrayList.get(position));
return view;
} }
Hello and thanks for your help!
Upon clicking the button, the alertdialog should open up with a gridview inside.
This is my code:
loginWith.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this);
builder.setTitle(getResources().getString(R.string.loginWith));
final AlertDialog dialog = builder.create();
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.dialog_loginwith, (ViewGroup) findViewById(R.id.layout_loginwith));
builder.setView(layout);
GridView gridView = (GridView) layout.findViewById(R.id.gvLoginwith);
gridView.setAdapter(new LoginWithAlertDialogImageAdapter(context));
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch (position) {
case 0:
loginWithFacebook();
break;
case 1:
loginWithGoogle();
break;
case 2:
loginWithLinkedIn();
break;
case 3:
loginWithTwitter();
break;
}
dialog.dismiss();
}
});
dialog.show();
}
});
But it ends up leaving the NullPointerException at gridView.setAdapter() part above and the context.getResources().getString() methods in the following class.
public class LoginWithAlertDialogImageAdapter extends BaseAdapter {
private Context context;
private LayoutInflater mLayoutInflater;
public LoginWithAlertDialogImageAdapter(Context context) {
this.context = context;
mLayoutInflater = LayoutInflater.from(context);
}
#Override
public int getCount() {
return 0;
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView == null) {
convertView = mLayoutInflater.inflate(R.layout.categorycontent_log_in_with, null);
convertView.setLayoutParams(new GridView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
holder = new ViewHolder();
holder.title = (TextView) convertView.findViewById(R.id.loginwith_title);
holder.icon = (ImageView) convertView.findViewById(R.id.loginwith_icon);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.icon.setAdjustViewBounds(true);
holder.icon.setScaleType(ImageView.ScaleType.FIT_XY);
holder.icon.setPadding(8, 8, 8, 8);
holder.title.setText(items[position]);
holder.icon.setImageResource(icons[position]);
return convertView;
}
class ViewHolder {
TextView title;
ImageView icon;
}
private Integer[] icons = {
R.drawable.ic_facebook,
R.drawable.ic_google,
R.drawable.ic_linkedin,
R.drawable.ic_twitter
};
private String[] items = {
context.getResources().getString(R.string.facebook),
context.getResources().getString(R.string.google),
context.getResources().getString(R.string.linkedin),
context.getResources().getString(R.string.twitter)
};
}
How should this be overcome?
Added I corrected the adapter code following the suggestion as follows.
public class LoginWithAlertDialogImageAdapter extends BaseAdapter {
private Context context;
private LayoutInflater mLayoutInflater;
private String items[];
public LoginWithAlertDialogImageAdapter(Context context) {
this.context = context;
mLayoutInflater = LayoutInflater.from(context);
items = new String[] {
context.getResources().getString(R.string.facebook),
context.getResources().getString(R.string.google),
context.getResources().getString(R.string.linkedin),
context.getResources().getString(R.string.twitter)
};
}
#Override
public int getCount() {
return 0;
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView == null) {
convertView = mLayoutInflater.inflate(R.layout.categorycontent_log_in_with, null);
convertView.setLayoutParams(new GridView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
holder = new ViewHolder();
holder.title = (TextView) convertView.findViewById(R.id.loginwith_title);
holder.icon = (ImageView) convertView.findViewById(R.id.loginwith_icon);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.icon.setAdjustViewBounds(true);
holder.icon.setScaleType(ImageView.ScaleType.FIT_XY);
holder.icon.setPadding(8, 8, 8, 8);
holder.title.setText(items[position]);
holder.icon.setImageResource(icons[position]);
return convertView;
}
class ViewHolder {
TextView title;
ImageView icon;
}
private Integer[] icons = {
R.drawable.ic_facebook,
R.drawable.ic_google,
R.drawable.ic_linkedin,
R.drawable.ic_twitter
};
}
Now the NullPointerException is not thrown anymore, but the thing is the icons and the icon titles and the GridView itself are not shown, but only the title of the AlertDialog is shown. Do you figure out the next issue?
Here:
context.getResources().getString(R.string.facebook)
context is null .
Why?
items Array is declared and initialized as a class level variable, during adding items in items Array, using context for accessing getResources method which is null because context is initialized inside constructor of LoginWithAlertDialogImageAdapter class.
Means context is accessed before executing constructor of LoginWithAlertDialogImageAdapter class.
So, to fix this issue declare items Array as class level variable but add item in it after initializing context :
private String[] items;
public LoginWithAlertDialogImageAdapter(Context context) {
this.context = context;
...
items = {
context.getResources().getString(R.string.facebook),
...
};
}