Android-如何使用自定义ListAdapter创建自定义ListView

Android-如何使用自定义ListAdapter创建自定义ListView,android,listview,listadapter,Android,Listview,Listadapter,我的问题是:我想根据从服务器收到的输入创建一个按钮列表(或可点击的文本视图,无所谓)。 基本上,服务器会告诉我可以向用户显示哪些选项,我会使用自定义onClickListeners在列表视图中的不同列表项中显示每个选项文本(onClickListeners需要做同样的事情,所以我可以在自定义项模型中声明一个侦听器,我想是吧?)。这要做几次,所以在用户选择一个选项后,我需要多次删除和创建列表项。 我已经在互联网上搜索并找到了解决方案,但我太困惑了,无法实现它,所以我会在这里要求确认,而不是浪费无谓

我的问题是:我想根据从服务器收到的输入创建一个按钮列表(或可点击的文本视图,无所谓)。 基本上,服务器会告诉我可以向用户显示哪些选项,我会使用自定义onClickListeners在列表视图中的不同列表项中显示每个选项文本(onClickListeners需要做同样的事情,所以我可以在自定义项模型中声明一个侦听器,我想是吧?)。这要做几次,所以在用户选择一个选项后,我需要多次删除和创建列表项。 我已经在互联网上搜索并找到了解决方案,但我太困惑了,无法实现它,所以我会在这里要求确认,而不是浪费无谓的时间试图找出什么是正确的

首先,我需要一个ListView,我在prediction_fragment.xml中创建了它,它包含另一个用于显示用户选择历史的textview:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    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">

    <TextView
        android:id="@+id/textView_prediction_history"
        android:layout_width="match_parent"
        android:layout_height="344dp"
        android:gravity="center"
        android:scrollbars="vertical" />

    <ListView
        android:layout_width="match_parent"
        android:layout_height="461dp"
        android:id="@+id/listview_prediction"
        android:layout_below="@+id/textView_prediction_history"/>

</RelativeLayout>
麻烦来了,我设置了我的自定义ListAdapter,但我不知道在getView方法中的findViewById调用中放置哪些项ID。我想这就是我所缺少的,不管怎样,这是代码:

public class MyListAdapter extends BaseAdapter{
    View renderer;
    List<MyItemModel> items;

    public MyListAdapter(View renderer) {
        this.renderer = renderer;
    }

    public void setModel(List<MyItemModel> items){
        this.items = items;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return items!=null?items.size():0;
    }
    @Override
    public Object getItem(int position) {
        return items!=null?items.get(position):null;
    }
    @Override
    public long getItemId(int position) {
        return items!=null?items.get(position).id:-1;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(convertView==null){
            convertView = renderer;
        }
        MyItemModel item = items.get(position);
        TextView label = (TextView)convertView.findViewById(R.id.??????);
        label.setText(item.description);
        Button button = (Button)convertView.findViewById(R.id.???????);
        button.setOnClickListener(item.listener);
        return convertView;
    }
}
公共类MyListAdapter扩展BaseAdapter{
视图渲染器;
清单项目;
公共MyListAdapter(视图渲染器){
this.renderer=渲染器;
}
公共void集合模型(列表项){
这个项目=项目;
notifyDataSetChanged();
}
@凌驾
public int getCount(){
返回项!=null?项。大小():0;
}
@凌驾
公共对象getItem(int位置){
返回项目!=null?项目。获取(位置):null;
}
@凌驾
公共长getItemId(int位置){
返回项目!=null?项目。获取(位置)。id:-1;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
if(convertView==null){
convertView=渲染器;
}
MyItemModel item=items.get(位置);
TextView标签=(TextView)convertView.findViewById(R.id.);
label.setText(项目说明);
按钮按钮=(按钮)convertView.findViewById(R.id.);
button.setOnClickListener(item.listener);
返回视图;
}
}
然后更新活动中的列表项,在这里我不知道在MyListAdapter构造函数调用中写入什么项ID以及如何正确更新列表:

List<MyItemModel> myListModel = new ArrayList<MyItemModel>();
ListAdapter adapter = new MyListAdapter(getLayoutInflater().inflate(R.layout.??????, this));
boolean choicesAvailable= true;

while(choicesAvailable){
   //fill myListModel
   adapter.setModel(myListModel)
   myListModel.clear();
   //server waits for item click
   choicesAvailable= (String) in.readObject(); //server received item selection and detects if there are
   //more choices available or the process is over
}
List myListModel=new ArrayList();
ListAdapter=新的MyListAdapter(GetLayoutFlater().充气(R.layout,this));
boolean choicesAvailable=true;
while(可选){
//填充模型
adapter.setModel(myListModel)
myListModel.clear();
//服务器等待项目单击
choicesAvailable=(String)in.readObject();//服务器接收到项目选择并检测是否存在
//更多的选择可用,或者过程结束
}
此外,我想设置列表可滚动,默认情况下是否可滚动?这是因为选项数量可以从2到10不等。。。 提前非常感谢您抽出时间,非常感谢

List<MyItemModel> myListModel = new ArrayList<MyItemModel>();
ListAdapter adapter = new MyListAdapter(getLayoutInflater().inflate(R.layout.??????, this));
boolean choicesAvailable= true;

while(choicesAvailable){
   //fill myListModel
   adapter.setModel(myListModel)
   myListModel.clear();
   //server waits for item click
   choicesAvailable= (String) in.readObject(); //server received item selection and detects if there are
   //more choices available or the process is over
}