I'm obviously missing something in making this custom adapter. Basically, I've got code that grabs JSON from the site, then breaks it down into a ArrayList. I can do a .toString() and spit it all out onto a listview just fine, but everything I find on the net for creating a custom adapter creates the list inside of it. Is there a way to just supply your pre-created list? I've yet to make any headway on getting the custom one to work...but, as I said I can get the string to work just fine, so I'll include that code.
Here's my classes:
public class JSONEvents {
String eid;
String bid;
private String bname;
private String valid;
#Override
public String toString() {
return "Events [eid=" + eid + ", bid=" + bid + ", bname=" + bname
+ ", start=" + start + ", end=" + end + ", points=" + points
+ ", title=" + title + ", description=" + description
+ ", cat=" + cat + ", type=" + type + ", subtype=" + subtype
+ ", valid=" + valid + "]";
}
public String getEventInfo(String field){
if("eid".equals(field)){return eid;}
else if("bid".equals(field)){return bid;}
else if("bname".equals(field)){return bname;}
else if("valid".equals(field)){return valid;}
return "none";
}
}
public class JSONAdventures {
private String aid;
private String bid;
private String start;
private String valid;
#Override
public String toString() {
return "Adventures [aid=" + aid + ", bid=" + bid + ", start=" + start
+ ", end=" + end + ", points=" + points + ", title=" + title
+ ", description=" + description + ", cat=" + cat + ", type="
+ type + ", subtype=" + subtype + ", steps_comp=" + steps_comp
+ ", total_steps=" + total_steps + ", valid=" + valid + "]";
}
public String getEventInfo(String field){
if("aid".equals(field)){return aid;}
else if("bid".equals(field)){return bid;}
else if("start".equals(field)){return start;}
else if("valid".equals(field)){return valid;}
return "none";
}
}
public class JSONEandA {
private ArrayList<JSONEvents> events;
private ArrayList<JSONAdventures> adventures;
#Override
public String toString() {
return "ResponseHolder [events=" + events + ", adventures="
+ adventures + "]";
}
public ArrayList<JSONEvents> getJSONEvents() {
return events;
}
public ArrayList<JSONAdventures> getJSONAdventures() {
return adventures;
}
}
For the code in m activity:
List<JSONEvents> eventlist = new ArrayList<JSONEvents>();
try {
Gson googleJson = new Gson();
JSONEandA rh = googleJson.fromJson(example,
JSONEandA.class);
for (JSONEvents e : rh.getJSONEvents()) {
eventlist.add(e);
//eventlist.addAll(e);
// System.out.println(e.toString());
System.out.println(e.getEventInfo("eid"));
System.out.println(e.toString());
}
final ListView listview = (ListView) findViewById(R.id.eventlistview);
String[] values = new String[eventlist.size()];
eventlist.toArray(values);
// String[] values = new String[] {e.toString()};
final ArrayList<String> list = new ArrayList<String>();
for (int i = 0; i < values.length; ++i) {
list.add(values[i]);
}
final StableArrayAdapter adapter = new StableArrayAdapter(
Events.this,
android.R.layout.simple_list_item_1, list);
listview.setAdapter(adapter);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent,
final View view, int position, long id) {
final String item = (String) parent
.getItemAtPosition(position);
list.remove(item);
adapter.notifyDataSetChanged();
Toast.makeText(getApplicationContext(),
"Item Removed", Toast.LENGTH_LONG).show();
});
I've also pieced together an attempt at an adapter from various tutorials and such, but can't figure out how to tie it all together:
class EventAdapter extends ArrayAdapter<JSONEvents> {
private ArrayList<JSONEvents> items;
public EventAdapter(Context context, int textViewResourceId,
ArrayList<JSONEvents> items) {
super(context, textViewResourceId, items);
this.items = items;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.list_events, null);
}
JSONEvents q = items.get(position);
if (q != null) {
TextView nameText = (TextView) v.findViewById(R.id.firstline);
TextView priceText = (TextView) v.findViewById(R.id.secondLine);
if (nameText != null) {
nameText.setText(q.getEventInfo("eid"));
}
if (priceText != null) {
priceText.setText(q.getEventInfo("bid"));
}
}
return v;
}
}
I would appreciate any help, thanks!
Did you put tha data into your adapter?
adapter.addAll(String[] data);
Or you could do it while creating adapter:
EventAdapter adapter = new EventAdapter(this, R.id.some_id, eventlist);
Related
At first my sendData method is working but when I call it from another activity it has 'not attached to a context.' error.
When I opened it looks work, but after i delete some data it wasn't work.
Here is my code.
DeleteDialog.java
public class DeleteDialog extends android.support.v4.app.DialogFragment {
private Context mContext;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_delete_dialog, container, false);
mContext=this.getContext();
...
ok_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(editText.getText().toString().equals("")){
Toast.makeText(getContext(), "과목명을 입력해 주세요", Toast.LENGTH_SHORT).show();
}else{
dbHelper = new DBHelper(mContext);
db = dbHelper.getWritableDatabase();
dbHelper.deleteColum(editText.getText().toString());
timeTableFragment.sendData(mContext);
getDialog().dismiss();
}
}
});
TimeTableFragment.java
public class TimeTableFragment extends Fragment {
private Context mContext;
public void sendData(Context context) { //get data and draw to layout
dbHelper = new DBHelper(context);
db = dbHelper.getWritableDatabase();
dbHelper.onCreate(db);
String date = "", start = "", end = "", title = "", color = "";
ArrayList<SubjectItem> timetable_data;
timetable_data = dbHelper.getDataTimetable(); //get data from sql
SimpleDateFormat sf = new SimpleDateFormat("HHmm");
int resID = 0;
int mok;
Date a, b;
long diff, min;
View view = getView();
for (int i = 0; i < timetable_data.size(); i++) { // draw to layout
date = timetable_data.get(i).getSub_date();
start = timetable_data.get(i).getSub_start();
end = timetable_data.get(i).getSub_end();
title = timetable_data.get(i).getSub_title();
color = timetable_data.get(i).getSub_color();
if (date.substring(0, 2).equals("Th")) {
date = "h";
Log.d("ㄹㄹ","목요일 : " +date);
}
date = date.toLowerCase().substring(0, 1);
Log.d("ㄹㄹ","바깥의 데이터 : " +date);
start = start.replace(":", "");
end = end.replace(":", "");
try {
a = sf.parse(start);
b = sf.parse(end);
diff = b.getTime() - a.getTime();
min = (diff / (1000 * 60));
mok = (int) min / 30;
Log.d(TAG,"title " +title + " start : " + start +","+ a.getTime()+ " / end : " + end+","+b.getTime() + " date : " + date);
for (int k = 0; k < mok; k++) {
if (mok != 1) {
if (end.substring(2, 4).equals("30")) {
end = Integer.toString(Integer.parseInt(end) - 30);
if (end.length() == 3) end = "0" + end;
Log.d(TAG, " 그리기 title : " +title );
//Log.d(TAG , "콘텍스트 : " + getContext().getPackageName());
Error -> resID = getResources().getIdentifier(date + end, "id", getContext().getPackageName());
TextView textView = (TextView) view.findViewById(resID);
textView.setText(title);
paintTimetable(textView,color);
} else {
end = Integer.toString(Integer.parseInt(end) - 70);
if (end.length() == 3) end = "0" + end;
Log.d("Contxet " , "콘텍스트 : " + getContext().getPackageName());
resID = getResources().getIdentifier(date + end, "id", getContext().getPackageName());
TextView textView = (TextView) view.findViewById(resID);
textView.setText(title);
paintTimetable(textView,color);
Log.d(TAG, " 그리기 title : " +title);
}
} else {
Log.d("Contxet " , "콘텍스트 : " + getContext().getPackageName());
resID = getResources().getIdentifier(date + start, "id", getContext().getPackageName());
TextView textView = (TextView) view.findViewById(resID);
textView.setText(title);
paintTimetable(textView,color);
}
}
} catch (ParseException e) {
e.printStackTrace();
}
}
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
try {
mContext=getContext();
} catch (ClassCastException e) {
Log.d(TAG, "Exception" + e.getMessage());
}
}
DBHelper.java
public class DBHelper extends SQLiteOpenHelper {
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
...
public void deleteColum(String name)
{
String DELETE = "DELETE FROM " + TABLE_TIMETABLE + " WHERE " + KEY_TITLE + "='" + name + "'";
SQLiteDatabase db = this.getWritableDatabase();
db.execSQL(DELETE);
Log.d("dd", "sql deleted : " + name);
}
And Error message :
Fragment TimeTableFragment{f4aa844} not attached to a context.
In DB Insert, Delete are perfectly works but after call sendData method in DeleteDialog occurred 'not attached to a context'.
What context should I send and receive?
All database tasks should be done in async (another thread). It might take a while and after job is done you should come back to main thread and check if the view still exist, this way you will avoid memory leaks and error that you mentioned.
Use getApplicationContext() for initializing your DBHelper. This will ensure proper context of your app and does create the error you are receiving.
Addtional Info:
Make your DBHelper as Singleton it will optimize your app resources.
I created an adapter for my ListView that's displaying a data from my SQLite database, the problem is that in the ListView, I have 5 times the same line:
My adapter's code:
public class MyAdapter extends ArrayAdapter<Historique> {
private LayoutInflater mInflat;
private ArrayList<Historique> hist = new ArrayList<Historique>();
private int mVRessId;
public MyAdapter (Context context, int ressId, ArrayList<Historique> hists){
super(context,ressId,hists);
this.hist =hists;
mInflat = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mVRessId =ressId;
}
public View getView(int position, View convertedView, ViewGroup parents){
convertedView = mInflat.inflate(mVRessId,null);
Historique histor = hist.get(position);
if (histor != null){
TextView name = (TextView) convertedView.findViewById(R.id.hnom);
TextView quest = (TextView) convertedView.findViewById(R.id.hques);
TextView rep = (TextView) convertedView.findViewById(R.id.hrep);
TextView date = (TextView) convertedView.findViewById(R.id.hdate);
if (name != null){
name.setText(""+histor.getNom()+ ": "+histor.getLigne());
}
if (quest != null){
quest.setText(histor.getQuest());
}
if (rep != null){
rep.setText(histor.getRep());
}
if (date != null){
date.setText(histor.getDate().toString());
}
}
return convertedView;
}
}
The ListView must display the list, and here's the code:
final MyAdapter adapter = new MyAdapter (this, R.layout.adapter_view_layout,histList);
mCsr = openhelper.getTableHistoAsCursor();
int rows = mCsr.getCount();
if (rows == 0 ){
Toast.makeText(Historian.this, "Pas d'historique disponible", Toast.LENGTH_SHORT).show();
} else {
while (mCsr.moveToNext()){
histo = new Historique(mCsr.getString(0).toString(),mCsr.getString(1).toString(), mCsr.getString(2).toString(),mCsr.getString(3).toString(),mCsr.getString(4).toString());
histList.add(histo);
}
}
mListView .setAdapter(adapter);
The getTableHisto is a function that returns a cursor from my SQLite Modelhelper:
public Cursor getTableHistoAsCursor() {
SQLiteDatabase db = this.getReadableDatabase();
Cursor data = db.rawQuery(" SELECT Q." + KEY_QUESTION + " ,L." + KEY_LIGNE + " , U." + KEY_NOM + " , A." + KEY_DATE + " , A. " + KEY_REPONSE + " from "+ TABLE_LIGNE + " L, " + TABLE_QUESTION + " Q, " + TABLE_USER + " U, " + TABLE_ANSWER + " A WHERE Q." + KEY_ID_QUESTION + " = A." + KEY_ID_QUESTION + " AND A." + KEY_MATRICULE + " = U." + KEY_MATRICULE, null);
return data;
}
In the first time, I thought that in the insert I was doing something wrong and it goes 5 times in the database, so I've created an activity to try to get the number of inserts with a Select count();
And I saw that the result that I'm getting is the true result, I don't insert 5 times the same line, but the adapter is displaying it 5 times in 5 lines.
If you have any idea that can help me, I would be thankful.
thanks.
//Initialize my adapter after populating the list
histList.add(histo);
Adapter is only initialized after populating values in the Array List
not with null values.
final MyAdapter adapter = new MyAdapter (this, R.layout.adapter_view_layout,histList);
mListView .setAdapter(adapter);
I got it, the problem is solved, in case someone has the same problem, the loop wasn't good so I changed
else {
while (mCsr.moveToNext()){
by
else {
if (mCsr != null){
mCsr.moveToFirst();
There is my class with custom adapater
It's working with date and title.
Date and title are with table Page and textTag with Table Tag
public class MyAdapter extends ArrayAdapter <PageTag> {
Context context;
int ressource;
ArrayList<Page> data ;
TextView tvTitreList;
TextView tvDateList;
TextView tvTagList;
Page page;
public MyAdapter(Context context, ArrayList <PageTag> data ) {
super(context, 0, data);
}
My getView method.
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.activity_list_perso, parent, false);
}
tvTitreList = (TextView) convertView.findViewById(R.id.tvTitre);
tvDateList = (TextView) convertView.findViewById(R.id.tvDate);
tvTagList = (TextView) convertView.findViewById(R.id.tvTag);
tvTitreList.setText(getItem(position).getPage().titrePage);
tvDateList.setText(getItem(position).getPage().datePage.toString());
tvTagList.setText(getItem(position).getTag().texteTag);
return convertView;
}
}
My table PageTag
public class PageTag {
Page page ;
Tag tag ;
public PageTag ( Page page, Tag tag){
this.page = page;
this.tag = tag;
}
// with getter and setter
my ListPageActivity
pageMgr = new PageMgr(this);
lvList = (ListView) findViewById(R.id.listPage);
ArrayList<Page> listPage = pageMgr.getAllPageArray();
ArrayList<Tag> listTag = pageMgr.getAllTagArray();
ArrayList<PageTag> pageTag = new ArrayList<>();
for(int i = 0 ;i < listPage.size() ;i++){
Page page = listPage.get(i);
Tag tag = listTag.get(i);
(cannot resolve method) ==> pageTag.setPage(page);
==> pageTag.setTag(tag);
}
MyAdapter myAdapt = new MyAdapter(this,pageTag);
lvList.setAdapter(myAdapt);
getAllpageArray method
public ArrayList<Page> getAllPageArray(){
ArrayList <Page> listPageArray = new ArrayList <Page>();
String rqPage = " SELECT * FROM " + BaseSQLITE.TABLE_PAGE
+ " JOIN " + BaseSQLITE.TABLE_LIENS_TAG_PAGE
+ " ON " + BaseSQLITE.TABLE_PAGE+ "." + BaseSQLITE.COL_ID_PAGE + " = " + BaseSQLITE.TABLE_LIENS_TAG_PAGE+ "." + BaseSQLITE.COL_ID_PAGE_LTP
+ " JOIN " + BaseSQLITE.TABLE_TAG
+ " ON "+ BaseSQLITE.TABLE_TAG+ "." + BaseSQLITE.COL_ID_TAG + " = " + BaseSQLITE.TABLE_LIENS_TAG_PAGE+ "." + BaseSQLITE.COL_ID_TAG_LTP ;
Cursor cPage;
this.open();
cPage = bdd.rawQuery(rqPage,null);
Date date;
long l;
String titre;
while (cPage.moveToNext()){
String titrePage ;
Date datePage ;
l = cPage.getLong(2);
titrePage = cPage.getString(1);
datePage = new Date(l);
listPageArray.add(new Page(titrePage,datePage));
}
this.close();
return listPageArray;
}
same thing for getAllTagArray
Create a new Model class
class Result{
Page page;
Tag tag;
//getters and setters
}
Now loop your result and add it to List of Result
ArrayList<Result> result = new ArrayList<>();
for(int i=0 ;i < listPage.size() ;i++){
Page page = listPage.get(i);
Tag tag = tagList.get(i);
result.setPage(page);
result.setTag(tag);
}
In your adapter change Page to Result
tvTitreList.setText(getItem(position).getPage().getTitrePage());
tvDateList.setText(getItem(position).getPage().getDatePage().toString());
tvTagList.setText(getItem(position).getTag().getTextTag());
Finally it's ok with your method I have changed some code.
thanks to you #rajan ks
pageMgr = new PageMgr(this);
lvList = (ListView) findViewById(R.id.listPage);
ArrayList<Page> listPage = pageMgr.getAllPageArray();
ArrayList<Tag> listTag = pageMgr.getAllTagArray();
ArrayList<PageTag> pageTag = new ArrayList<>();
for(int i = 0 ;i < listPage.size() ;i++) {
Page page = listPage.get(i);
Tag tag = listTag.get(i);
PageTag pageT = new PageTag(page, tag);
pageTag.add(pageT);
}
MyAdapter myAdapt = new MyAdapter(this,pageTag);
lvList.setAdapter(myAdapt);
I have created a custom list view with 6 colomns. Each row is populated.
the first 3 fields are textviews,which are filled from db. Then a spinner and the last two edittexts.
i want to enter values to edittexts and spinner.. this much is ok.
The problem is when i enter values in edittext and after that if i scroll the list, the values entered in edittext changes in position. if i entered in first row, after scrolling it will be in last row or any other.. how i can solve this.. Need help pls..
My adapter class is given below.
public class CustomTransAdapter extends BaseAdapter {
public ArrayList<retrieveTrans> ret_arrArrayList;
private List<retrieveTrans> ret_translist;
private LayoutInflater inflater;
ArrayAdapter<String> adapterspin;
String time, currntdate, status;
String Maxcramnt, balamount;
double Mamount, Bamount, actualamnt;
String idpref, str_agent_id2, accno;
int trcheck;
int d = 0;
String tramnt = "", remarks = "";
Context context;
int count;
public CustomTransAdapter(NewTransactionSheet newTransactionSheet,
int transListPop, List<retrieveTrans> translist) {
// TODO Auto-generated constructor stub
super();
this.ret_translist = translist;
inflater = LayoutInflater.from(newTransactionSheet);
this.ret_arrArrayList = new ArrayList<retrieveTrans>();
this.ret_arrArrayList.addAll(translist);
this.context = newTransactionSheet;
String[] items = new String[] { "Type", "credit", "debit" };
final ArrayAdapter<String> adapterspin = new ArrayAdapter<String>(
newTransactionSheet, android.R.layout.simple_spinner_item,
items);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return ret_translist.size();
}
#Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return ret_translist.get(arg0);
}
#Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
class ViewHolder {
TextView acc_txt, name_txt, balAmnt_txt;
Spinner trans_spinner;
EditText transAmnt_edittxt, remarks_edittxt;
Button save;
}
#Override
public View getView(final int pos, View cv, ViewGroup arg2) {
// TODO Auto-generated method stub
try {
final ViewHolder holder;
View row = cv;
int p = pos;
Log.e("position of getview:", "" + p);
if (cv == null) {
cv = inflater.inflate(R.layout.trans_pop_list, null);
holder = new ViewHolder();
holder.acc_txt = (TextView) cv
.findViewById(R.id.txtTransAccNo_pop);
holder.name_txt = (TextView) cv
.findViewById(R.id.txtTransName_pop);
holder.balAmnt_txt = (TextView) cv
.findViewById(R.id.txtTransBalAmnt_pop);
holder.trans_spinner = (Spinner) cv
.findViewById(R.id.spinTrans_Type);
holder.transAmnt_edittxt = (EditText) cv
.findViewById(R.id.editTransAmnt_pop);
holder.save = (Button) cv.findViewById(R.id.save);
holder.remarks_edittxt = (EditText) cv
.findViewById(R.id.editRemarks_pop);
cv.setTag(holder);
} else {
holder = (ViewHolder) cv.getTag();
}
holder.acc_txt.setText("" + ret_translist.get(pos).getRetAccNo());
String hari = holder.acc_txt.getText().toString();
holder.name_txt.setText("" + ret_translist.get(pos).getRetName());
holder.balAmnt_txt.setText(""
+ ret_translist.get(pos).getRetBalAmnt());
String[] items = new String[] { "Type", "credit", "debit" };
final ArrayAdapter<String> adapterspin = new ArrayAdapter<String>(
context, android.R.layout.simple_spinner_item, items);
final String haris = holder.name_txt.getText().toString();
holder.trans_spinner.setAdapter(adapterspin);
/* ******************* Save button Click **************** */
holder.save.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
int position = pos;
final String accno = holder.acc_txt.getText().toString();
final String tramnt = holder.transAmnt_edittxt.getText()
.toString();
String remarks = holder.remarks_edittxt.getText()
.toString();
final String spinner = holder.trans_spinner
.getSelectedItem().toString();
Log.e("accno:", "" + accno);
Log.e("name :", "" + tramnt);
Log.e("position:", "" + position);
Log.e("remark:", "" + remarks);
Log.e("spinner:", "" + spinner);
/****************** time and date **********************/
// ----------Time----------
Calendar c = Calendar.getInstance();
time = (c.get(Calendar.HOUR) + ":" + c.get(Calendar.MINUTE)
+ ":" + c.get(Calendar.SECOND));
Log.e("Current Time", " " + time);
// -----------Date---------
Date now = new Date();
Date alsoNow = Calendar.getInstance().getTime();
currntdate = new SimpleDateFormat("M-d-yyyy").format(now);
Log.e("Date ", " " + currntdate);
status = new String("y");
if (tramnt.equals("")) {
Toast.makeText(arg0.getContext(),
"enter Transaction amount", 5000).show();
Log.e("if cndition", " " + tramnt);
} else if (spinner.equals("type")) {
Toast.makeText(arg0.getContext(),
"enter Transaction type", 5000).show();
Log.e("if cndition", " " + tramnt);
}
else {
if (remarks.equals("")) {
remarks = "null";
}
/************ declaration for dbcall *********************/
AccountDBAdapter db = new AccountDBAdapter(context);
NewTransactionSheet ts = new NewTransactionSheet();
try {
db.open();
String accid = db.getAccID(accno);
String Maxcramnt = db.getMaxCrAmnt(accno);
db.close();
Mamount = Double.parseDouble(Maxcramnt);
Bamount = Double.parseDouble(tramnt);
actualamnt = Mamount - Bamount;
balamount = "" + actualamnt;
db.open();
db.close();
/* Log.e("c value", " " +actualamnt); */
Log.e("tramnt", " " + tramnt);
Log.e("if cndition out", " " + tramnt);
db.open();
db.update_MaxCrAmnt(balamount, accid);
db.close();
actualamnt = 0;
db.open();
String userId = "" + 2;
db.insertTransactionTable(accid.toString(),
currntdate.toString(), spinner.toString(),
tramnt.toString(), userId.toString(),
time.toString(), remarks.toString(),
status.toString());
db.close();
holder.save.setText("SAVED");
holder.save.setBackgroundColor(Color.DKGRAY);
// }
} catch (Exception e) {
Log.e("error", e.getMessage());
}
}
}
});
} catch (Exception c) {
Log.e("adapter error", "" + c.getMessage());
}
return cv;
}
}
Working on this for many days... need help.. Thanks in advance.
I built a listview and implemented an infinite scroll, the listview is limited to show 5 items per load until it reaches the end, but it is duplicating the initial 5 items, I'll add some image so you can understand better:
How can I fix it?
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
rootView = inflater.inflate(R.layout._fragment_clientes, container, false);
rootView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT ));
try {
lv = (ListView) rootView.findViewById(R.id.listaClientes);
clientes = new ArrayList<ClienteModel>();
final ClientViewAdapter ad = new ClientViewAdapter(getActivity(), this, clientes);
lv.setVerticalFadingEdgeEnabled(true);
lv.setVerticalScrollBarEnabled(true);
lv.setOnScrollListener(new EndlessScrollListener(){
#Override
public void onLoadMore(int page, int totalItemsCount) {
new LoadMoreClientTask(progressBar,FragmentClientes.this,ad,getActivity()).execute(page);
}
});
lv.addFooterView(footerLinearLayout);
lv.setAdapter(ad);
new LoadMoreClientTask(progressBar,this,ad,getActivity()).execute(1);
} catch (Exception e) {
e.printStackTrace();
}
return rootView;
}
The Adapter:
public class ClientViewAdapter extends BaseAdapter {
private Activity activity;
private FragmentClientes frag;
private List<ClienteModel> cliente;
private static LayoutInflater inflater=null;
public ClientViewAdapter(Context context, FragmentClientes fragmentClientes, List<ClienteModel> clientes) {
this.inflater = LayoutInflater.from( context );
this.cliente = clientes;
frag = fragmentClientes;
}
public int getCount() {
return cliente.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
ViewHolder holder;
if(convertView == null){
vi = inflater.inflate(R.layout.fragment_cliente_item, null);
holder=new ViewHolder();
holder.id = (TextView)vi.findViewById(R.id.clienteId);
holder.nome = (TextView)vi.findViewById(R.id.clienteNome);
holder.tipo = (TextView)vi.findViewById(R.id.clienteTipo);
vi.setTag(holder);
}else{
holder = (ViewHolder)vi.getTag();
}
ClienteModel item = new ClienteModel();
item = cliente.get(position);
holder.id.setText(String.valueOf(item.getClientes_id()));
holder.nome.setText(item.getNome());
holder.tipo.setText(item.getTipo());
return vi;
}
public void setData(List<ClienteModel> clientes){
this.cliente.addAll(clientes);
this.notifyDataSetChanged();
}
public class ViewHolder
{
TextView id;
TextView nome;
TextView tipo;
}
}
And the LoadMoreTask snippet that gets the data from the database:
protected Boolean doInBackground(Integer... parameters) {
int npagina = parameters[0];
cliente= new ArrayList<ClienteModel>();
try {
Repositorio mRepositorio = new Repositorio(context);
List listaDeClientes = mRepositorio.getClientes(npagina,5,"");
cliente = listaDeClientes;
System.out.println("pagina " + npagina);
}catch (Exception e){
e.printStackTrace();
return false;
}
return true;
}
Function getClientes:
public List<ClienteModel> getClientes(Integer pagina, Integer limit, String consulta) throws SQLException {
Integer offset = pagina * limit - limit;
List<ClienteModel> listaDeRegistros = new ArrayList<ClienteModel>();
if(consulta.isEmpty()) {
query = "SELECT * FROM " + tabelaCLIENTES + " WHERE credencial_id = " + mSessao.getString("id_credencial") + " LIMIT " + offset + ", " + limit;
}else {
query = "SELECT * FROM " + tabelaCLIENTES + " WHERE (credencial_id = " + mSessao.getString("id_credencial") + ") and (nome LIKE '%"+consulta+"%') LIMIT " + offset + ", " + limit;
}
System.out.println(query);
try {
Cursor mCursor = bd.rawQuery(query, null);
if (mCursor.getCount() > 0) {
if (mCursor.moveToFirst()) {
do {
ClienteModel mClienteModel = new ClienteModel();
mClienteModel.setClientes_id(mCursor.getInt(mCursor.getColumnIndex(ClienteModel.Coluna.CLIENTES_ID)));
mClienteModel.setId_rm(mCursor.getInt(mCursor.getColumnIndex(ClienteModel.Coluna.ID_RM)));
mClienteModel.setCredencial_id(mCursor.getInt(mCursor.getColumnIndex(ClienteModel.Coluna.CREDENCIAL_ID)));
mClienteModel.setNome(mCursor.getString(mCursor.getColumnIndex(ClienteModel.Coluna.NOME)));
mClienteModel.setTipo(mCursor.getString(mCursor.getColumnIndex(ClienteModel.Coluna.TIPO)));
mClienteModel.setInformacao_adicional(mCursor.getString(mCursor.getColumnIndex(ClienteModel.Coluna.INFORMACAO_ADICIONAL)));
mClienteModel.set_criado(mCursor.getString(mCursor.getColumnIndex(ClienteModel.Coluna._CRIADO)));
mClienteModel.set_modificado(mCursor.getString(mCursor.getColumnIndex(ClienteModel.Coluna._MODIFICADO)));
mClienteModel.set_status(mCursor.getString(mCursor.getColumnIndex(ClienteModel.Coluna._STATUS)));
listaDeRegistros.add(mClienteModel);
} while (mCursor.moveToNext());
}
}
} catch (Exception e) {
e.printStackTrace();
}
return listaDeRegistros;
}
When you are hitting the end to load more, your load code is just re-loading the same 5 entries. You need to check what you have already loaded and validate if it is the end or not to stop adding entries.
try this one (exchange limit with offset):
query = "SELECT * FROM " + tabelaCLIENTES + " WHERE credencial_id = " + mSessao.getString("id_credencial") + " LIMIT " + limit + ", " + offset;