mardi 22 décembre 2015

Loop through a dynamically created ListView

I am dynamically filling up a ListView with data from a locally stored SQLite database (This database already has existing data and I add more to it via a static HashMap from another Activity and update the database before I use it to fill up the ListView). I setup the ListView to have a TextField and an EditText via a custom class(used as an adapter) on each row which is instantiated and used in the Activity where the ListView is displayed.

Listview looks like this: Name1 is the TextField and 100 is the EditText

name1 100

name2 200

name3 300

I am able to display the information as intended and able to Edit the EditTexts. But upon editing, how do I loop through the newly edited ListView content and possibly store back in a HashMap (TextField, EditText) or 2 ArrayLists when I click a button? I want to clear out my SQLite database and fill it back with the updated information.

Since it is dynamically created, I am not sure how to reference the new data after editing it.

//CustomAdapter Class
public class CustomAdapter extends BaseAdapter implements ListAdapter {
    private ArrayList<String> list = new ArrayList<>();
    private ArrayList<String> numList = new ArrayList<>();
    private Context context;

    public CustomAdapter(ArrayList<String> list, Context context, ArrayList<String> numList){
        this.list = list;
        this.context = context;
        this.numList = numList;
    }

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

    @Override
    public Object getItem(int pos) {
        return list.get(pos);
    }

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

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View view = convertView;
        if (view == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.custom_order_summary_edit_layout, null);
        }

        TextView listItemText = (TextView)view.findViewById(R.id.list_edit_item_string);
        listItemText.setText(list.get(position));

        final EditText etEditOrderSummary = (EditText)view.findViewById(R.id.etEditOrderSummary);
        etEditOrderSummary.setText(numList.get(position));

        return view;
    }
}



//Order Activity
ListView summaryEditListView;
ArrayList<String> orderLists, numLists;
CustomAdapter customAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_order_summary_edit);

    summaryEditListView = (ListView)findViewById(R.id.summaryEditListView);
    orderLists = new ArrayList<>();
    numLists = new ArrayList<>();
    databaseSetup();
}

void databaseSetup(){

        try {
            SQLiteDatabase orderDb = OrderSummaryEditActivity.this.openOrCreateDatabase("myorder", MODE_PRIVATE, null);
            orderDb.execSQL("CREATE TABLE IF NOT EXISTS orders(name VARCHAR(50), quantity INT(4))");
            //This is static HashMap from another Activity adding more data.  
            if(CustomOrderListAdapter.orders != null) {
                for (Map.Entry<String, String> x : CustomOrderListAdapter.orders.entrySet()) {
                    orderDb.execSQL("INSERT INTO orders (name, quantity) VALUES" +
                            "('" + x.getKey() + "', " + Integer.parseInt(x.getValue()) + ")");
                }
            }

            Cursor c = orderDb.rawQuery("SELECT * FROM orders", null);
            int nameIndex = c.getColumnIndex("name");
            int quantityIndex = c.getColumnIndex("quantity");

            while (c.moveToNext()){
                orderLists.add(c.getString(nameIndex));
                numLists.add(Integer.toString(c.getInt(quantityIndex)));
            }
            customAdapter = new CustomAdapter(orderLists, OrderActivity.this, numLists);
            summaryEditListView.setAdapter(customAdapter);
            c.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

Aucun commentaire:

Enregistrer un commentaire