Why did the ListView repeated every 6th item?

☆樱花仙子☆ 提交于 2019-11-27 05:27:19
eldarerathis
@Override
public View getView(int pos, View convertView, ViewGroup parent) {

    if(convertView == null){
        convertView = mLayoutInflater.inflate(zeilen_Layout, null);

        //Assignment code
    }
    return convertView;
}

The reason this is happening is because of how you've defined this method (which is getView()). It only modifies the convertView object if it is null, but after the first page of items is filled Android will start recycling the View objects. This means that i.e. the seventh one that gets passed in is not null - it is the object that scrolled off the top of the screen previously (the first item).

The conditional causes you just return what was passed in to the method so it appears to repeat the same 6 items over and over.

Modify you getView of list adapter as follow:

ViewHolder viewHolder = new ViewHolder();

@Override
public View getView(int pos, View convertView, ViewGroup parent) {


    if(convertView == null){
        convertView = mLayoutInflater.inflate(zeilen_Layout, null);

        viewHolder.txtTestText = (TextView) findViewById(R.id.txt_test_item);


        // Main code goes here

        convertView.setTag(viewHolder); // this function remember the the view you have inflated

    }else{
        viewHolder = (ViewHolder) convertView.getTag(); // return last set view of ith item
    }


    // set data to view here

    viewHolder.txtTestText.setText(listItem.get(postion).getSelectedItem());

    return convertView;

}




private class ViewHolder {
    TextView txtTestText;
}

Here is my Solution to Multi check in a listview

A normal listview inside any xml

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <ListView
        android:id="@+id/lv_lay_yoklamaal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"></ListView>
</LinearLayout>

item xml of listview that needed in adapter class seem liste this

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_marginTop="2dp"
        android:orientation="horizontal"
        android:paddingLeft="10dp">
        <LinearLayout
            android:layout_width="50dp"
            android:layout_height="match_parent"
            android:background="@drawable/cw_full_oval"
            android:gravity="center">
            <LinearLayout
                android:layout_width="35dp"
                android:layout_height="35dp"
                android:gravity="center">
                <TextView
                    android:id="@+id/txt_item_yoklamaal_id"
                    android:layout_width="40dp"
                    android:layout_height="40dp"
                    android:background="@drawable/cw_full_oval"
                    android:gravity="center"
                    android:text="1"
                    android:textSize="25dp"
                    android:textStyle="bold">
                </TextView>
            </LinearLayout>
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
            <TextView
                android:id="@+id/txt_item_yoklamaal_name"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:gravity="center|start"
                android:paddingLeft="15dp"
                android:text="Ahmet Kemal Bali">
            </TextView>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:orientation="horizontal"
                android:paddingLeft="8dp">
                <CheckBox
                    android:id="@+id/rb_item_yoklamaal_var"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:text="Var" />
                <CheckBox
                    android:id="@+id/rb_item_yoklamaal_yok"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:text="Yok" />
                <CheckBox
                    android:id="@+id/rb_item_yoklamaal_izinli"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:text="İzinli" />
                <CheckBox
                    android:id="@+id/rb_item_yoklamaal_gorevli"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:text="Görevli" />
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

The Activity where I call Listview and Adapter

public class YoklamaAlActivity extends Activity {

    private ListView lv_yoklama;
    private List<TalebeModel> alltalebe;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.lay_yoklamaal);


        lv_yoklama = (ListView) findViewById(R.id.lv_lay_yoklamaal);


        List<TalebeModel> talabList = getAlltalebe();
        YoklamaAlAdapter yoklamaAdapter = new YoklamaAlAdapter(YoklamaAlActivity.this, talabList);
        lv_yoklama.setAdapter(yoklamaAdapter);


    }

    public List<TalebeModel> getAlltalebe() {

        List<TalebeModel> modelList = new ArrayList<>();
        modelList.add(new TalebeModel(1, "Samir Samedov",false,false,false,false));
        modelList.add(new TalebeModel(2, "Ahmet Kemal Bali",false,false,false,false));
        modelList.add(new TalebeModel(3, "Nurullah Atik",false,false,false,false));

        modelList.add(new TalebeModel(4, "Samir Samedov",false,false,false,false));
        modelList.add(new TalebeModel(5, "Ahmet Kemal Bali",false,false,false,false));
        modelList.add(new TalebeModel(6, "Nurullah Atik",false,false,false,false));

        modelList.add(new TalebeModel(7, "Samir Samedov",false,false,false,false));
        modelList.add(new TalebeModel(8, "Ahmet Kemal Bali",false,false,false,false));
        modelList.add(new TalebeModel(9, "Nurullah Atik",false,false,false,false));

        modelList.add(new TalebeModel(7, "Samir Samedov",false,false,false,false));
        modelList.add(new TalebeModel(8, "Ahmet Kemal Bali",false,false,false,false));
        modelList.add(new TalebeModel(9, "Nurullah Atik",false,false,false,false));

        modelList.add(new TalebeModel(7, "Samir Samedov",false,false,false,false));
        modelList.add(new TalebeModel(8, "Ahmet Kemal Bali",false,false,false,false));
        modelList.add(new TalebeModel(9, "Nurullah Atik",false,false,false,false));


        return modelList;
    }
}

The Model Class ( set its field getter and setters)

public class TalebeModel {
    private int numara;
    private String ad;
    private boolean var;
    private boolean yok;
    private boolean izinli;
    private boolean gorevli;


    public TalebeModel() {
    }

    public TalebeModel(int numara, String ad) {
        this.numara = numara;
        this.ad = ad;
    }

    public TalebeModel(int numara, String ad, boolean var, boolean yok, boolean izinli, boolean gorevli) {
        this.numara = numara;
        this.ad = ad;
        this.var = var;
        this.yok = yok;
        this.izinli = izinli;
        this.gorevli = gorevli;
    }
}

And finnaly the Adapter class

/**
 * Created by Samir on 24.1.2017.
 */
public class YoklamaAlAdapter extends BaseAdapter {

    Context context;
    List<TalebeModel> talebeList;
    ViewHolder holder;
    ArrayList<Boolean> checked;

    int displaySize;

    public YoklamaAlAdapter(Context contextt, List<TalebeModel> talebeModels) {
        this.context = contextt;
        this.talebeList = talebeModels;
        displaySize = 10;

    }

    private class ViewHolder {
        TextView mName;
        TextView mId;
        TextView mVar;
        TextView mYok;
        ImageView mIzinli;
        TextView mGorevli;
        LinearLayout lin_lay_etkinlik;

        CheckBox rbVar;
        CheckBox rbYok;
        CheckBox rbIzinli;
        CheckBox rbGorevli;
    }

    @Override
    public int getCount() {
        return talebeList.size();
    }

    /* @Override
     public int getViewTypeCount() {

         return getCount();
     }*/

    @Override
    public TalebeModel getItem(int i) {
        return talebeList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        holder = new ViewHolder();
        final TalebeModel currentTalebe;
        LayoutInflater mInflater = (LayoutInflater)
                context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

        if (view == null) {
            view = mInflater.inflate(R.layout.item_yoklamaal, null);
            holder = new ViewHolder();
            holder.mName = (TextView) view.findViewById(R.id.txt_item_yoklamaal_name);
            holder.mId = (TextView) view.findViewById(R.id.txt_item_yoklamaal_id);
            holder.rbVar = (CheckBox) view.findViewById(R.id.rb_item_yoklamaal_var);
            holder.rbYok = (CheckBox) view.findViewById(R.id.rb_item_yoklamaal_yok);
            holder.rbIzinli = (CheckBox) view.findViewById(R.id.rb_item_yoklamaal_izinli);
            holder.rbGorevli = (CheckBox) view.findViewById(R.id.rb_item_yoklamaal_gorevli);

            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }

        currentTalebe = getItem(i);
        holder.mName.setText(currentTalebe.getAd());
        holder.mId.setText("" + currentTalebe.getNumara());

        holder.rbVar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                holder.rbYok.clearFocus();
                if (currentTalebe.isVar()) {
                    currentTalebe.setVar(false);
                    // holder.rbVar.setChecked(false);
                    //Toast.makeText(context, "Var a tıkladın", Toast.LENGTH_SHORT).show();
                    holder.rbYok.setChecked(false);
                    holder.rbIzinli.setChecked(false);
                    holder.rbGorevli.setChecked(false);
                } else {
                    //holder.rbVar.setChecked(true);
                    currentTalebe.setVar(true);
                    holder.rbYok.setChecked(false);
                    holder.rbIzinli.setChecked(true);
                    holder.rbGorevli.setChecked(true);
                }
                currentTalebe.setYok(false);
                currentTalebe.setIzinli(false);
                currentTalebe.setGorevli(false);
            }
        });
        setCheckboxesChecked(currentTalebe);

        holder.rbYok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (currentTalebe.isYok()) {
                    currentTalebe.setYok(false);

                } else {
                    currentTalebe.setYok(true);
                }
                currentTalebe.setVar(false);
                currentTalebe.setIzinli(false);
                currentTalebe.setGorevli(false);
            }
        });
        setCheckboxesChecked(currentTalebe);

        holder.rbGorevli.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (currentTalebe.isGorevli()) {
                    currentTalebe.setGorevli(false);
                } else {
                    currentTalebe.setGorevli(true);
                }
                currentTalebe.setVar(false);
                currentTalebe.setYok(false);
                currentTalebe.setIzinli(false);
            }
        });

        setCheckboxesChecked(currentTalebe);

        holder.rbIzinli.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (currentTalebe.isIzinli()) {
                    currentTalebe.setIzinli(false);
                } else {
                    currentTalebe.setIzinli(true);
                }
                currentTalebe.setVar(false);
                currentTalebe.setYok(false);
                currentTalebe.setGorevli(false);
            }
        });
        setCheckboxesChecked(currentTalebe);

        return view;
    }

    /*@Override
    public boolean isEnabled(int position) {
        return mListItem[position].isEnabled();
    }*/

    private void setCheckboxesChecked(TalebeModel currentTalebe) {
        holder.rbVar.setChecked(currentTalebe.isVar());
        holder.rbYok.setChecked(currentTalebe.isYok());
        holder.rbIzinli.setChecked(currentTalebe.isIzinli());
        holder.rbGorevli.setChecked(currentTalebe.isGorevli());
    }
}

This is really a headache and no one giving any logical solution, that is why I shared all my work with this issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!