mirror of
https://github.com/ClaytonWWilson/Listify.git
synced 2025-12-16 18:48:48 +00:00
Can now remove items from list
This commit is contained in:
parent
94e636f1d5
commit
dcea8aa58e
@ -21,10 +21,11 @@ import androidx.appcompat.app.AppCompatActivity;
|
|||||||
|
|
||||||
public class List extends AppCompatActivity {
|
public class List extends AppCompatActivity {
|
||||||
ListView listView;
|
ListView listView;
|
||||||
String listName = "Sample List";
|
MyAdapter myAdapter;
|
||||||
|
|
||||||
Button incrQuan;
|
Button incrQuan;
|
||||||
Button decrQuan;
|
Button decrQuan;
|
||||||
|
Button removeItem;
|
||||||
|
|
||||||
ArrayList<String> pNames = new ArrayList<>(); //String[] pNames = {"Half-gallon organic whole milk"};
|
ArrayList<String> pNames = new ArrayList<>(); //String[] pNames = {"Half-gallon organic whole milk"};
|
||||||
ArrayList<String> pStores = new ArrayList<>(); //String[] pStores = {"Kroger"};
|
ArrayList<String> pStores = new ArrayList<>(); //String[] pStores = {"Kroger"};
|
||||||
@ -40,11 +41,25 @@ public class List extends AppCompatActivity {
|
|||||||
pQuantity.add("1");
|
pQuantity.add("1");
|
||||||
pImages.add(R.drawable.milk);
|
pImages.add(R.drawable.milk);
|
||||||
|
|
||||||
|
pNames.add("5-bunch medium bananas");
|
||||||
|
pStores.add("Kroger");
|
||||||
|
pPrices.add("$3.00");
|
||||||
|
pQuantity.add("1");
|
||||||
|
pImages.add(R.drawable.bananas);
|
||||||
|
|
||||||
|
pNames.add("JIF 40-oz creamy peanut butter");
|
||||||
|
pStores.add("Kroger");
|
||||||
|
pPrices.add("$7.00");
|
||||||
|
pQuantity.add("1");
|
||||||
|
pImages.add(R.drawable.peanutbutter);
|
||||||
|
|
||||||
|
int currSize = pNames.size();
|
||||||
|
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
setContentView(R.layout.activity_list);
|
setContentView(R.layout.activity_list);
|
||||||
|
|
||||||
listView = findViewById(R.id.listView);
|
listView = findViewById(R.id.listView);
|
||||||
MyAdapter myAdapter = new MyAdapter(this, pNames, pStores, pPrices, pQuantity, pImages);
|
myAdapter = new MyAdapter(this, pNames, pStores, pPrices, pQuantity, pImages);
|
||||||
|
|
||||||
listView.setAdapter(myAdapter);
|
listView.setAdapter(myAdapter);
|
||||||
}
|
}
|
||||||
@ -74,37 +89,48 @@ public class List extends AppCompatActivity {
|
|||||||
View listproduct = layoutInflater.inflate(R.layout.listproduct, parent,false);
|
View listproduct = layoutInflater.inflate(R.layout.listproduct, parent,false);
|
||||||
|
|
||||||
decrQuan = (Button) listproduct.findViewById(R.id.buttonDecr);
|
decrQuan = (Button) listproduct.findViewById(R.id.buttonDecr);
|
||||||
|
incrQuan = (Button) listproduct.findViewById(R.id.buttonIncr);
|
||||||
|
removeItem = (Button) listproduct.findViewById(R.id.buttonDel);
|
||||||
|
|
||||||
decrQuan.setOnClickListener(new View.OnClickListener() {
|
decrQuan.setOnClickListener(new View.OnClickListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onClick(View v) {
|
public void onClick(View v) {
|
||||||
TextView quantityText = (TextView) findViewById(R.id.quantityView);
|
int q = Integer.parseInt(pQuantity.get(position)) - 1;
|
||||||
int q = Integer.parseInt(quantityText.getText().toString()) - 1;
|
pQuantity.set(position, Integer.toString(q));
|
||||||
quantityText.setText(Integer.toString(q));
|
listView.setAdapter(myAdapter);
|
||||||
|
|
||||||
if(q <= 1) {
|
|
||||||
decrQuan.setEnabled(false);
|
|
||||||
}
|
|
||||||
if(q < 10) {
|
|
||||||
incrQuan.setEnabled(true);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
if(Integer.parseInt(pQuantity.get(position)) <= 1) {
|
||||||
decrQuan.setEnabled(false);
|
decrQuan.setEnabled(false);
|
||||||
|
}
|
||||||
|
if(Integer.parseInt(pQuantity.get(position)) < 10) {
|
||||||
|
incrQuan.setEnabled(true);
|
||||||
|
}
|
||||||
|
|
||||||
incrQuan = (Button) listproduct.findViewById(R.id.buttonIncr);
|
|
||||||
incrQuan.setOnClickListener(new View.OnClickListener() {
|
incrQuan.setOnClickListener(new View.OnClickListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onClick(View v) {
|
public void onClick(View v) {
|
||||||
TextView quantityText = (TextView) findViewById(R.id.quantityView);
|
int q = Integer.parseInt(pQuantity.get(position)) + 1;
|
||||||
int q = Integer.parseInt(quantityText.getText().toString()) + 1;
|
pQuantity.set(position, Integer.toString(q));
|
||||||
quantityText.setText(Integer.toString(q));
|
listView.setAdapter(myAdapter);
|
||||||
|
}
|
||||||
if(q > 1) {
|
});
|
||||||
|
if(Integer.parseInt(pQuantity.get(position)) > 1) {
|
||||||
decrQuan.setEnabled(true);
|
decrQuan.setEnabled(true);
|
||||||
}
|
}
|
||||||
if(q >= 10) {
|
if(Integer.parseInt(pQuantity.get(position)) >= 10) {
|
||||||
incrQuan.setEnabled(false);
|
incrQuan.setEnabled(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
removeItem.setOnClickListener(new View.OnClickListener() {
|
||||||
|
@Override
|
||||||
|
public void onClick(View v) {
|
||||||
|
pNames.remove(position);
|
||||||
|
pStores.remove(position);
|
||||||
|
pPrices.remove(position);
|
||||||
|
pQuantity.remove(position);
|
||||||
|
pImages.remove(position);
|
||||||
|
listView.setAdapter(myAdapter);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -114,11 +140,13 @@ public class List extends AppCompatActivity {
|
|||||||
TextView quantity = listproduct.findViewById(R.id.quantityView);
|
TextView quantity = listproduct.findViewById(R.id.quantityView);
|
||||||
ImageView image = listproduct.findViewById(R.id.imageView);
|
ImageView image = listproduct.findViewById(R.id.imageView);
|
||||||
|
|
||||||
|
if(!pNames.isEmpty()) {
|
||||||
name.setText(pNames.get(position));
|
name.setText(pNames.get(position));
|
||||||
store.setText(pStores.get(position));
|
store.setText(pStores.get(position));
|
||||||
price.setText(pPrices.get(position));
|
price.setText(pPrices.get(position));
|
||||||
quantity.setText(pQuantity.get(position));
|
quantity.setText(pQuantity.get(position));
|
||||||
image.setImageResource(pImages.get(position));
|
image.setImageResource(pImages.get(position));
|
||||||
|
}
|
||||||
|
|
||||||
return listproduct;
|
return listproduct;
|
||||||
}
|
}
|
||||||
|
|||||||
BIN
Listify/app/src/main/res/drawable/bananas.png
Normal file
BIN
Listify/app/src/main/res/drawable/bananas.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 24 KiB |
BIN
Listify/app/src/main/res/drawable/peanutbutter.png
Normal file
BIN
Listify/app/src/main/res/drawable/peanutbutter.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 127 KiB |
@ -4,7 +4,8 @@
|
|||||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||||
xmlns:tools="http://schemas.android.com/tools"
|
xmlns:tools="http://schemas.android.com/tools"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="match_parent">
|
android:layout_height="match_parent"
|
||||||
|
android:descendantFocusability="blocksDescendants">
|
||||||
|
|
||||||
<androidx.constraintlayout.widget.ConstraintLayout
|
<androidx.constraintlayout.widget.ConstraintLayout
|
||||||
android:id="@+id/constraintLayout"
|
android:id="@+id/constraintLayout"
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user