0

I have a very bad error

I have list view with a custom Adapter

If I put 15 row in list win I make for loop

the loop in visible item on the screen only

If the screen take 10 item it will loop in 10 item only and can't loop on other 5 item

This image https://ibb.co/Zgnx67T

https://ibb.co/f0wLFS6

This is custom adapter

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;
    }
}

And this my for loop

    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);

}```
Varad Mondkar
  • 1,441
  • 1
  • 18
  • 29
  • I advice you to learn how listview holders are used. You don't need loop 15 times and get the ui components from the view. The whole point of listview is to recycle the views. Also listview is long gone, I will recommend you to learn recyclerview and recyclerview adapter (it implements already the holder pattern unlike with listview that you need to do it manually) – DemoDemo Feb 14 '20 at 18:49
  • I need to save the 15 row in database i need loop for that – Mostafa Salama Feb 14 '20 at 18:54
  • Instead of looping through the child views in the ListView adapter, I recommend you loop through the data of the ListView adapter. Much like the ListView adapter code, use "HashMap map = list.get(position);" to get the metadata at that specific position. Then extract each value you want to save to the DB using it's key (the code already exists in your adapter as map.get(TextLineID)). – MikeOscarEcho Feb 14 '20 at 19:00
  • Can you explain how to do that – Mostafa Salama Feb 14 '20 at 19:02
  • @MostafaSalama Sure, posted as an answer as I can't format in comments. – MikeOscarEcho Feb 14 '20 at 19:31

1 Answers1

0

You're almost there. You're trying to extract the values of the ListView data in order to persist it locally which means in your loop you'd need to point to the list data passed into the adapter that you've configured.

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)
    }
}

This is pretty much the same approach used when handling onClicks, see first code block here: https://stackoverflow.com/a/38919205/2340813

MikeOscarEcho
  • 535
  • 2
  • 12
  • 27