ListView仅显示One Row

时间:2016-02-23 10:45:16

标签: android listview

我有一个简单的列表,它假设显示DB表中的所有行。但是,它在列表视图中只显示一行。

但是,它接收来自DB的所有行(验证了configArrayList的大小),但只显示第一行并丢弃其他行。

这是活动和自定义适配器。

主要活动:

ArrayList<HashMap<String, String>> configArrayList = dbTools.getAllConfig(); // Returns 4 rows
ArrayList<ConfigEntity> configMenu = new ArrayList<>();

if (!(configArrayList.isEmpty()))
{
    Iterator<HashMap<String, String>> iterator = configArrayList.iterator();

    while (iterator.hasNext())
    {
        HashMap<String, String> configMap = iterator.next();

        // ConfigEntity contains two members Name, ID and their setter, getter
        ConfigEntity entity = new ConfigEntity();

        entity.setID(configMap.get("ID"));
        entity.setName(String.valueOf(configArrayList.size()));
        // Set Name as Size to verify the list size is grater than 1. (Size is 4)

        configMenu.add(entity);
    }
}

if (!(configMenu.isEmpty()))
{
    ListAdapter adapter = new ConfigListAdapter(this, configMenu);
    ListView theListView = (ListView) findViewById(R.id.config_list_listView);
    theListView.setAdapter(adapter);
}

以下是自定义适配器:

    public class ConfigListAdapter extends BaseAdapter {

    private ArrayList<ConfigEntity> entityList;
    private LayoutInflater layoutInflater;

    public ConfigListAdapter(Context context, ArrayList<ConfigEntity> entityList) {
        this.entityList = entityList;
        layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return entityList.size();
    }

    @Override
    public Object getItem(int position) {
        return entityList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder;

        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.config_list_row_layout, null);
            holder = new ViewHolder();
            holder.nameTextView = (TextView) convertView.findViewById(R.id.config_list_row_textView);
            holder.idTextView = (TextView) convertView.findViewById(R.id.config_list_row_id);
            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        ConfigEntity entity = entityList.get(position);

        holder.nameTextView.setText(entity.getName());
        holder.idTextView.setText(entity.getID());

        return convertView;
    }

    static class ViewHolder {

        TextView nameTextView;
        TextView idTextView;
    }
}

main_activity.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">


        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Add New"
            android:id="@+id/config_list_addConfig_button"
            android:layout_gravity="right"
            android:onClick="addNewConfig" />

        <ListView
            android:id="@+id/config_list_listView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:dividerHeight="5dp">
        </ListView>

    </LinearLayout>
</ScrollView>

config_list_listView.xml

<?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"
    android:layout_marginLeft="@dimen/margin_20dp">
    android:layout_marginRight="@dimen/margin_20dp">


    <TextView
        android:id="@+id/config_list_row_textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="14sp"
        android:layout_marginTop="@dimen/margin_10dp"
        android:layout_marginLeft="@dimen/margin_20dp"
        android:longClickable="true" />

    <TextView
        android:id="@+id/config_list_row_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="gone" />
</LinearLayout>

2 个答案:

答案 0 :(得分:1)

也许我发现了你的问题,尝试删除ScrollView中的listView表单。你不应该将一个ListView放在ScrollView中,因为ListView类实现了自己的滚动,它只是不接收手势,因为它们都是由父ScrollView处理的。

如果您想在列表顶部显示内容,请使用标题,这就像使用listView.addHeader(yourlayour)一样简单,请查看此post以确定您的问题。

答案 1 :(得分:0)

请试试这个。 更改getView方法如下

@Override
public View getView(int position, View convertView, ViewGroup parent) {
     view=convertView;
    ViewHolder holder;

    if (convertView == null) {
        view = layoutInflater.inflate(R.layout.config_list_row_layout, null);
        holder = new ViewHolder();
        holder.nameTextView = (TextView) view.findViewById(R.id.config_list_row_textView);
        holder.idTextView = (TextView) view.findViewById(R.id.config_list_row_id);
        view.setTag(holder);

    } else {
        holder = (ViewHolder) view.getTag();
    }

    ConfigEntity entity = entityList.get(position);

    holder.nameTextView.setText(entity.getName());
    holder.idTextView.setText(entity.getID());

    return view;
}

和Declare View视图;作为班级变量; 希望它有助于谢谢。请参阅此处: - https://androidruler.wordpress.com/2016/02/21/android-custom-listview-example/