1
I’m having a problem with a Listview that isn’t loading the values. That’s my situation:
I have an application that at a certain point opens a Tabbar with 2 Fragments and inside one of these Fragments I have a Listview that I am trying to popular through a POST request with Volley.
I’m bringing the values correctly through the webservice It brings results (getCount() in the Adapter has values), but it’s not entering getView() so I think that’s why it’s not displaying anything in 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);
}
}
Layout of the Fragment
<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 did a test populating a public variable on another page and then populated any Listview in another Activity and it worked, it goes through getView() and shows the Listview correctly but not in the Fragment being populated in Fragment itself.
Any suggestions? I’ve been searching for days for a solution and nothing..
Thank you very much, thank you very much! worked perfectly!
– H. Cardoso