Android ListView не может l oop невидимый элемент в ScrollBar - PullRequest
0 голосов
/ 14 февраля 2020

У меня очень плохая ошибка

У меня есть список с пользовательским адаптером

Если я ставлю 15 строк в списке, я получаю за l oop

l oop в видимом элементе только на экране

Если на экране взять 10 элементов, он будет l oop только в 10 элементах и ​​не может l oop на других 5 элементах

Это изображение https://ibb.co/Zgnx67T

https://ibb.co/f0wLFS6

Это пользовательский адаптер

package com.bella_system.mostafasalama.bellasystem;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import static com.bella_system.mostafasalama.bellasystem.Constant.Add_Units_Count;
import static com.bella_system.mostafasalama.bellasystem.Constant.ItemID;
import static com.bella_system.mostafasalama.bellasystem.Constant.ItemName;
import static com.bella_system.mostafasalama.bellasystem.Constant.TextLineID;
import static com.bella_system.mostafasalama.bellasystem.Constant.TextLineM;
import static com.bella_system.mostafasalama.bellasystem.Constant.SUintID;
import static com.bella_system.mostafasalama.bellasystem.Constant.TextPrice;
import static com.bella_system.mostafasalama.bellasystem.Constant.TextQty;
import static com.bella_system.mostafasalama.bellasystem.Constant.TextSubQty;
import static com.bella_system.mostafasalama.bellasystem.Constant.Total_COLUMN;
import static com.bella_system.mostafasalama.bellasystem.Constant.UintTwoID;
import static com.bella_system.mostafasalama.bellasystem.Constant.UintTwoPrice;
public class ListViewAdapter extends BaseAdapter{
public ArrayList<HashMap<String, String>> list;
Activity activity;
public ListViewAdapter(Activity activity,ArrayList<HashMap<String, String>> list){
        super();
        this.activity = activity;
        this.list = list;
    }

    @Override public int getCount() { return list.size(); }
    @Override public Object getItem(int position) { return list.get(position); }
    @Override public long getItemId(int position) { return 0; }

    private class ViewHolder{
        TextView TextLineID;
        TextView TextItemID;
        TextView textLineM;
        TextView TextItemName;
        TextView TextTextQty;
        TextView TextPrice;
        TextView TextSubQty;
        TextView UintTwoPrice;
        TextView TextItemTotal;
        TextView TextUintID;
        TextView UintTwoID;
        TextView Add_Units_Count;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
     ViewHolder holder;
        if(convertView == null){
            LayoutInflater inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView=inflater.inflate(R.layout.datagridviewinv, parent,false);
            holder=new ViewHolder();
            holder.TextLineID = (TextView) convertView.findViewById(R.id.TextLineID);
            holder.TextItemID=(TextView) convertView.findViewById(R.id.TextItemID);
            holder.textLineM=(TextView) convertView.findViewById(R.id.TextLineM);
            holder.TextItemName=(TextView) convertView.findViewById(R.id.ItemName);
            holder.TextTextQty=(TextView) convertView.findViewById(R.id.TextQty);
            holder.TextPrice=(TextView) convertView.findViewById(R.id.TextPrice);
            holder.TextSubQty=(TextView) convertView.findViewById(R.id.TextSubQty);
            holder.UintTwoPrice=(TextView) convertView.findViewById(R.id.UintTwoPrice);
            holder.TextItemTotal=(TextView) convertView.findViewById(R.id.TextItemTotal);
            holder.TextUintID=(TextView) convertView.findViewById(R.id.TextUintID);
            holder.UintTwoID=(TextView) convertView.findViewById(R.id.UintTwoID);
            holder.Add_Units_Count=(TextView) convertView.findViewById(R.id.Add_Units_Count);
            convertView.setTag(holder);
        } else{ holder=(ViewHolder) convertView.getTag(); }

        HashMap<String, String> map = list.get(position);
     if (map != null) {
      holder.TextLineID.setText(map.get(TextLineID));
      holder.TextItemID.setText(map.get(ItemID));
      holder.textLineM.setText(map.get(TextLineM));
      holder.TextItemName.setText(map.get(ItemName));
      holder.TextTextQty.setText(map.get(TextQty));
      holder.TextPrice.setText(map.get(TextPrice));
      holder.TextSubQty.setText(map.get(TextSubQty));
      holder.UintTwoPrice.setText(map.get(UintTwoPrice));
      holder.TextItemTotal.setText(map.get(Total_COLUMN));
      holder.TextUintID.setText(map.get(SUintID));
      holder.UintTwoID.setText(map.get(UintTwoID));
      holder.Add_Units_Count.setText(map.get(Add_Units_Count));
     }
        return convertView;
    }
}

А это мой для л oop

    for (int i = 0; i < listView.getAdapter().getCount(); i++) {
    if (listView.getChildAt(i) != null) {
    TextView LineID = (TextView) listView.getChildAt(i).findViewById(R.id.TextLineID);
    TextView ItemIDV = (TextView) listView.getChildAt(i).findViewById(R.id.TextItemID);
    TextView LineNumber = (TextView) listView.getChildAt(i).findViewById(R.id.TextLineM);
    TextView ItemName = (TextView) listView.getChildAt(i).findViewById(R.id.ItemName);
    TextView Qty = (TextView) listView.getChildAt(i).findViewById(R.id.TextQty);
    TextView SubQty = (TextView) listView.getChildAt(i).findViewById(R.id.TextSubQty);
    TextView Price = (TextView) listView.getChildAt(i).findViewById(R.id.TextPrice);
    TextView ItemTotal = (TextView) listView.getChildAt(i).findViewById(R.id.TextItemTotal);
    TextView SUintID = (TextView) listView.getChildAt(i).findViewById(R.id.TextUintID);
    TextView TUintTwoID = (TextView) listView.getChildAt(i).findViewById(R.id.UintTwoID);
    TextView UintTwoPriceV = (TextView) listView.getChildAt(i).findViewById(R.id.UintTwoPrice);
    TextView Add_Units_CountASDV = (TextView) listView.getChildAt(i).findViewById(R.id.Add_Units_Count);

}```

1 Ответ

0 голосов
/ 14 февраля 2020

Ты почти у цели. Вы пытаетесь извлечь значения ListView data , чтобы сохранить его локально, что означает, что в вашем l oop вам нужно будет указать на список data, переданный в adapter что вы настроили.

for (int i = 0; i < listView.getAdapter().getCount(); i++) {
    // if you have direct access to the list
    HashMap<String, String> itemMap = list.get(i);
    // if you don't have direct access you can do something like instead:
    // HashMap<String, String> itemMap = ((ListViewAdapter)listView.getAdapter()).list.get(i);
    if (itemMap != null) {
      String txtLineId = itemMap.get(TextLineID)
    }
}

Это почти тот же подход, который используется при обработке onClicks, смотрите первый блок кода здесь: { ссылка }

...