我想自定义List视图的每一行是Button,里面有文本视图。
但它没有用!
它只显示我在第一个布局中创建的按钮。
我可以做些什么来修复?非常感谢
这是我的代码:
public class MainActivity extends AppCompatActivity {
Button button;
ListView lv ;
ListViewAdapter listViewAdapter;
ArrayList<String> al ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
lv = (ListView)findViewById(R.id.lstView);
button = (Button)findViewById(R.id.button);
al = new ArrayList();
al.add("cho");
al.add("phuong");
al.add("hello");
al.add("bye");
al.add("love");
listViewAdapter= new ListViewAdapter(this , R.layout.button , al);
lv.setAdapter(listViewAdapter);
这是适配器:
public class ListViewAdapter extends ArrayAdapter {
/**
* Constructor
*
* @param context The current context.
* @param resource The resource ID for a layout file containing a TextView to use when
*/
Activity context;
int layResID;
ArrayList<String> object;
public ListViewAdapter(Activity context, int resource , ArrayList<String> object) {
super(context, resource);
this.context=context;
this.layResID = resource;
this.object = object;
}
@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView==null){
LayoutInflater inflater = context.getLayoutInflater();
convertView = inflater.inflate(layResID, null);
}
Button btn = (Button)convertView.findViewById(R.id.view);
String string = object.get(position);
btn.setText(string);
return convertView;
}
}
这是主要的xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.macbook.note.MainActivity"
android:background="#1A237E"
android:orientation="vertical"
android:weightSum="10"
>
<RelativeLayout
android:layout_weight="2"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_marginTop="30dp"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:textSize="10dp"
android:background="@drawable/button"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:id="@+id/button" />
</RelativeLayout>
<LinearLayout
android:orientation="vertical"
android:layout_weight="9"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<ListView
android:id="@+id/lstView"
android:divider="#1A237E"
android:dividerHeight="20dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</ScrollView>
</LinearLayout>
</LinearLayout>
这是自定义布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/view"
android:layout_width="300dp"
android:layout_height="100dp"
android:background="@drawable/btn_list"
android:textSize="10dp"/>
</LinearLayout>
答案 0 :(得分:0)
将ArrayAdapter的超级调用更改为以下
super(context, object);
答案 1 :(得分:0)
我想问题是你没有指定应该在列表视图中创建的项目的当前计数。你有两个选择:
1.调用ArrayAdapter的其他构造函数,以便ArrayAdapter类为您处理它:
public ListViewAdapter(Activity context, int resource , ArrayList<String> object) {
super(context, resource, object);
}
2.在适配器类中覆盖ArrayAdapater.getCount():
@Override
public int getCount() {
return this.object.size();
}