I have a ToDo project with listview that shows the data from SQLite with custom adapter. Also, I have a button to add the new task but, the items in list view will be repeated.
my custom adapter is
public class CustomAdapter extends BaseAdapter {
private final Activity activity;
private final ArrayList < LT_Model > data;
private static LayoutInflater inflater = null;
public CustomAdapter(Activity a, ArrayList < LT_Model > d) {
this.activity = a;
this.data = d;
inflater = (LayoutInflater) this.activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public static class ViewHolder {
public TextView task;
public ImageView imgD;
public ImageView imgE;
public ImageView imgS;
}#
Override
public int getCount() {
if (data.size() <= 0)
return 1;
return data.size();
}
#
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) {
ViewHolder holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.item_todo, null);
holder = new ViewHolder();
holder.task = (TextView) convertView.findViewById(R.id.task_title);
holder.imgD = (ImageView) convertView.findViewById(R.id.imgDelete);
holder.imgE = (ImageView) convertView.findViewById(R.id.imgEdit);
holder.imgS = (ImageView) convertView.findViewById(R.id.imgCheck);
convertView.setTag(holder);
} else
holder = (ViewHolder) convertView.getTag();
if (data.size() <= 0) {
holder.task.setText("No Data");
} else {
holder.task.setText(data.get(position).getTaskName());
holder.imgD.setImageResource(android.R.drawable.ic_delete);
holder.imgE.setImageResource(data.get(position).getImgComment());
holder.imgS.setImageResource(data.get(position).getImgStatus());
}
return convertView;
}
}
My LT_Model with getter and setter
public class LT_Model {
private String TaskName = "";
private int ImgComment;
private int ImgStatus;
public String getTaskName() {
return TaskName;
}
public void setTaskName(String taskName) {
TaskName = taskName;
}
public int getImgComment() {
return ImgComment;
}
public void setImgComment(int imgComment) {
ImgComment = imgComment;
}
public int getImgStatus() {
return ImgStatus;
}
public void setImgStatus(int imgStatus) {
ImgStatus = imgStatus;
}
}
My UpdateUI which return the data to Arraylist from SQLite in the activity.
My table has 3 columns and I passed them to ArrayList.
private void updateUI() {
ArrayList<LT_Model> taskList = new ArrayList<>();
LT_Model lt_model = new LT_Model();
SQLiteDatabase db = mdb.getReadableDatabase();
Cursor cursor = db.query(DB_Value.Constant.List_Table,
new String[]{DB_Value.Constant._ID, DB_Value.Constant.COL_Task, DB_Value.Constant.COL_Comment,
DB_Value.Constant.COL_Status}, null, null, null, null, null);
while (cursor.moveToNext()) {
//int idx = cursor.getColumnIndex(DB_Value.Constant.COL_Task);
lt_model.setTaskName(cursor.getString(1));
Toast.makeText(getApplicationContext(), lt_model.getTaskName(), Toast.LENGTH_SHORT).show();
if (cursor.getString(2) == null || cursor.getString(2).equals("")) {
lt_model.setImgComment(android.R.drawable.ic_menu_edit);
}else{
lt_model.setImgComment(android.R.drawable.ic_menu_agenda);
}
if (cursor.getInt(3)==1){
lt_model.setImgStatus(android.R.drawable.checkbox_on_background);
}else{
lt_model.setImgStatus(android.R.drawable.checkbox_off_background);
}
taskList.add(lt_model);
}
mTaskListView.setAdapter(new CustomAdapter(dailyNew, taskList));
cursor.close();
db.close();
}
and this is my problem.
When I add the second task the title of first task would change with that, and this is repeated for the others rows.
When you iterate through your cursor, you need to make a new Model for each row, so you're adding a distinct object each time.
while (cursor.moveToNext()) {
LT_Model lt_model = new LT_Model();
Related
So I wanted to change my GridView to a RecyclerView and with that I had to change my BaseAdapter to a RecyclerAdapter.
I already tried making changes, but I have no clue how to switch the code into the RecyclerAdapter.
Here is how my BaseAdapter looks like :
class AlbumAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<HashMap< String, String >> data;
public AlbumAdapter(Activity a, ArrayList < HashMap < String, String >> d) {
activity = a;
data = d;
}
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) {
AlbumViewHolder holder = null;
if (convertView == null) {
holder = new AlbumViewHolder();
convertView = LayoutInflater.from(activity).inflate(
R.layout.album_row, parent, false);
holder.galleryImage = (ImageView) convertView.findViewById(R.id.galleryImage);
holder.gallery_count = (TextView) convertView.findViewById(R.id.gallery_count);
holder.gallery_title = (TextView) convertView.findViewById(R.id.gallery_title);
convertView.setTag(holder);
} else {
holder = (AlbumViewHolder) convertView.getTag();
}
holder.galleryImage.setId(position);
holder.gallery_count.setId(position);
holder.gallery_title.setId(position);
HashMap < String, String > song = new HashMap < String, String > ();
song = data.get(position);
try {
holder.gallery_title.setText(song.get(Function.KEY_ALBUM));
holder.gallery_count.setText(song.get(Function.KEY_COUNT));
Glide.with(activity)
.load(new File(song.get(Function.KEY_PATH))) // Uri of the picture
.into(holder.galleryImage);
} catch (Exception e) {}
return convertView;
}
}
class AlbumViewHolder {
ImageView galleryImage;
TextView gallery_count, gallery_title;
}
And thanks in advance !
Simply use this, feel free to modify to suit your need, make sure to read the comments as they really will help you understand few things.
public class AlbumAdapter extends RecyclerView.Adapter<AlbumAdapter.ViewHolder> {
private static final String KEY_ALBUM = "KEY_ALBUM";
private static final String KEY_COUNT = "KEY_COUNT";
private static final String KEY_PATH = "KEY_PATH";
private itemClickInterface clickInterface;
//private List<String> data;
private ArrayList<HashMap<String, String >> data;
// public AlbumAdapter(itemClickInterface clickInterface, List<String> data) { // Forget about this if your data is not an array of Strings.
// public AlbumAdapter(itemClickInterface clickInterface, ArrayList<HashMap< String, String >> data) { // Forget about this if you're not ready to pass an onclick interface
public AlbumAdapter(ArrayList<HashMap< String, String >> data) {
this.data = data;
// this.clickInterface = clickInterface; //Simply ignore since you're not passing an interface of clicklister
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.album_row, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(final ViewHolder holder, int position) {
try {
final String data_for_albums = this.data.get(position).get(KEY_ALBUM);
final String data_for_counts = this.data.get(position).get(KEY_COUNT);
final String data_for_paths = this.data.get(position).get(KEY_PATH);
holder.gallery_title.setText(data_for_albums);
holder.gallery_count.setText(data_for_counts);
Glide.with(activity)
.load(new File(data_for_paths)) // Uri of the picture
.into(holder.galleryImage);
/*
* You can modify this as you want.
* */
// holder.galleryImage.setOnClickListener(new View.OnClickListener() {
// #Override
// public void onClick(View v) {
// clickInterface.click(data_for_paths); // This depends on you.
// }
// });
} catch (Exception e) {
e.printStackTrace();
}
}
#Override
public int getItemCount() {
return data.size();
}
/*--------------------------------------------------------------------------------------
| GET REFERENCES TO VIEWS HERE
*--------------------------------------------------------------------------------------*/
class ViewHolder extends RecyclerView.ViewHolder {
ImageView galleryImage;
TextView gallery_count, gallery_title;
ViewHolder(View itemView) {
super(itemView);
galleryImage = itemView.findViewById(R.id.galleryImage);
gallery_count = itemView.findViewById(R.id.gallery_count);
gallery_title = itemView.findViewById(R.id.gallery_title);
}
}
}
How to call from our activity
ArrayList<HashMap<String, String >> data = new ArrayList<>();
//I'm assuming you already feed your data, so you're not passing null like me here.
cardlistview = findViewById(R.id.cardlistview);
albumAdapter = new AlbumAdapter(your_arraysOfHashMap);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
cardlistview.setLayoutManager(mLayoutManager);
cardlistview.setAdapter(albumAdapter);
I'm newbie. For couple of days I'm trying to resolve this issue, but no luck. Any help ? Thanks in advance.
Below is the images,
I selected few items and then scrolled up
Once scrolled down selection disappears
Below the adapter class I implemented,
/**
* Created by abcd on 27/1/17.
*/
public class ListItemAdapterTaxCheckBox extends BaseAdapter {
//flag = 1 name/id
//flag =2 //ID/Name
public class HolderCheck
{
TextView tv;
CheckBox ck;
}
private static LayoutInflater inflater=null;
Tax[] result;
Context context;
Tax[] selections = null;
String [] IDs = null;
boolean bAllSel = false;
Resources res = null;
public ListItemAdapterTaxCheckBox(Activity mainActivity, Tax[] prgmNameList, Tax[] sel, Resources rs, String [] ids) {
result=prgmNameList;
context=mainActivity;
selections = sel;
if(selections==null)
{
selections = new Tax[1];
selections[0] = new Tax();
}
IDs = ids;
res = rs;
inflater = ( LayoutInflater )context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return result.length + 1;
}
#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) {
final HolderCheck holderCk = new HolderCheck();;
View rowView = convertView;
final int pos = position;
rowView = inflater.inflate(R.layout.custom_list_check_box, null);
holderCk.tv = (TextView) rowView.findViewById(R.id.code);
holderCk.ck = (CheckBox) rowView.findViewById(R.id.checkBox1);
if(position==0) {
holderCk.tv.setText(context.getString(R.string.all_text));
}
else {
holderCk.tv.setText("" + result[position-1].m_TaxID + " - " + result[position-1].m_TaxName);
}
holderCk.tv.setTextSize(16);
holderCk.tv.setTextColor(Color.BLACK);
rowView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (holderCk.ck.isChecked()) {
holderCk.tv.setBackgroundColor(Color.WHITE);
holderCk.ck.setChecked(false);
if(pos!=0)
{
if (IDs != null)
IDs[pos -1] = "0";
}
else
{
bAllSel = false;
}
} else {
holderCk.tv.setBackgroundColor(GetResourceColor.getColorWrapper(context, R.color.selHintColor));
holderCk.ck.setChecked(true);
if(pos!=0)
{
if (IDs != null)
IDs[pos -1] = "" + result[pos-1].m_TaxID;
}
else
{
bAllSel = true;
}
}
}
});
return rowView;
}
public String [] getIDs() {
if(bAllSel)
return null;
else {
ArrayList<String> arrayList = new ArrayList<String>();
for (int i = 0, j = 0; i < IDs.length; i++) {
if (IDs[i].trim() != "0") {
arrayList.add(j, IDs[i].trim());
j = j + 1;
if (arrayList.size() == MySQLHelper.MAXITEMSLISTDELETE)
break;
}
}
return arrayList.toArray(new String[arrayList.size()]);
}
}
}
Below the way I'm populating the ListView
//class member
Tax[] valuesTaxID = null;
//inside a method
//default
Tax[] tx = new Tax[1];
tx[0] = new Tax();
if (valuesTaxID != null) {
listV.setAdapter(null);
listV.setAdapter(new ListItemAdapterTaxCheckBox(ctx, valuesTaxID, tx, rs, Ids));
listV.setVisibility(View.VISIBLE);
ListView will create and destroy views when you scroll. Your code is changing a checkbox when a view is clicked, but you store that info nowhere else, you scroll, ListView destroys the view when you scroll, and there you lost the status of the view.
You want to store the "checked" state of the view somewhere. Could be an ArrayList with states, could be a tag attached to the ViewHolders, ... just store it somewhere.
I have created a simple dynamic spinner all I need to add spacing and add a break line between each item,also I need to know how to add more attributes like item text color,item text size......etc,by the way this spinner is created in a tool bar
and this is my Simple Code with adapter
Spinner navigationSpinner = new Spinner(getSupportActionBar().getThemedContext());
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, Prests_Name);
navigationSpinner.setAdapter(adapter);
I have same requirement so i have created generic adapter class with some extra parameters. so you can check this out and make changes as per your requirement.
SpinnerAdapter
public class SpinnerAdapter<T> extends ArrayAdapter<T> {
private Context context;
private ArrayList<T> values;
private Typeface fontLight;
private int color, layoutId, textViewId;
//private int fontSize;
private boolean IsSingleLine;
private boolean IsFirstPositionSelectable, setTextSizeLimit = true;
public SpinnerAdapter(Context context, int resource, int textViewResourceId,
ArrayList<T> objects) {
super(context, resource, textViewResourceId, objects);
this.context = context;
this.values = objects;
this.layoutId = resource;
this.textViewId = textViewResourceId;
}
public SpinnerAdapter(Context context, int resource, ArrayList<T> objects, int color,
boolean isSingleLine) {
super(context, resource, objects);
this.context = context;
this.values = objects;
this.color = color;
this.IsSingleLine = isSingleLine;
}
#Override public int getCount() {
return values.size();
}
#Override public T getItem(int position) {
return values.get(position);
}
#Override public long getItemId(int position) {
return position;
}
public boolean isFirstPositionSelectable() {
return IsFirstPositionSelectable;
}
public void setFirstPositionSelectable(boolean enable) {
IsFirstPositionSelectable = enable;
}
public void setSetTextSizeLimit(boolean setTextSizeLimit) {
this.setTextSizeLimit = setTextSizeLimit;
}
public Typeface getFontLight() {
return fontLight;
}
public void setFontLight(Typeface fontLight) {
this.fontLight = fontLight;
}
#Override public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(context);
if (layoutId == 0) {
convertView = inflater.inflate(android.R.layout.simple_spinner_item, parent, false);
} else {
convertView = inflater.inflate(layoutId, parent, false);
}
}
TextView label;
if (textViewId == 0) {
label = (TextView) convertView.findViewById(android.R.id.text1);
} else {
label = (TextView) convertView.findViewById(textViewId);
}
if (setTextSizeLimit) {
label.setFilters(new InputFilter[] { new InputFilter.LengthFilter(2) });
}
label.setSingleLine(IsSingleLine);
label.setPaddingRelative(25, 15, 25, 15);
if (fontLight != null)
label.setTypeface(getFontLight());
label.setText(values.toArray(new Object[values.size()])[position].toString());
return label;
}
#Override public View getDropDownView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(context);
if (layoutId == 0) {
convertView = inflater.inflate(android.R.layout.simple_spinner_item, parent, false);
} else {
convertView = inflater.inflate(layoutId, parent, false);
}
}
TextView label;
if (textViewId == 0) {
label = (TextView) convertView.findViewById(android.R.id.text1);
} else {
label = (TextView) convertView.findViewById(textViewId);
}
//label.setPadding(30, 10, 10, 30);
//label.setTextSize(context.getResources().getDimension(R.dimen.text_size_small));
if (fontLight != null)
label.setTypeface(fontLight);
label.setSingleLine(IsSingleLine);
label.setText(values.toArray(new Object[values.size()])[position].toString());
return label;
}
}
I have RecycleView List with an ExpandableLinearLayout ( i use a third party library). In my ExpandableLinearLayout I have a ListView, with an custom ArrayAdapter.
I set my Adapter in the RecycleViewAdapter and submit one ArrayList that contains 4 ArrayLists and the position from the onBindViewHolder() method , too fill the different sections in the RecycleView.
I pass the data in the correct section but i get only the first element of each ArrayList. I Log some stuff and the problem is the position from the getView method in my ArrayAdapter is always 0..
I search and played around a bit but i didnt found a solution.. I hope somebody can help..
Sorry for my bad grammar
This is my RecycleView :
public class EmergencyPassAdapter extends RecyclerView.Adapter<EmergencyPassAdapter.EmergencyPassViewHolder> {
private static final String LOG_TAG = EmergencyPassAdapter.class.getName();
private Context context;
private ArrayList<CellInformation> cellInformations;
private SparseBooleanArray expandState = new SparseBooleanArray();
EmergencyPassExpandAdapter emergencyPassExpandAdapter;
public EmergencyPassAdapter(Context context, ArrayList<CellInformation> cellInformations) {
this.context = context;
this.cellInformations = cellInformations;
for (int i = 0; i < cellInformations.size(); i++) {
expandState.append(i, false);
}
}
#Override
public EmergencyPassViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.cell_emergency_pass, parent, false);
return new EmergencyPassViewHolder(view);
}
#Override
public void onBindViewHolder(final EmergencyPassViewHolder holder, final int position) {
CellInformation cellInformation = cellInformations.get(position);
holder.imageViewIcon.setImageDrawable(cellInformation.getIcon());
holder.textViewTitle.setText(cellInformation.getTitel());
emergencyPassExpandAdapter = new EmergencyPassExpandAdapter(context, EmergencyPassExpandDetailFactory.getExpandCellInformation(context), position);
holder.listView.setAdapter(emergencyPassExpandAdapter);
if (cellInformation.getTitel().equals(context.getResources().getString(R.string.emergency_informations_health_insurance_emergency_contacts))) {
holder.expandableLinearLayout.setExpanded(expandState.get(position));
holder.expandableLinearLayout.setListener(new ExpandableLayoutListenerAdapter() {
#Override
public void onPreOpen() {
expandState.put(position, true);
holder.imageViewArrow.setRotation(180);
}
#Override
public void onPreClose() {
expandState.put(position, false);
holder.imageViewArrow.setRotation(360);
}
});
} else if (cellInformation.getTitel().equals(context.getResources().getString(R.string.emergency_informations_health_insurance_legal_guardian))) {
holder.expandableLinearLayout.setExpanded(expandState.get(position));
holder.expandableLinearLayout.setListener(new ExpandableLayoutListenerAdapter() {
#Override
public void onPreOpen() {
expandState.put(position, true);
holder.imageViewArrow.setRotation(180);
}
#Override
public void onPreClose() {
expandState.put(position, false);
holder.imageViewArrow.setRotation(360);
}
});
} else {
holder.expandableLinearLayout.setExpanded(expandState.get(position));
holder.expandableLinearLayout.setListener(new ExpandableLayoutListenerAdapter() {
#Override
public void onPreOpen() {
expandState.put(position, true);
holder.imageViewArrow.setRotation(180);
}
#Override
public void onPreClose() {
expandState.put(position, false);
holder.imageViewArrow.setRotation(360);
}
});
}
holder.relativeLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (!holder.expandableLinearLayout.isExpanded()) {
Log.i(LOG_TAG, "expand");
holder.expandableLinearLayout.expand();
} else {
Log.i(LOG_TAG, "collapse");
holder.expandableLinearLayout.collapse();
}
}
});
}
#Override
public int getItemCount() {
return cellInformations.size();
}
public static class EmergencyPassViewHolder extends RecyclerView.ViewHolder {
TextView textViewTitle, textViewExpandText;
ImageView imageViewIcon, imageViewArrow;
ExpandableLinearLayout expandableLinearLayout;
RelativeLayout relativeLayout;
ListView listView;
public EmergencyPassViewHolder(View itemView) {
super(itemView);
textViewTitle = (TextView) itemView.findViewById(R.id.cell_emergency_pass_title_tv);
textViewExpandText = (TextView) itemView.findViewById(R.id.cell_emergency_pass_expand_detail_tv);
imageViewIcon = (ImageView) itemView.findViewById(R.id.cell_emergency_pass_icon_iv);
imageViewArrow = (ImageView) itemView.findViewById(R.id.cell_emergency_pass_arrow_icon_iv);
expandableLinearLayout = (ExpandableLinearLayout) itemView.findViewById(R.id.cell_emergency_pass_arrow_expandable_list);
relativeLayout = (RelativeLayout) itemView.findViewById(R.id.cell_emergency_pass_root_rl);
listView = (ListView) itemView.findViewById(R.id.cell_emergency_pass_expand_lv);
}
}
}
My ArrayAdapter
public class EmergencyPassExpandAdapter extends ArrayAdapter<CellExpandInformation>{
private static final String LOG_TAG = EmergencyPassExpandAdapter.class.getName();
private ArrayList<CellExpandInformation> values;
private int checkPosition;
private Context context;
public EmergencyPassExpandAdapter(Context context, ArrayList<CellExpandInformation> values, int checkPosition) {
super(context, -1, values);
this.context = context;
this.values = values;
this.checkPosition = checkPosition;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
Log.i(LOG_TAG,"View");
Log.i(LOG_TAG,"Position: " + position);
EmergencyPassExpandViewHolder viewHolder;
CellExpandInformation cellExpandInformation = values.get(position);
if (convertView == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
convertView = inflater.inflate(R.layout.cell_emergency_pass_expand, parent, false);
viewHolder = new EmergencyPassExpandViewHolder();
viewHolder.textViewExpandTitle = (TextView) convertView.findViewById(R.id.cell_emergency_pass_expand_detail_tv);
convertView.setTag(viewHolder);
} else {
viewHolder = (EmergencyPassExpandViewHolder) convertView.getTag();
}
Log.i(LOG_TAG,"CheckPosition: " + checkPosition);
if (values != null) {
if (checkPosition == 0) {
viewHolder.textViewExpandTitle.setText(cellExpandInformation.getMedications().get(position).getMedicationName());
Log.i(LOG_TAG, "Medications: " + cellExpandInformation.getMedications().get(position).getMedicationName());
} else if (checkPosition == 1) {
viewHolder.textViewExpandTitle.setText(cellExpandInformation.getAllergies().get(position).getAllgergiesName());
} else if (checkPosition == 2) {
viewHolder.textViewExpandTitle.setText(cellExpandInformation.getDiseases().get(position).getDiseasesName());
} else if (checkPosition == 3) {
viewHolder.textViewExpandTitle.setText(cellExpandInformation.getLegalGuradian().get(position).getGuradianName());
} else if (checkPosition == 4) {
viewHolder.textViewExpandTitle.setText(cellExpandInformation.getEmergencyContact().get(position).getEmergencyContactName());
}
}
for(int i = 0; i < cellExpandInformation.getMedications().size(); i++){
Log.i(LOG_TAG,"Medis: " + cellExpandInformation.getMedications().get(i).getMedicationName());
}
return convertView;
}
static class EmergencyPassExpandViewHolder {
TextView textViewExpandTitle;
ImageView imageViewPhone, imageViewEmail, imageViewAdd;
}
}
I am trying to create an android application using a database from Parse.com. I am using a custom adapter to create a listview. I don't find any errors with the code and yet the listeview is not showing up. Nothing there in the logcat as well. Just the listview does not show up.
lv = (ListView)findViewById(R.id.listView);
mProgress = (ProgressBar)findViewById(R.id.check_progress);
mProgress.setVisibility(View.VISIBLE);
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Sellers");
query.orderByAscending("Name");
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> parseObjects, ParseException e) {
if (e == null) {
studentsList = new ArrayList<Sellers>();
for (ParseObject ob : parseObjects) {
s = new Sellers();
s.setName(ob.getString("Name").toString());
s.setAddress1(ob.getString("Address1").toString());
s.setAddress2(ob.getString("Address2").toString());
s.setShopName(ob.getString("ShopName").toString());
s.setEmail(ob.getString("Email").toString());
s.setPhone(ob.getString("Phone").toString());
s.setZipcode(ob.getString("Zipcode").toString());
studentsList.add(s);
}
adapter = new ListviewAdapter(CheckStatus.this, studentsList);
lv.setAdapter(adapter);
mProgress.setVisibility(View.GONE);
} else {
mProgress.setVisibility(View.GONE);
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
});
This is the activity where I am invoking the listview.
public class ListviewAdapter extends BaseAdapter{
private final static String TAG = ListviewAdapter.class.getSimpleName();
private Context activity;
private LayoutInflater inflater = null;
private List<Sellers> sellers;
int layout;
public ListviewAdapter(Context activity, List<Sellers> sellers) {
this.activity = activity;
this.sellers = sellers;
inflater = LayoutInflater.from(activity);
}
#Override
public int getCount() {
return 0;
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
public class ViewHolder {
TextView name ;
TextView shop ;
TextView address1 ;
TextView address2;
TextView phone;
TextView email;
RelativeLayout rl;
}
#Override
public View getView(int position, View view, ViewGroup parent) {
View v =view;
ViewHolder holder = new ViewHolder();
if (view == null) {
LayoutInflater li = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = li.inflate(R.layout.list_item_layout,null);
holder.name = (TextView)v.findViewById(R.id.seller_name);
holder.shop = (TextView)v.findViewById(R.id.shop_name);
holder.address1 = (TextView)v.findViewById(R.id.address1);
holder.address2 = (TextView)v.findViewById(R.id.address2);
holder.phone = (TextView)v.findViewById(R.id.phone);
holder.email = (TextView)v.findViewById(R.id.emailID);
v.setTag(holder);
} else {
holder = (ViewHolder) v.getTag();
}
Sellers s = sellers.get(position);
// String a = s.Name;
// Log.d(TAG, a);
holder.name.setText(s.getName());
holder.shop.setText(s.getShopName());
holder.address1.setText(s.getAddress1());
holder.address2.setText(s.getAddress2());
holder.phone.setText(s.getPhone());
holder.email.setText(s.getEmail());
Log.d("CustomAdapter.class", "CustomAdapter");
// imageView.setImageDrawable(s.getPic());
return v;
}
}
And this is the custom adapter. There are no null pointer exceptions showing up in the logcat. I can't determine why the listview is not getting populated.
Try this;
#Override
public int getCount() {
return sellers.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position:
}
You have to implement your code on getCount() by return number of item listview will be created.