Android listview content won't refresh upon call back - java

I got some problem when trying to refresh the list view content on call back. Inside my onCreate():
private ListAdapter mAdapter;
private ListView listview;
mAdapter = new ListAdapter(getActivity());
listview.setAdapter(mAdapter);
Then, inside my callback which is working as I managed to print out the data:
public void callback(ArrayList<Transaction> list)
{
expenseslist.clear();
Log.d("SIZE", String.valueOf(list.size()));
for(int i = 0; i < list.size(); i++){
expenseslist.add(list.get(i));
}
mAdapter.notifyDataSetChanged();
listview.invalidateViews();
}
private class ListAdapter extends BaseAdapter {
LayoutInflater inflater;
ViewHolder viewHolder;
public ListAdapter(Context context) {
inflater = LayoutInflater.from(context);
}
public int getCount() {
return expenseslist.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.transactioncat_listview_row,
null);
viewHolder = new ViewHolder();
viewHolder.txt_dcatTitle = (TextView) convertView
.findViewById(R.id.txtCatTitle);
viewHolder.txt_dcatTotal = (TextView) convertView
.findViewById(R.id.txtCatTotal);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.txt_dcatTitle.setText(expenseslist.get(position)
.getCategory().getCategoryName().trim());
viewHolder.txt_dcatTotal.setText(Extra.formatCurrency(expenseslist.get(position)
.getTransactionAmt()));
return convertView;
}
}
I called the notifyDataSetChanged() and invalidateViews() but it is not refreshing. I am not adding new rows to the list view but rather, updating the content in each row.
How can I do this correctly?
public class ExpenseActivity extends Fragment implements TransactionListCallBack{
private ArrayList<Transaction> transactionlist = new ArrayList<Transaction>();
private ArrayList<Transaction> expenseslist = new ArrayList<Transaction>();
private ListAdapter mAdapter;
private ListView listview;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.activity_expense,container,false);
expenseslist.clear();
transactionlist = (ArrayList<Transaction>)getArguments().getSerializable("transactionlist");
for(int j = 0; j < transactionlist.size(); j++){
expenseslist.add(transactionlist.get(j));
}
listview = (ListView) v.findViewById(R.id.transactionCatListview);
// Binding the list into list view
mAdapter = new ListAdapter(getActivity());
listview.setAdapter(mAdapter);
return v;
}
#Override
public void callback(ArrayList<Transaction> list)
{
total = 0;
expenseslist.clear();
for(int i = 0; i < list.size(); i++){
expenseslist.add(list.get(i));
}
}
mAdapter.notifyDataSetChanged();
listview.invalidateViews();
}
private class ListAdapter extends BaseAdapter {
LayoutInflater inflater;
ViewHolder viewHolder;
public ListAdapter(Context context) {
inflater = LayoutInflater.from(context);
}
public int getCount() {
return expenseslist.size();
}
public Object getItem(int position) {
return expenseslist.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.transactioncat_listview_row,
null);
viewHolder = new ViewHolder();
viewHolder.txt_dcatTitle = (TextView) convertView
.findViewById(R.id.txtCatTitle);
viewHolder.txt_dcatTotal = (TextView) convertView
.findViewById(R.id.txtCatTotal);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.txt_dcatTitle.setText(expenseslist.get(position)
.getCategory().getCategoryName().trim());
viewHolder.txt_dcatTotal.setText(Extra.formatCurrency(expenseslist.get(position)
.getTransactionAmt()));
return convertView;
}
}
private class ViewHolder {
CircularProgressBar progressBar;
ImageView background_catview;
ImageView icon_catview;
TextView txt_dcatTitle;
TextView txt_dcatTotal;
}
// override menu item to add new transaction
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.clear();
inflater.inflate(R.menu.menu_main_addtransaction, menu);
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_openNewTransaction) {
new GetAllCategoriesAsyncTask(
new GetAllCategoriesAsyncTask.OnRoutineFinished() {
public void onFinish() {
AddTransactionActivity addFragment = new AddTransactionActivity();
FragmentTransaction tt = getFragmentManager().beginTransaction();
addFragment.setTransactionListCallBack(ExpenseActivity.this);
tt.replace(R.id.frame,addFragment);
tt.addToBackStack("tag");
tt.commit();
}
}).execute();
}
return super.onOptionsItemSelected(item);
}
}

Make this change
public Object getItem(int position) {
return expenseslist.get(position);
}

You need to keep a list of your data in adapter for notifyDataSetChanged to work:
Change
mAdapter = new ListAdapter(getActivity(), expenseslist);
And inside your adapter:
list<Transaction> expenseslist;
public ListAdapter(Context context, list<Transaction> expenseslist) {
inflater = LayoutInflater.from(context);
this.expenseslist = expenseslist;
}
EDIT:
Also change these methods:
public Transaction getItem(int position) {
return expenseslist.get(position);
}
public long getItemId(int position) {
return expenseslist.get(position).getId();
}

Hi Denise in your adapter create a new function called updateTransactions(). This is the code for function where expenseslist is the list your passing to adapter.
public void updateTransactions(List<Transaction> list) {
expenseslist.clear();
expenseslist.addAll(list);
notifyDataSetChanged();
Log.d("SIZE", String.valueOf(expensesList.size()));
}
Then in your callback function call updateTransactions() using instance of adapter like this
public void callback(ArrayList<Transaction> list)
{
mAdapter.updateTransactions(list);
}
Edit: Demo run code
package ********;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Collections;
/**
* Created by Rishabh Bhatia on 18/5/17.
*/
public class TestActivity extends Activity {
private ListView list;
private ListAdapter adapter;
private ArrayList<String> dataList;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
list = (ListView) findViewById(R.id.list);
dataList = new ArrayList<>();
for (int i = 0; i < 20; i++) {
String string = "abc";
dataList.add(string);
}
adapter = new ListAdapter(this, dataList);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.d("rishabh", "something is selected");
String transaction = dataList.get(0);
transaction = "xyz";
dataList.remove(0);
dataList.add(0, transaction);
adapter.updateExpenses(dataList);
}
});
}
private class ListAdapter extends BaseAdapter {
private ArrayList<String> expenseslist;
LayoutInflater inflater;
ViewHolder viewHolder;
public ListAdapter(Context context, ArrayList<String> list) {
inflater = LayoutInflater.from(context);
this.expenseslist = (ArrayList<String>) list.clone();
}
public int getCount() {
return expenseslist.size();
}
public Object getItem(int position) {
return expenseslist.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.row,
null);
viewHolder = new ViewHolder();
viewHolder.text = (TextView) convertView
.findViewById(R.id.tvRow);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.text.setText(expenseslist.get(position));
return convertView;
}
public void updateExpenses(ArrayList<String> list) {
Log.d("rishabh", "updating the expenses "+list.size());
expenseslist.clear();
for(int i =0; i<list.size(); i++) {
Log.d("rishabh", "inside for loop");
expenseslist.add(list.get(i));
Log.d("rishabh", "updating item to list "+list.get(i));
}
notifyDataSetChanged();
Log.d("rishabh", "updating the expenses "+expenseslist.size());
}
}
static class ViewHolder {
private TextView text;
}
}
Hopefully it helps.

Are you are coding within a fragment? Could i ask where/when you call this method
public void callback(ArrayList list);
As I guess you call this method from an activity on a lifecycle method right after you add the fragment. Or maybe you call this method not in a UI thread with a try - catch block.

Related

How To Show specific Data From SQLite Database In Android ListView

I have one table in SQLite database which has two fields one is "b" and another is "n" "b" represent book number and "n" represent book names I am successfully fetching all "n" field in my list view now I want when any user click particular list item show book b number which in my database, for example, I have book n=Genesis when anyone click on Genesis it show book number like b=1;
here my code an database structure
*/
public class Bookname extends Fragment {
private ListView listView;
private ArrayList<String> stringArrayList;
private ArrayAdapter<String> adapter;
private DatabaseHelper mDBHelper;
private SQLiteDatabase mDb;
boolean book;
public Bookname() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_bookname, container, false);
setData();
listView = view.findViewById(R.id.list);
adapter = new ListViewAdapter((MainActivity) getContext(), R.layout.item_listview, stringArrayList);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
});
return view;
}
private void setData() {
stringArrayList = new ArrayList<>();
mDBHelper = new DatabaseHelper(getContext());
mDb = mDBHelper.getReadableDatabase();
Cursor cursor = mDb.rawQuery("SELECT b, n FROM key_english;", new String[]{});
if (cursor.getCount() == 0) {
Toast.makeText(getContext(), "NO DATA FOUND", Toast.LENGTH_SHORT).show();
} else {
while (cursor.moveToNext()) {
stringArrayList.add(cursor.getString(1));
}
}
}
}
Myadopter class
package bible.swordof.God;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.amulyakhare.textdrawable.TextDrawable;
import com.amulyakhare.textdrawable.util.ColorGenerator;
import java.util.List;
public class ListViewAdapter extends ArrayAdapter<String> {
private MainActivity activity;
private List<String> friendList;
public ListViewAdapter(MainActivity context, int resource, List<String> objects) {
super(context, resource, objects);
this.activity = context;
this.friendList = objects;
}
#Override
public int getCount() {
return friendList.size();
}
#Override
public String getItem(int position) {
return friendList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
// If holder not exist then locate all view from UI file.
if (convertView == null) {
// inflate UI from XML file
convertView = inflater.inflate(R.layout.item_listview, parent, false);
// get all UI view
holder = new ViewHolder(convertView);
// set tag for holder
convertView.setTag(holder);
} else {
// if holder created, get tag from view
holder = (ViewHolder) convertView.getTag();
}
holder.friendName.setText(getItem(position));
//get first letter of each String item
String firstLetter = String.valueOf(getItem(position).charAt(0));
ColorGenerator generator = ColorGenerator.MATERIAL; // or use DEFAULT
// generate random color
int color = generator.getColor(getItem(position));
TextDrawable drawable = TextDrawable.builder()
.buildRound(firstLetter, color); // radius in px
holder.imageView.setImageDrawable(drawable);
return convertView;
}
private class ViewHolder {
private ImageView imageView;
private TextView friendName;
public ViewHolder(View v) {
imageView = (ImageView) v.findViewById(R.id.image_view);
friendName = (TextView) v.findViewById(R.id.text);
}
}
}
[My database structure][1]
[1]: https://i.stack.imgur.com/7aXut.png
Create model
Like:
public class BookData {
private String bookNo;
private String bookName;
BookData(String bookNo, String bookName){
this.bookNo = bookNo;
this.bookName = bookName;
}
public String getBookNo() {
return bookNo;
}
public void setBookNo(String bookNo) {
this.bookNo = bookNo;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
} }
Create ArrayList object like:
ArrayList<BookData> stringArrayList = new ArrayList<>();
Store your data in model
stringArrayList.add(new BookData(cursor.getString(0),cursor.getString(1)));
And retrieve data using:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String bookNo = stringArrayList.get(position).getBookNo();
Log.e("BookNo : ", bookNo);
}
});

How to change the background color of a list item at click on a button?

I have a ListView and a CustomAdapter. The elements are all successfully loaded into the list. Now I want to change the background color of a certain element of the list by clicking on an external button. But I do not know how to access a specific item in the list.
Here is the CustomAdapter class:
public class CustomAdapter extends BaseAdapter {
private Context ctx;
private int resource;
private List<ItemModel> items;
public PreorderListAdapter(Context context, int resource, List<ItemModel> items){
this.ctx = context;
this.resource = resource;
this.items = items;
}
#Override
public int getCount() {
return items.size();
}
#Override
public ItemModel getItem(int position) {
return items.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#NonNull
#Override
public View getView(int i, View convertView, #NonNull ViewGroup parent) {
View view = convertView;
if(view == null){
LayoutInflater inflater = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(resource, null);
}
TextView text1 = (TextView) view.findViewById(R.id.text1);
TextView text2 = (TextView) view.findViewById(R.id.text2);
TextView text3 = (TextView) view.findViewById(R.id.text3);
ItemModel item = items.get(i);
text1.setText(item.getName());
text2.setText(item.getOption2());
text3.setText(item.getOption3());
return view;
}
}
You can do it like this inside your getView() method
view.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
view.setBackgroundColor(ContextCompat.getColor(this, R.color.yourcolor));
}
});
If you have a button on your view then performs the listener on that button
If you want to get your selected item view from your parent activity then :
yourlistview.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent,View view, int position, long id)
{
selectedposition = position ;
}
});
View view = listView.getAdapter().getView(selectedposition,null,listview);
Then change its background:
view.setBackgroundColor(ContextCompat.getColor(this, R.color.yourcolor));
please define your color in your color.xml file
If you have more than one view then , create an ArrayList<View> and do some loop
create a custom listener interface in your activity and your
adapter will implement this.
public interface OnClickListenerFromActivity {
void onActivityButtonClick(int position);
}
on click in your button call your listener's method
mOnClickListenerFromActivity.onActivityButtonClick(mList.getItem(yourPostion));
implement this listener into your adapter
public class CustomAdapter extends BaseAdapter implements Activity.OnClickListenerFromActivity {
private Context ctx;
private int resource;
private List<ItemModel> items;
public PreorderListAdapter(Context context, int resource, List<ItemModel> items){
this.ctx = context;
this.resource = resource;
this.items = items;
}
#Override
public int getCount() {
return items.size();
}
#Override
public ItemModel getItem(int position) {
return items.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#NonNull
#Override
public View getView(int i, View convertView, #NonNull ViewGroup parent) {
View view = convertView;
if(view == null){
LayoutInflater inflater = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(resource, null);
}
TextView text1 = (TextView) view.findViewById(R.id.text1);
TextView text2 = (TextView) view.findViewById(R.id.text2);
TextView text3 = (TextView) view.findViewById(R.id.text3);
ItemModel item = items.get(i);
text1.setText(item.getName());
text2.setText(item.getOption2());
text3.setText(item.getOption3());
return view;
}
public void onActivityButtonClick(int position) {
// get your item through position and
// set your color here
}
}

add(object) extended from ArrayAdapter is not being overridden. Because of it I am unable to call that mtd

I am getting an error as "ERROR: Method cannot override method from superclass" for the add() method from the ArrayAdapter class. Removing the annotation is not helping.
package com.example.den_2.json_example;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class ContactAdapter extends ArrayAdapter {
List list = new ArrayList();
public ContactAdapter(Context context, int resource) {
super(context, resource);
}
#Override // ERROR: Method cannot override method from superclass
public void add(Contacts object) {
super.add(object);
list.add(object);
}
#Override
public int getCount() {
return list.size();
}
#Nullable
#Override
public Object getItem(int position) {
return list.get(position);
}
#NonNull
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row;
row = convertView;
ContactHolder contactHolder;
if(row==null)
{
LayoutInflater layoutInflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = layoutInflater.inflate(R.layout.row_layout,parent,false);
contactHolder = new ContactHolder();
contactHolder.tx_name = (TextView) row.findViewById(R.id.tx_name);
contactHolder.tx_email = (TextView) row.findViewById(R.id.tx_email);
contactHolder.tx_mobile = (TextView) row.findViewById(R.id.tx_mobile);
row.setTag(contactHolder);
}
else{
contactHolder = (ContactHolder) row.getTag();
}
Contacts contacts = (Contacts) this.getItem(position);
contactHolder.tx_name.setText(contacts.getName());
contactHolder.tx_email.setText(contacts.getEmail());
contactHolder.tx_mobile.setText(contacts.getMobile());
return row;
}
static class ContactHolder
{
TextView tx_name, tx_email, tx_mobile;
}
}
Use BaseAdapter instead of ArrayAdapter for your use:
public class ContactAdapter extends BaseAdapter {
private List<Contacts> list = new ArrayList<Contacts>();
private Context mContext;
public ContactAdapter(Context context, List<Contacts> list) {
this.mContext=context;
this.list=list;
}
/* #Override // ERROR: Method cannot override method from superclass
public void add(Contacts object) {
super.add(object);
list.add(object);
}*/
//Your custom add Function for this adapter
public void add(Contacts object){
list.add(object);
notifyDataSetChanged();
}
#Override
public int getCount() {
return list.size();
}
#Nullable
#Override
public Contacts getItem(int position) {
return list.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#NonNull
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row;
row = convertView;
ContactHolder contactHolder;
if(row==null)
{
LayoutInflater layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = layoutInflater.inflate(R.layout.row_layout,parent,false);
contactHolder = new ContactHolder();
contactHolder.tx_name = (TextView) row.findViewById(R.id.tx_name);
contactHolder.tx_email = (TextView) row.findViewById(R.id.tx_email);
contactHolder.tx_mobile = (TextView) row.findViewById(R.id.tx_mobile);
row.setTag(contactHolder);
}
else{
contactHolder = (ContactHolder) row.getTag();
}
Contacts contacts = (Contacts) this.getItem(position);
contactHolder.tx_name.setText(contacts.getName());
contactHolder.tx_email.setText(contacts.getEmail());
contactHolder.tx_mobile.setText(contacts.getMobile());
return row;
}
static class ContactHolder
{
TextView tx_name, tx_email, tx_mobile;
}
}
Do not override it. Just use composition and call add method of arrayadapter in your own add method.

Android Checkbox in Listview selecting automatically

in my android application I am displaying the contact list of phone in a list view and a check box in each row for selecting contacts. But when I am selecting a particular row about tenth row is also getting selected automatically. I am giving my code below, if any one knows please help..
public class ContactsAdapter extends BaseAdapter
{
private Context context;
private ArrayList<Contact> contacts;
public ContactsAdapter(Context context, ArrayList<Contact> contacts)
{
this.context = context;
this.contacts = contacts;
}
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
final ViewHolder mHolder;
if (convertView == null)
{
// gridView = new View(context);
// get layout from mobile.xml
//gridView = inflater.inflate(R.layout.contact, null);
convertView = inflater.inflate(R.layout.contact, null);
mHolder = new ViewHolder();
mHolder.textName =(TextView) convertView.findViewById(R.id.name);
mHolder.textMobile =(TextView) convertView.findViewById(R.id.mobile);
mHolder.textSelector =(CheckBox) convertView.findViewById(R.id.selected);
convertView.setTag(mHolder);
));
}
else
{
mHolder = (ViewHolder) convertView.getTag();
mHolder.textSelector.setOnCheckedChangeListener(null);
}
mHolder.textMobile.setText(contacts.get(position).getMobile());
mHolder.textName.setText(contacts.get(position).getName());
mHolder.textSelector.setFocusable(false);
return convertView;
}
private class ViewHolder {
private TextView textMobile,textName;
private CheckBox textSelector;
}
#Override
public int getCount() {
return contacts.size();
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
}
Well Thats because ViewHolder will recycle the Views everytime you Scroll
i suggest you to Use onCLick on ListItem instead checkbox
To overcome this Declare a SparseBooleanArray
SparseBooleanArray sba=new SparseBooleanArray();//this should be global
Then set the items checked state as soon as you render it
mHolder.textSelector =(CheckBox) convertView.findViewById(R.id.selected);
mHolder.textSelector.setChecked(sba.get(position));
Then write a onClickListener to you convertView and check it manually
convertView.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
mHolder.textSelector.setChecked(isChecked);
sba.put(position,isChecked); //storing the state
}
}
);
**Well Now the sba has list items checked and you can use that for further Actions**
# Jocheved... edit your code like this...
public class ContactsAdapter extends BaseAdapter
{
private Context context;
private ArrayList<Contact> contacts;
SparseBooleanArray sba=new SparseBooleanArray();
public ContactsAdapter(Context context, ArrayList<Contact> contacts)
{
this.context = context;
this.contacts = contacts;
}
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final ViewHolder mHolder;
if (convertView == null)
{
convertView = inflater.inflate(R.layout.contact, null);
mHolder = new ViewHolder();
mHolder.textName =(TextView) convertView.findViewById(R.id.name);
mHolder.textMobile =(TextView) convertView.findViewById(R.id.mobile);
mHolder.textSelector =(CheckBox) convertView.findViewById(R.id.selected);
convertView.setTag(mHolder);
}
else
{
mHolder = (ViewHolder) convertView.getTag();
}
mHolder.textMobile.setText(contacts.get(position).getMobile());
mHolder.textName.setText(contacts.get(position).getName());
mHolder.textName.setSelected(true);
mHolder.textSelector.setChecked(sba.get(position));
mHolder.textSelector.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v) {
if(mHolder.textSelector.isChecked())
{
sba.put(position, true);
}
else
{
sba.put(position, false);
}
}
});
return convertView;
}
private class ViewHolder
{
private TextView textMobile,textName;
private CheckBox textSelector;
}
#Override
public int getCount() {
return contacts.size();
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
}

Custom Adapter not working for the listview from Parse.com

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.

Categories

Resources