Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Item.java
- ---------
- package com.example.mohamadpc.listviewshopping;
- /**
- * Created by MOHAMADPC on 10/10/2017.
- */
- public class Item {
- private String name;
- private int quentity;
- public Item(String name, int quentity) {
- this.name = name;
- this.quentity = quentity;
- }
- public Item(String name) {
- this.name = name;
- this.quentity = 1;
- }
- public int getQuentity() {
- return quentity;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public void setQuentity(int quentity) {
- this.quentity = quentity;
- }
- }
- ItemAdapter.java
- ----------------
- package com.example.mohamadpc.listviewshopping;
- import android.content.Context;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.BaseAdapter;
- import android.widget.TextView;
- import java.util.List;
- import static android.icu.lang.UCharacter.GraphemeClusterBreak.V;
- /**
- * Created by MOHAMADPC on 10/10/2017.
- */
- public class ItemAdapter extends BaseAdapter {
- private Context context;
- private List<Item> itemsList;
- public ItemAdapter(List<Item> itemsList, Context context) {
- this.itemsList = itemsList;
- this.context = context;
- }
- @Override
- public int getCount() {
- return this.itemsList.size();
- }
- @Override
- public Object getItem(int position) {
- return itemsList.get(position);
- }
- @Override
- public long getItemId(int position) {
- return position;
- }
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- View myView = LayoutInflater.from(context).inflate(R.layout.list_row_layout, null);
- Item item = itemsList.get(position);
- ((TextView) myView.findViewById(R.id.tvItemName)).setText(item.getName());
- ((TextView) myView.findViewById(R.id.tvQuentity)).setText(item.getQuentity() + "");
- return myView;
- }
- }
- MainActivity.java
- -----------------
- package com.example.mohamadpc.listviewshopping;
- import android.app.AlertDialog;
- import android.content.ClipData;
- import android.content.Context;
- import android.content.DialogInterface;
- import android.content.SharedPreferences;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.widget.AdapterView;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.ListView;
- import android.widget.Toast;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Set;
- import static android.os.Build.VERSION_CODES.M;
- public class MainActivity extends AppCompatActivity implements View.OnClickListener {
- private EditText etNewItem;
- private Button btnAddItem;
- private ListView lsvItems;
- List<Item> lst;
- Context context;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- connectToLayout();
- }
- private void connectToLayout() {
- this.context = this;
- etNewItem = (EditText) findViewById(R.id.etNewItem);
- btnAddItem = (Button) findViewById(R.id.btnAddItem);
- lsvItems = (ListView) findViewById(R.id.lsvItems);
- btnAddItem.setOnClickListener(this);
- lst = loadFromFile();
- ItemAdapter myAdapter = new ItemAdapter(lst, this);
- lsvItems.setAdapter(myAdapter);
- lsvItems.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
- @Override
- public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
- final AlertDialog.Builder myDialog = new AlertDialog.Builder(context);
- final View myView = LayoutInflater.from(context).inflate(R.layout.update_dialog, null);
- myDialog.setView(myView);
- myDialog.setTitle("Update Quentity");
- myDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- String inputData = ((EditText) myView.findViewById(R.id.etNewQuentity)).getText().toString();
- if (!inputData.isEmpty()) {
- int newQ = Integer.parseInt(inputData);
- String itemName = lst.get(position).getName();
- // save to file
- SharedPreferences myfile = context.getSharedPreferences("MyShoppingList", Context.MODE_PRIVATE);
- if (newQ > 0) {
- myfile.edit().putInt(itemName, newQ).commit();
- } else {
- myfile.edit().remove(itemName).commit();
- }
- // update list
- lst = loadFromFile();
- lsvItems.setAdapter(new ItemAdapter(lst, context));
- dialog.cancel();
- }
- }
- });
- myDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- dialog.dismiss();
- }
- });
- myDialog.show();
- return true;
- }
- });
- }
- private List<Item> loadFromFile() {
- SharedPreferences myfile = this.getSharedPreferences("MyShoppingList", Context.MODE_PRIVATE);
- Object[] keys = myfile.getAll().keySet().toArray();
- List<Item> lst = new ArrayList<>();
- for (int i = 0; i < keys.length; i += 1) {
- String name = (String) keys[i];
- lst.add(new Item(name, myfile.getInt(name, 0)));
- }
- return lst;
- }
- @Override
- public void onClick(View v) {
- switch (v.getId()) {
- case R.id.btnAddItem:
- String newItemName = etNewItem.getText().toString();
- if (!newItemName.isEmpty()) {
- // add the item
- SharedPreferences myfile = this.getSharedPreferences("MyShoppingList", Context.MODE_PRIVATE);
- int oldQ = myfile.getInt(newItemName, -1);
- if (oldQ == -1) {
- myfile.edit().putInt(newItemName, 1).commit();
- } else {
- myfile.edit().putInt(newItemName, oldQ + 1).commit();
- }
- // update the list
- lst = loadFromFile();
- lsvItems.setAdapter(new ItemAdapter(lst, this));
- etNewItem.setText("");
- }
- break;
- default:
- Toast.makeText(this, "Error handling a click event", Toast.LENGTH_LONG).show();
- break;
- }
- }
- }
- activity_main.xml
- -----------------
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical">
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal">
- <EditText
- android:id="@+id/etNewItem"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:textSize="20sp" />
- <Button
- android:id="@+id/btnAddItem"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_weight="2"
- android:text="Add"
- android:textSize="20sp" />
- </LinearLayout>
- <ListView
- android:id="@+id/lsvItems"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:choiceMode="singleChoice"></ListView>
- </LinearLayout>
- list_row_layout.xml
- -------------------
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal">
- <TextView
- android:id="@+id/tvItemName"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:textSize="18sp" />
- <TextView
- android:id="@+id/tvQuentity"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:textSize="18sp" />
- </LinearLayout>
- update_dialog.xml
- -----------------
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical">
- <EditText
- android:id="@+id/etNewQuentity"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:hint="Enter quentity ..."
- android:inputType="number"
- android:textSize="20sp" />
- </LinearLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement