-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSellAddItem SONUPDATE
168 lines (144 loc) · 5.35 KB
/
SellAddItem SONUPDATE
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package com.example.bilkentcompanion;
import android.content.Intent;
import android.media.Image;
import android.net.Uri;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.Toast;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;
import com.google.firebase.storage.UploadTask;
import com.squareup.picasso.Picasso;
import java.text.SimpleDateFormat;
import java.util.Date;
public class SellAddItem extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
Spinner category;
String categorySelect;
Button delete;
Button add;
EditText itemName;
EditText priceText;
String stringDate;
EditText description;
Button choose;
Uri uri;
ImageView image;
static final int GALLERY_INTENT = 2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sell_add_item);
//spinner
category = findViewById(R.id.buySellSpinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.buySellCategories, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
category.setAdapter(adapter);
category.setOnItemSelectedListener(this);
choose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent, GALLERY_INTENT);
}
});
//delete and add buttons
deleteButtonActions();
addButtonActions();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == GALLERY_INTENT && resultCode == RESULT_OK && data != null && data.getData() != null) {
uri = data.getData();
Picasso.get().load(uri).into(image);
}
}
private void uploadFile(String key)
{
if (uri != null)
{
StorageReference fileRef = FirebaseStorage.getInstance().getReference().child(key);
fileRef.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Toast.makeText(SellAddItem.this,"Upload successful",Toast.LENGTH_SHORT).show();
}
});
}
else
{
Toast.makeText(this, "No file selected",Toast.LENGTH_SHORT ).show();
}
}
//get item name
public String getName() {
itemName = findViewById(R.id.editText);
return itemName.getText().toString();
}
//get price
public int getPrice() {
int price;
priceText = findViewById(R.id.description);
price = Integer.parseInt(String.valueOf(priceText.getText()));
if (price > 1000 || price < 0) {
Toast.makeText(getApplicationContext(), "Enter a price between 0 and 1000", Toast.LENGTH_SHORT).show();
return -1;
} else {
return price;
}
}
public String getDescription() {
String descriptionFull;
description = findViewById(R.id.descriptionSell);
descriptionFull = String.valueOf(description.getText());
return descriptionFull;
}
private void addButtonActions() {
add = (Button) findViewById(R.id.addItem);
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//String uid = getUID;
String uid = "cansu";
String[] info = {getName(), uid, categorySelect, getDescription(),"" + getPrice() };
String s = DatabaseHelper.initialiseSellPost(info);
uploadFile(s);
Intent added = new Intent(SellAddItem.this, BuyFilter.class);
startActivity(added);
}
});
}
private void deleteButtonActions() {
delete = (Button) findViewById(R.id.deleteButton);
delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//delete info
//pass to the previous page
Intent deleted = new Intent(SellAddItem.this, BuyFilter.class);
startActivity(deleted);
}
});
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
categorySelect = parent.getItemAtPosition(position).toString();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
@Override
public void onPointerCaptureChanged(boolean hasCapture) {
}
}