Hello. Fill the ListView from the adapter. By condition, I change the Layout of the list. When scrolling the list, the emails are mixed up, I can not figure out how to win it.

Adapter code:

public class OrderAdapter extends CursorAdapter { private LayoutInflater inflater; private class ViewHolder { TextView name; } public OrderAdapter(Context context, Cursor c, int flags) { super(context, c, flags); this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); } @Override public void bindView(View view, Context context, Cursor cursor) { ViewHolder holder = (ViewHolder) view.getTag(); holder.name.setText(cursor.getString(cursor.getColumnIndex("mitm_Name"))); //holder.time.setText(cursor.getString(holder.timeIndex)); } @Override public View newView(Context context, Cursor cursor, ViewGroup parent) { ViewHolder holder = new ViewHolder(); View v = null; if (cursor.getInt(cursor.getColumnIndex("State")) == 0) { v = inflater.inflate(R.layout.cellordername, parent, false); holder.name = (TextView) v.findViewById(R.id.txtmitmname); } else { v = inflater.inflate(R.layout.cellorder, parent, false); holder.name = (TextView) v.findViewById(R.id.txtmitmname); } v.setTag(holder); return v; } } 

Rewrote adapter:

 public Cursor getOrderData() { SQLiteDatabase database = dbHelper.getWritableDatabase(); String buildSQL = "SELECT * FROM elf_Orders ORDER BY _id"; return database.rawQuery(buildSQL, null); } public class OrderAdapter extends CursorAdapter { private LayoutInflater inflater; private class ViewHolder { TextView name; } public OrderAdapter(Context context, Cursor c, int flags) { super(context, c, flags); this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); } @Override public void bindView(View view, Context context, Cursor cursor) { } @Override public View newView(Context context, Cursor cursor, ViewGroup parent) { ViewHolder holder = new ViewHolder(); if (cursor.getInt(cursor.getColumnIndex("State")) == 0) { View v = inflater.inflate(R.layout.cellordername, parent, false); holder.name = (TextView) v.findViewById(R.id.txtmitmname); v.setTag(holder); holder.name.setText(cursor.getString(cursor.getColumnIndex("mitm_Name"))); return v; } else { View v = inflater.inflate(R.layout.cellorder, parent, false); holder.name = (TextView) v.findViewById(R.id.txtmitmname); v.setTag(holder); holder.name.setText(cursor.getString(cursor.getColumnIndex("mitm_Name"))); return v; } } } 

Now the markup is assigned correctly, but the data is displayed in a chaotic order when scrolling. In which direction to dig?

  • one
    The newView(...) method is designed to create a View object, but not to bind data to this View . Data binding to a specific View is handled by the bindView(...) method. Accordingly, you need to transfer all of your setText(...) from newView(...) to bindView(...) . This is at least. - post_zeew
  • I did it in the first version, the markup is moving there. In the second variant with the markup everything is OK, but the data jumps as they want. - Nick ELF-M
  • If everything is transferred to bindView, then scrolling data is displayed correctly, but the markup is assigned to any item. The vicious circle - Nick ELF-M
  • See how the adapter is implemented here . - post_zeew
  • Thank you very much! Everything is working. Only now we need to learn how to update the list correctly. I just do it, assign a cursor to the list, every time I add something to the database. And my whole list starts all over again, but you have to add it to the end of the list - Nick ELF-M

2 answers 2

Adapter working code:

 public Cursor getOrderData() { SQLiteDatabase database = dbHelper.getWritableDatabase(); String buildSQL = "SELECT * FROM elf_Orders ORDER BY _id"; return database.rawQuery(buildSQL, null); } public class OrderAdapter extends CursorAdapter { private LayoutInflater inflater; private class ViewHolder { TextView name; } public OrderAdapter(Context context, Cursor c, int flags) { super(context, c, flags); this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); } private int getItemViewType(Cursor cursor) { String type = cursor.getString(cursor.getColumnIndex("State")); if (type.equals("1")) { return 0; } else { return 1; } } @Override public int getItemViewType(int position) { Cursor cursor = (Cursor) getItem(position); return getItemViewType(cursor); } @Override public int getViewTypeCount() { return 2; } @Override public void bindView(View view, Context context, Cursor cursor) { ViewHolder holder = (ViewHolder) view.getTag(); holder.name.setText(cursor.getString(cursor.getColumnIndex("mitm_Name"))); } @Override public View newView(Context context, Cursor cursor, ViewGroup parent) { ViewHolder holder = new ViewHolder(); View v = null; if (cursor.getInt(cursor.getColumnIndex("State")) == 0) { v = inflater.inflate(R.layout.cellordername, parent, false); holder.name = (TextView) v.findViewById(R.id.txtmitmname); } else { v = inflater.inflate(R.layout.cellorder, parent, false); holder.name = (TextView) v.findViewById(R.id.txtmitmname); } v.setTag(holder); return v; } } 

Thank you very much!

  • What is this code for? @Override public int getViewTypeCount() { return 2; } @Override public int getViewTypeCount() { return 2; } - zayn1991
  • @ zayn1991 this method returns the number of different types of items in the adapter (in this case, 2 different types) - pavlofff
  • @pavlofff, thanks for the reply. So, this method is used specifically for its implementation? - zayn1991
  • @ zayn1991 Adapter methods getViewTypeCount() and getItemViewType() overridden when you need to implement different kind of items in the list. - pavlofff

So, for the future.

If you fill in the if adapter in the Cursor adapter, then you need to fill in the else , as the listview randomly changes the changes made. I also suffered, changed the element's id by the color of the textView through if, scrolled, and upon returning it was already chaos. Then they prompted me to fill in the else with the color that should be according to the standard - it helped. I think my answer will help someone.

The code of the Adapter class that changes the color by id of the element:

 package ru.diit.healthychildrenfree.adapters; import android.content.Context; import android.database.Cursor; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.CursorAdapter; import android.widget.TextView; import ru.diit.healthychildrenfree.R; /** * Created by zaynulabid on 12.01.16. */ public class GroupAdapter extends CursorAdapter { private LayoutInflater mInflater; public GroupAdapter(Context context, Cursor cursor){ super(context, cursor, 1); mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); } @Override public View newView(Context context, Cursor cursor, ViewGroup parent) { return mInflater.inflate(R.layout.item, parent,false); } @Override public void bindView(View view, Context context, Cursor cursor) { TextView txName = (TextView) view.findViewById(R.id.textViewNameOfGrup); // инициализировали textView String name = cursor.getString(cursor.getColumnIndex("name_gruppi"));// достали строку в переменную txName.setText(name);// присвоили значение TextView tvOpisanieGruppi = (TextView) view.findViewById(R.id.textViewOpisanieGrup); String opisanieGruppi = cursor.getString(cursor.getColumnIndex("opisanie_gruppi")); tvOpisanieGruppi.setText(opisanieGruppi); long idGroup = cursor.getLong(cursor.getColumnIndex("id_gruppi")); //нашли айди элемента с БД //Log.e("GAdapter", "bindView: idGroup = " + idGroup); if (idGroup == 11){ // если id = 11, меняем цвета текстов на те, что есть в файле colors //Log.e("GAdapter", "bindView: idGroup == 11\nзакрашиваем строчки"); txName.setTextColor(context.getResources().getColor(R.color.textColorTitleFilled)); tvOpisanieGruppi.setTextColor(context.getResources().getColor(R.color.textColorTitleFilled)); } else { // если не равен 11 (как раз этот блок не даст цветам меняться при прокрутке) txName.setTextColor(context.getResources().getColor(R.color.textColorTitle)); tvOpisanieGruppi.setTextColor(context.getResources().getColor(R.color.textColorTitle)); } } } 

No, I do not think that this solution is correct, but it is working and it is easy to use this solution when developing simple tasks in the cursor adapter.