【发布时间】: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。我修复了它。谢谢!!!!