3

I have simple list which suppose to display all the rows from DB table. However, it shows only one row in the list view.

Though, it receives all the rows from the DB (verified the size of configArrayList), but displays only the first row and discards others.

Here is the Activity and Custom Adapter.

Main Activity :

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);
}

Here is the Custom 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>
Biswajit
  • 415
  • 4
  • 13
  • Could you please share your xml which have listview? – Beena Feb 23 '16 at 11:40
  • I know that you are using the size to make sure that you have the data, but could you print out what do you have in the configMenu when you pass it to the list to be 100% sure that the problem is not there? – Spirrow Feb 23 '16 at 11:54
  • @Spirrow Ideally ConfigMenu should have the same data. However, as per your suggestion, I have verified the data in that list. Everything looks fine. (It contains the list of Objects of ConfigEntity and each ConfigEntity object holds the data fetched from each row from DB). – Biswajit Feb 23 '16 at 12:03
  • Is there a reason for putting the ListView inside a ScrollView ? I think this is bad practice. What does getCount() return in your ConfigListAdapter ? – Jason Saruulo Feb 23 '16 at 12:09
  • @JasonSaruulo I understand your concern and I am aware of this. But I want the Button which is in the top of the page, should scroll with the list contents. If I put the list out of the ScrollView, it will split the page into two parts, top one containing only the Button and the bottom one with the listView. So only the list will scroll and the button will be static, which is not expected. Any better way to implement this, is welcomed though. – Biswajit Feb 23 '16 at 13:14
  • I see ... did you try the listview.addHeader(...) approach ? What about the getCount() question ? What does it return ? Can you also provide a screenshot of your layout ? Maybe there is something wrong with the height definitions (always wrap_content, maybe match_parent would be better) ? – Jason Saruulo Feb 24 '16 at 17:00
  • @JasonSaruulo I guess, I found a solution, though not sure, why it was behaving awkward. By specifying the list height dynamically, it started showing the entire list. Here is the code for setting dymanic height of list. [Question : 17693578](http://stackoverflow.com/questions/17693578/android-how-to-display-2-listviews-in-one-activity-one-after-the-other) – Biswajit Feb 25 '16 at 10:25

1 Answers1

0

Please try this. Change getView Method as follows

@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;
}

and Declare View view; as class level variable; Hope it helps Thanks. Refer here :-https://androidruler.wordpress.com/2016/02/21/android-custom-listview-example/

Jagjit Singh
  • 1,909
  • 1
  • 14
  • 19