Search code examples
androidlistviewandroid-arrayadapterandroid-adapter

Save adapter array when new item added to Listview


I just started learn Android. I'm not sure if my question is correct or not. My purpose is when user press on button add item to listview. Each single item (row) have 3 inputs. Currently my code adds item (row) when press on button. But its creating new array and deleting all user inputs. I also almost don't know Java (have very very little OOP skill). Can you answer what am I doing wrong?

Activity:

    package com.example.gereltod.test7;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListView;

import java.util.ArrayList;

public class SellActivity extends Activity {

    ArrayList<Items> items = new ArrayList<Items>();
    ItemsAdapter adapter;
    ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sell);

        adapter = new ItemsAdapter(this, items);
        listView = (ListView) findViewById(R.id.listView_main);
        listView.setAdapter(adapter);
    }

    public void add_item(View view) {

        Log.i("clicked add item", "yes");

        items.add(new Items("", "", ""));
        adapter.notifyDataSetChanged();
    }
}

Layout

<ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/listView_main"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_above="@+id/btn_add_item" />


    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/btn_add_item"
        android:id="@+id/btn_add_item"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:onClick="add_item" />

Items Adapter

package com.example.gereltod.test7;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class ItemsAdapter extends ArrayAdapter<Items> {

    private static class ViewHolder {
        TextView name;
        TextView qty;
        TextView price;
    }

    public ItemsAdapter(Context context, ArrayList<Items> items) {
        super(context, 0, items);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // Get the data item for this position
        Items items = getItem(position);
        // Check if an existing view is being reused, otherwise inflate the view
        ViewHolder viewHolder; // view lookup cache stored in tag
        if (convertView == null) {
//            convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);
            viewHolder = new ViewHolder();
            LayoutInflater inflater = LayoutInflater.from(getContext());
            convertView = inflater.inflate(R.layout.list_item, parent, false);
            viewHolder.name = (TextView) convertView.findViewById(R.id.edittxt_name);
            viewHolder.qty = (TextView) convertView.findViewById(R.id.edittxt_qty);
            viewHolder.price = (TextView) convertView.findViewById(R.id.edittxt_price);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.name.setText(items.name);
        viewHolder.qty.setText(items.qty);
        viewHolder.price.setText(items.price);
        return convertView;
    }
}

List item

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:layout_weight="1">

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp">

        <EditText
            android:id="@+id/edittxt_name"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.5"
            android:background="@drawable/input_shape"
            android:hint="@string/hint_item_name"
            android:padding="5dp"
            android:singleLine="true" />

        <EditText
            android:id="@+id/edittxt_qty"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_weight="0.2"
            android:background="@drawable/input_shape"
            android:gravity="right"
            android:hint="@string/hint_item_qty"
            android:inputType="number"
            android:padding="5dp"
            android:singleLine="true" />

        <EditText
            android:id="@+id/edittxt_price"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_weight="0.3"
            android:background="@drawable/input_shape"
            android:gravity="end"
            android:hint="@string/hint_item_price"
            android:inputType="number"
            android:padding="5dp"
            android:singleLine="true" />
    </TableRow>
</TableLayout>

Edit: Since my English sucks, I can't exactly describe whats going on. So I added screenshot. See here: https://i.sstatic.net/YNQlQ.jpg


Solution

  • You are creating every time a new list and adding an item to the list and then setting the adapter of single list to listview.

    Set the adapter on create of activity and then add items to adapter and call notifyDataSetChanged() method. Here is the code for activity:

    public class SellActivity extends Activity {
    
       ArrayList<Items> items = new ArrayList<Items>();
    
       private ItemsAdapter adapter;
    
       @Override
       protected void onCreate(Bundle savedInstanceState) {
    
           super.onCreate(savedInstanceState);
           setContentView(R.layout.activity_sell);
    
           ListView listView = (ListView) findViewById(R.id.listView_main);
    
           adapter = new ItemsAdapter(this);
           listView.setAdapter(adapter);
       }
    
       public void add_item(View view) {
    
           Log.i("clicked add item", "yes");
    
           adapter.add(new Items("", "", ""));
    
           adapter.notifyDataSetChanged();
       }
    }