在ListView Android中使用两个图像

时间:2019-01-17 13:31:56

标签: android image listview android-listview

我试图使用ArrayList将两个图像放入listView中,但是当我运行该项目时,仅出现一个图像(卡车一个),但没有出现星星 。 Android Studio没有显示任何错误。

有人可以看一下我的代码并指出我,我在做什么错了?

ListaDistribuidoresActivity.java

package com.pedido.meu.telas_meu_pedido;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;

public class ListaDistribuidoresActivity extends AppCompatActivity {

    ItemAdapter adapter;
    ArrayList<Integer> idImages;
    ArrayList<String> nameList;
    ArrayList<Integer> starImage;

    int ids[]={R.mipmap.ic_distribuidor};
    String names[]={"DISTRIBUIDOR"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lista_distribuidores);
        ListView listViewDistribuidores = findViewById(R.id.txtListViewDistribuidores);

        idImages = new ArrayList<>();
        idImages = getList();
        starImage = new ArrayList<>();
        starImage = getListStar();
        nameList = new ArrayList<>();
        nameList = getNameList();
        ItemAdapter adapter = new ItemAdapter(ListaDistribuidoresActivity.this, idImages,nameList, starImage);
        listViewDistribuidores.setAdapter(adapter);
        listViewDistribuidores.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id)
            {
                Toast.makeText(ListaDistribuidoresActivity.this, "Distribuidor " + nameList.get(position) + "selecionado", Toast.LENGTH_SHORT).show();
            }
        });
    }

    private ArrayList<Integer> getListStar()
    {
        starImage = new ArrayList<>();
        starImage.add(R.mipmap.ic_star_round);
        return  starImage;
    }

    private ArrayList<String> getNameList()
    {
        nameList = new ArrayList<>();
        nameList.add("DISTRIBUIDOR 1");
        nameList.add("DISTRIBUIDOR 2");
        nameList.add("DISTRIBUIDOR 3");
        nameList.add("DISTRIBUIDOR 4");
        return  nameList;
    }

    private ArrayList<Integer> getList()
    {
        idImages = new ArrayList<>();
        idImages.add(R.mipmap.ic_distribuidor);
        idImages.add(R.mipmap.ic_distribuidor);
        idImages.add(R.mipmap.ic_distribuidor);
        idImages.add(R.mipmap.ic_distribuidor);
        return idImages;
    }
}

ItemAdapter.java

package com.pedido.meu.telas_meu_pedido;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

public class ItemAdapter extends BaseAdapter
{
    private Context context;
    private ArrayList<Integer> listaId;
    private ArrayList<String> listaNome;
    private ArrayList<Integer> listaStar;

    public ItemAdapter(Context context, ArrayList<Integer> listaId, ArrayList<String> listaNome, ArrayList<Integer> listaStar)
    {
        this.context = context;
        this.listaId = listaId;
        this.listaNome=listaNome;
        this.listaStar=listaStar;
    }

    public ItemAdapter(ListaProdutosActivity listaProdutosActivity, ArrayList<Integer> idImages, ArrayList<String> nameList)
    {
        this.context=listaProdutosActivity;
        this.listaId=idImages;
        this.listaNome=nameList;
    }

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

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

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

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

        if (convertView == null)
        {
            convertView = View.inflate(context, R.layout.list_item_produtos, null);
        }
        ImageView img = convertView.findViewById(R.id.imgListItemProduto);
        TextView tv =  convertView.findViewById(R.id.txtListItemProduto);
        img.setImageResource(listaId.get(position));
        tv.setText(listaNome.get(position));

        return convertView;
    }
}

Imagem.java

package com.pedido.meu.telas_meu_pedido;

public class Imagem
{
    private int imageId;
    private String imageName;

    public int getImageId() {
        return imageId;
    }

    public void setImageId(int imageId) {
        this.imageId = imageId;
    }

    public String getImageName() {
        return imageName;
    }

    public void setImageName(String imageName) {
        this.imageName = imageName;
    }
}

activity_lista_distribuidores.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_height="match_parent"
    android:layout_width="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.pedido.meu.telas_meu_pedido.ListaDistribuidoresActivity"
    >

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@mipmap/ic_distribuidor"
        android:layout_gravity="center"
        android:layout_marginTop="30dp"
        />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/txtTitleListaDistribuidores"
        android:gravity="center"
        android:textColor="@color/colorPrimary"
        android:textStyle="bold"
        android:textSize="14pt"
        android:layout_marginTop="15dp"
        android:layout_marginBottom="20dp"
        />

    <ListView
        android:id="@+id/txtListViewDistribuidores"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </ListView>

</LinearLayout>

list_item_distribuidor.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android" >

    <ImageView
        android:id="@+id/imgListaProdutos"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:background="@mipmap/ic_acai"
        android:layout_marginLeft="8dp"
        />

    <TextView
        android:id="@+id/txtDistribuidor"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="@+id/imgListaProdutos"
        />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@mipmap/ic_star_round"
        android:layout_alignParentTop="@+id/imgListaProdutos"
        android:layout_marginLeft="30dp"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>

4 个答案:

答案 0 :(得分:0)

使用此库:

implementation 'com.squareup.picasso:picasso:2.5.2'

,然后在适配器中执行此操作:

ImageView img = convertView.findViewById(R.id.imgListItemProduto);
Picasso.with(context).load(listaId.get(position)).into(img);

答案 1 :(得分:0)

用下面的一个更新您的list_item_distribuidor.xml

DLM='09'x

和您的ItemAdapter.java类(其中一个以下)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android" >

    <ImageView
        android:id="@+id/imgListaProdutos"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:background="@mipmap/ic_acai"
        android:layout_marginLeft="8dp"
        />

    <TextView
        android:id="@+id/txtDistribuidor"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="@+id/imgListaProdutos"
        />

    <ImageView android:id="@+id/imgListaStar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@mipmap/ic_star_round"
        android:layout_alignParentTop="@+id/imgListaProdutos"
        android:layout_marginLeft="30dp"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>

您没有设置图像, 让我知道它是否有效

答案 2 :(得分:0)

您的XML和适配器存在问题。首先,将 id 添加到第二个图像(我想是星形图像)的XML中。

list_item_distribuidor.xml

 <ImageView
    android:id="@+id/imgListStars"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@mipmap/ic_star_round"
    android:layout_alignParentTop="@+id/imgListaProdutos"
    android:layout_marginLeft="30dp"
    />

然后在 ItemAdapter.java 中通过其 ID 对其进行初始化,并从列表中设置其值。这与您处理其他(卡车)图像的方式相同。

    ImageView imgStar = convertView.findViewById(R.id.imgListStars);
    imgStar.setImageResource(listaStar.get(position));

答案 3 :(得分:0)

完整功能代码

ListaDistribuidoresActivity.java

package com.pedido.meu.telas_meu_pedido.controller;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Toast;

import com.pedido.meu.telas_meu_pedido.R;
import com.pedido.meu.telas_meu_pedido.adapter.ItemAdapter;

import java.util.ArrayList;

public class ListaDistribuidoresActivity extends AppCompatActivity {

    ItemAdapter adapter;
    ArrayList<Integer> idImages;
    ArrayList<String> nameList;
    ArrayList<Integer> starImage;

    int ids[]={R.mipmap.ic_distribuidor};
    String names[]={"DISTRIBUIDOR"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lista_distribuidores);
        ListView listViewDistribuidores = findViewById(R.id.txtListViewDistribuidores);
        Spinner spinner = findViewById(R.id.spinnerCategoria);

        ArrayAdapter<CharSequence> adapterSpinner = ArrayAdapter.createFromResource(ListaDistribuidoresActivity.this, R.array.category_array, android.R.layout.simple_spinner_dropdown_item);
        adapterSpinner.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapterSpinner);

        idImages = new ArrayList<>();
        idImages = getList();
        starImage = new ArrayList<>();
        starImage = getListStar();
        nameList = new ArrayList<>();
        nameList = getNameList();

        ItemAdapter adapter = new ItemAdapter(ListaDistribuidoresActivity.this, idImages,nameList, starImage);
        listViewDistribuidores.setAdapter(adapter);
        listViewDistribuidores.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id)
            {
                Toast.makeText(ListaDistribuidoresActivity.this, "Distribuidor " + nameList.get(position) + "selecionado", Toast.LENGTH_SHORT).show();
            }
        });
    }

    private ArrayList<Integer> getListStar()
    {
        starImage = new ArrayList<>();
        starImage.add(R.mipmap.ic_star_round);
        return  starImage;
    }

    private ArrayList<String> getNameList()
    {
        nameList = new ArrayList<>();
        nameList.add("DISTRIBUIDOR 1");
        nameList.add("DISTRIBUIDOR 2");
        nameList.add("DISTRIBUIDOR 3");
        nameList.add("DISTRIBUIDOR 4");
        return  nameList;
    }

    private ArrayList<Integer> getList()
    {
        idImages = new ArrayList<>();
        idImages.add(R.mipmap.ic_distribuidor);
        idImages.add(R.mipmap.ic_distribuidor);
        idImages.add(R.mipmap.ic_distribuidor);
        idImages.add(R.mipmap.ic_distribuidor);
        return idImages;
    }
}

Imagem.java

package com.pedido.meu.telas_meu_pedido.modelo;

public class Imagem
{
    private int imageId;
    private String imageName;

    public int getImageId() {
        return imageId;
    }

    public void setImageId(int imageId) {
        this.imageId = imageId;
    }

    public String getImageName() {
        return imageName;
    }

    public void setImageName(String imageName) {
        this.imageName = imageName;
    }
}

ItemAdapter.java

package com.pedido.meu.telas_meu_pedido.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.pedido.meu.telas_meu_pedido.controller.ListaProdutosActivity;
import com.pedido.meu.telas_meu_pedido.R;

import java.util.ArrayList;

public class ItemAdapter extends BaseAdapter {
    private Context context;
    private ArrayList<Integer> listaId;
    private ArrayList<String> listaNome;
    private ArrayList<Integer> listaStar;

    public ItemAdapter(Context context, ArrayList<Integer> listaId, ArrayList<String> listaNome, ArrayList<Integer> listaStar) {
        this.context = context;
        this.listaId = listaId;
        this.listaNome = listaNome;
        this.listaStar = listaStar;
    }

    public ItemAdapter(ListaProdutosActivity listaProdutosActivity, ArrayList<Integer> idImages, ArrayList<String> nameList) {
        this.context = listaProdutosActivity;
        this.listaId = idImages;
        this.listaNome = nameList;
    }

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

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        int view = 0;

        if (convertView == null)
        {
            if(view == R.layout.list_item_produtos)
            {
                convertView = View.inflate(context, R.layout.list_item_produtos, null);
                ImageView imgProducts = convertView.findViewById(R.id.imgListItemProduto);
                TextView textViewProducts = convertView.findViewById(R.id.txtListItemProduto);
                imgProducts.setImageResource(listaId.get(position));
                textViewProducts.setText(listaNome.get(position));
            }
            else
            {
                convertView = View.inflate(context, R.layout.list_item_distribuidor, null);
                ImageView imgDistributor = convertView.findViewById(R.id.imgStarDistributor);
                TextView textDistribuidor = convertView.findViewById(R.id.txtDistribuidor);
                imgDistributor.setImageResource(listaStar.get(0));
                textDistribuidor.setText(listaNome.get(position));
            }
        }
        return convertView;
    }
}

activity_lista_distribuidores.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_height="match_parent"
    android:layout_width="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.pedido.meu.telas_meu_pedido.controller.ListaDistribuidoresActivity"
    >

    <ImageView
        android:id="@+id/imgTitleListDistributor"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@mipmap/ic_distribuidor"
        android:layout_gravity="center"
        android:layout_marginTop="30dp"
        android:contentDescription="@string/txtTitleListaDistribuidores"
        />

    <TextView
        android:id="@+id/txtTitleDistributor"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/txtTitleListaDistribuidores"
        android:gravity="center"
        android:textColor="@color/colorPrimary"
        android:textStyle="bold"
        android:textSize="14pt"
        android:layout_marginTop="15dp"
        android:layout_marginBottom="20dp"
        />

    <Spinner
        android:id="@+id/spinnerCategoria"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginBottom="15dp"
        />

    <ListView
        android:id="@+id/txtListViewDistribuidores"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </ListView>

</LinearLayout>

list_item_distribuidor.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:android="http://schemas.android.com/apk/res/android"
    >

    <ImageView
        android:id="@+id/imgListaDistribuidor"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:background="@mipmap/ic_distribuidor"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        />

    <TextView
        android:id="@+id/txtDistribuidor"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginStart="122dp"
        android:layout_marginLeft="122dp"
        android:layout_marginRight="-150dp"
        android:textStyle="bold"/>

    <ImageView
        android:id="@+id/imgStarDistributor"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:background="@mipmap/ic_star_round"
        android:layout_marginLeft="295dp"
        android:layout_marginTop="35dp"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="4.2"
        android:layout_marginLeft="320dp"
        android:layout_marginTop="35dp"
        android:textStyle="bold"
        />

</RelativeLayout>