【问题标题】:ListView is giving me a blank pageListView 给了我一个空白页
【发布时间】:2021-10-08 22:29:01
【问题描述】:

我试图让 ListView 显示帐户列表,但它返回一个空白页。我也没有收到任何错误消息。我四处寻找类似的问题,但没有一个解决方案可以解决这个问题。如果有人可以提供帮助,不胜感激。

文件 > ListActivity.java

public class ListeActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_liste);

        Guichet guichet = new Guichet();
        ArrayList<Epargne> epargneList = new ArrayList<Epargne>(guichet.listEpargne);
        
        EpargneAdapter adapter = new EpargneAdapter(ListeActivity.this, R.layout.liste_comptes, epargneList);
        final ListView list = (ListView) findViewById(R.id.maListe);

        list.setAdapter(adapter);

    }
}

文件 > EpargneAdapter.java


    private ArrayList<Epargne> epargneList;
    private Context context;
    private int viewRes;
    private Resources res;

    public EpargneAdapter(Context context, int textViewResourceId, ArrayList<Epargne> versions) {
        super(context, textViewResourceId, versions);
        this.epargneList = versions;
        this.context = context;
        this.viewRes = textViewResourceId;
        this.res = context.getResources();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        if (view == null) {
            LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.
                    LAYOUT_INFLATER_SERVICE);
            view = layoutInflater.inflate(viewRes, parent, false);
        }
        final Epargne epargne = epargneList.get(position);

        if (epargne != null) {
            final TextView numero = (TextView) view.findViewById(R.id.numeroCompte);
            final TextView solde = (TextView)view.findViewById(R.id.soldeCompte);

            String numeroC = res.getString(R.string.numero) + " " + epargne.getNumeroCompte();
            numero.setText(numeroC);
            String SoldeC =res.getString(R.string.solde) + " " + epargne.getSoldeCompte();
            solde.setText(SoldeC);
        }
        return view;
    }
}

文件 > activity_liste.xml

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


    <ListView
        android:id="@+id/maListe"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="6dp"
        />
    
</LinearLayout>

文件 > liste_Compte.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="6dp">

    <ImageView android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="6dp"
        android:src="@mipmap/ic_launcher" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
        <TextView android:id="@+id/numeroCompte"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_vertical" />
        <TextView
            android:id="@+id/soldeCompte"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </LinearLayout>

</LinearLayout>

文件 > Guichet.java

public class Guichet extends AppCompatActivity {

    ArrayList<Epargne> listEpargne = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_principal);
        setTitle("Guichet automatique ATM");

        initListEpargne(listEpargne);
    }


    public void initListEpargne(ArrayList<Epargne> listEpargne) {
        Epargne epargne1 = new Epargne();
        epargne1.setNumeroCompte("E0001");
        epargne1.setSoldeCompte(10000);
        listEpargne.add(epargne1);

        Epargne epargne2 = new Epargne();
        epargne2.setNumeroCompte("E0002");
        epargne2.setSoldeCompte(10000);
        listEpargne.add(epargne2);

        Epargne epargne3 = new Epargne();
        epargne3.setNumeroCompte("E0003");
        epargne3.setSoldeCompte(10000);
        listEpargne.add(epargne3);

        Epargne epargne4 = new Epargne();
        epargne4.setNumeroCompte("E0004");
        epargne4.setSoldeCompte(10000);
        listEpargne.add(epargne4);
    }
}

非常感谢!

【问题讨论】:

  • 你能展示一下你的Guichet 类的样子吗?并确保guichet.listEpargne 中已经有数据。
  • 检查guichet.listEpargne的大小
  • 我添加了 Guichet.java 文件
  • 你是对的。 guichet.listEpargne 的大小 = 0。我修复了它。谢谢!!!!

标签: android android-listview


【解决方案1】:

当您调用 listEpargne 时,您并未对其进行初始化。

但是让我们忘记它。您创建一个扩展 AppCompatActivity 的类,并尝试使用 new 关键字 :o 创建该类。

AppCompatActivity 扩展的类应该只按意图创建而不是启动。

您可以创建一个名为 Guichet 的类,该类不扩展 AppCompatActivity,并提供一个方法 getData()。此方法将返回该列表。但不要忘记初始化并填写该列表:)。

【讨论】:

  • 谢谢伊曼纽尔。我发起了listEpargne,现在它工作正常。
猜你喜欢
  • 2016-08-07
  • 2020-04-03
  • 2022-06-15
  • 2014-04-02
  • 1970-01-01
  • 2021-02-10
  • 2015-11-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多