问题描述
我正在创建一个ListView,其中将显示ArrayList(全局声明)的内容。我在这里使用自定义布局。一切看起来不错,但ListView并未显示在输出屏幕上。而在其他项目上使用的相同编码可提供所需的输出。请告诉我代码是否有问题或其他问题。
Globalvar.java //声明ArrayList的位置
package com.example.tale;
import java.util.ArrayList;
import java.util.List;
public class Globalvar
{
public static int value=0;
public static List<String> list = new ArrayList<String>();
}
activity_cart2.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".cart2">
<ListView
android:id="@+id/lvl"
android:layout_width="match_parent"
android:layout_height="404dp">
</ListView>
cart2.java
package com.example.tale;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
public class cart2 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cart2);
Globalvar.list.add("Example 1");
ListView listView = (ListView)findViewById(R.id.lvl);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.forcart2,Globalvar.list);
listView.setAdapter(adapter);
}
}
forcart2.xml //自定义布局
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/lable"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="40sp">
</TextView>
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)