getView is not being called but getCount has items in ListView

1

I'm having a problem with a ListView that is not loading the values. This is my situation:

I have an application that at one point opens a TabBar with 2 Fragments and inside one of these Fragments I have a ListView that I am trying to populate through a POST request with Volley.

I'm getting values correctly through webservice and it brings results (getCount () in the Adapter has values), but it's not getting into getView () so I guess that should be why it's not displaying anything in the ListView.

Adapter:

 public class LancarVendaCarrinhoListViewAdapter extends BaseAdapter
 {
    private Context mContext;
    private List<PesquisarProdutoObjetoRetorno> mDataSource;

    public LancarVendaCarrinhoListViewAdapter(Context context, List<PesquisarProdutoObjetoRetorno> items)
    {
        mContext = context;
        mDataSource = items;
    }

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

    @Override
    public Object getItem(int position) {
        return mDataSource.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        try
        {
            LayoutInflater mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View sView = mInflater.inflate(R.layout.activity_lancarvenda_carrinho_list_view_item, parent, false);

            PesquisarProdutoObjetoRetorno sItem = (PesquisarProdutoObjetoRetorno) getItem(position);

            TextView descricao = (TextView)sView.findViewById(R.id.lancarvenda_carrinho_item_txtdescricao);
            descricao.setText(sItem.getDescricao());

            TextView preco = (TextView)sView.findViewById(R.id.lancarvenda_carrinho_item_txvpreco);
            preco.setText(String.valueOf(sItem.getPreco()));

            EditText quantidade = (EditText)sView.findViewById(R.id.lancarvenda_carrinho_item_etquantidade);
            quantidade.setText("1");

            return sView;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return null;
        }
    }

    public List<PesquisarProdutoObjetoRetorno> getItemList()
    {
        return mDataSource;
    }

    public void setItemList(List<PesquisarProdutoObjetoRetorno> itemList)
    {
        this.mDataSource = itemList;
    }
}

Fragment:

 public class LancarVendaCarrinhoFragment extends Fragment 
 {
    private ListView fCarrinhoResultado;
    List<PesquisarProdutoObjetoRetorno> sCarrinhoAuxiliar;
    LancarVendaCarrinhoListViewAdapter sAdaptador;

    private PesquisarProdutoRequest sCorpoBusca = new PesquisarProdutoRequest();

    public LancarVendaCarrinhoFragment() {

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        LayoutInflater sInflater = getActivity().getLayoutInflater();
        View sView = sInflater.inflate(R.layout.fragment_lancarvenda_carrinho, null);

        fCarrinhoResultado = (ListView) sView.findViewById(R.id.lancarvenda_carrinho_lvwresultado);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        Time sData = new Time();
        sData.setToNow();

        sCorpoBusca = new PesquisarProdutoRequest();
        sCorpoBusca.setIdioma("pt-BR");
        sCorpoBusca.setUsaGrade(Variaveis.UsaGrade);
        sCorpoBusca.setFilial(Variaveis.Filial);
        sCorpoBusca.setUFFilial(Variaveis.UFFilial);
        sCorpoBusca.setTipoSistema(Variaveis.TipoSistema);
        sCorpoBusca.setNumeroCaixa(Variaveis.NumeroCaixa);
        sCorpoBusca.setRede(Variaveis.Rede);
        sCorpoBusca.setTabelaTributo((byte) 0);
        sCorpoBusca.setCodigoProduto("0030900939");

        PesquisarProduto();


        return inflater.inflate(R.layout.fragment_lancarvenda_carrinho, container, false);
    }


    private void PesquisarProduto() {
        RequestQueue sQueue = Volley.newRequestQueue(getActivity());
        String sEnderecoBase = "http://www.minha-url";

        sCarrinhoAuxiliar = new ArrayList<>();

        StringRequest sRequest = new StringRequest(Request.Method.POST, sEnderecoBase, new Response.Listener<String>() {

            @Override
            public void onResponse(String response) {
                PesquisarProdutoResponse sResultado = new Gson().fromJson((String) response, PesquisarProdutoResponse.class);
                if (sResultado.getCodigoRetorno() == 0) {
                    Variaveis.Carrinho.add(sResultado);

                    try {

                        sCarrinhoAuxiliar.clear();

                        for (int i = 0; i < Variaveis.Carrinho.size(); i++) {
                            PesquisarProdutoObjetoRetorno sItem = Variaveis.Carrinho.get(i).getDadosProduto();
                            sCarrinhoAuxiliar.add(sItem);
                            Variaveis.CarrinhoAuxiliar.add(sItem);
                        }

                        sAdaptador = new LancarVendaCarrinhoListViewAdapter(getActivity(), sCarrinhoAuxiliar);
                        sAdaptador.setItemList(sCarrinhoAuxiliar);
                        fCarrinhoResultado.setAdapter(sAdaptador);

                        sAdaptador.notifyDataSetChanged();


                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                byte[] sBytesResposta = error.networkResponse.data;
                String sTexto = new String(sBytesResposta);
                Mensagem.ExibirAlert(getActivity(), String.valueOf(error.networkResponse.statusCode));
            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> sHeaders = new HashMap<>();
                sHeaders.put("Authorization", "Bearer " + Variaveis.ApiToken);
                return sHeaders;
            }

            @Override
            public byte[] getBody() throws AuthFailureError {
                return new Gson().toJson(sCorpoBusca).getBytes();
            }

            @Override
            public String getBodyContentType() {
                return "application/json";
            }
        };

        sQueue.add(sRequest);
    }
}

Fragment Layout

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="15dp">

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/lancarvenda_carrinho_lvwresultado"
        android:orientation="vertical" />

</LinearLayout>

Cell layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <de.hdodenhof.circleimageview.CircleImageView xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/lancarvenda_carrinho_item_imvFoto"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_gravity="center_horizontal"
        android:background="@drawable/circulo_foto2"
        app:border_color="#898989"
        app:border_width="2dp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_marginLeft="10dp">

        <TextView
            android:id="@+id/lancarvenda_carrinho_item_txtdescricao"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:text="Relogio Mormaii 2730"
            android:textSize="17dp"
            android:textStyle="bold"
            android:layout_marginRight="8dp"
            android:lines="2"
            android:minLines="2"
            android:singleLine="false"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">

            <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent">

                <TextView
                    android:id="@+id/lancarvenda_carrinho_item_txvpreco"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_centerHorizontal="true"
                    android:layout_marginTop="8dp"
                    android:text="R$ 399,89"
                    android:textColor="#1ba39c"
                    android:textSize="19dp"
                    android:layout_marginRight="70dp"/>

                <EditText
                    android:id="@+id/lancarvenda_carrinho_item_etquantidade"
                    android:layout_width="50dp"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:backgroundTint="#9a9b9c"
                    android:hint="0"
                    android:textColor="#2a2d2e"
                    android:layout_marginRight="8dp"/>

            </RelativeLayout>

        </LinearLayout>
    </LinearLayout>

</LinearLayout>

I have a test populate a public variable on another page and then populate a ListView any in another Activity and it worked, it goes through getView () and shows the ListView correctly but not in the Fragment being populated in the Fragment itself. p>

Any suggestions? I've been searching for a solution for days and nothing ..

    
asked by anonymous 31.03.2017 / 21:00

1 answer

0

You are popping an instance of ListView that is not the one that appears in fragment .

The ListView that is displayed in fragment is in the layout that is created in onCreateView() , through inflater.inflate(R.layout.fragment_lancarvenda_carrinho, container, false)

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    Time sData = new Time();
    sData.setToNow();

    sCorpoBusca = new PesquisarProdutoRequest();
    .....
    .....
    sCorpoBusca.setCodigoProduto("0030900939");

    PesquisarProduto();

    return inflater.inflate(R.layout.fragment_lancarvenda_carrinho, container, false);
} 

The ListView that was assigned the adapter was created in onCreate()

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    LayoutInflater sInflater = getActivity().getLayoutInflater();
    View sView = sInflater.inflate(R.layout.fragment_lancarvenda_carrinho, null);

    fCarrinhoResultado = (ListView) sView.findViewById(R.id.lancarvenda_carrinho_lvwresultado);
}

Get the reference to the ListView in onCreateView()

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View sView = inflater.inflate(R.layout.fragment_lancarvenda_carrinho, container, false);

    fCarrinhoResultado = (ListView) sView.findViewById(R.id.lancarvenda_carrinho_lvwresultado);

    Time sData = new Time();
    sData.setToNow();

    sCorpoBusca = new PesquisarProdutoRequest();
    .....
    .....
    sCorpoBusca.setCodigoProduto("0030900939");

    PesquisarProduto();

    return view;
} 

Delete method onCreate() .

    
31.03.2017 / 23:10