package com.example.nikita.moneytracker;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.IgnoreExtraProperties;
import com.google.firebase.database.Query;
import java.io.Serializable;
public class MainActivity extends AppCompatActivity {
@IgnoreExtraProperties
static class Item implements Serializable{
public String name;
public int price;
public Item() {
}
Item(String name, int price) {
this.name = name;
this.price = price;
}
}
private FirebaseDatabase database;
private DatabaseReference myRef;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
database = FirebaseDatabase.getInstance();
database.setPersistenceEnabled(true);
myRef = database.getReference("items");
final EditText name = (EditText) findViewById(R.id.name);
final EditText price = (EditText) findViewById(R.id.price);
final Button add = (Button) findViewById(R.id.add);
final ListView items = (ListView) findViewById(R.id.items);
final ItemsAdapter adapter = new ItemsAdapter();
items.setAdapter(adapter);
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Item item = new Item(name.getText().toString(), Integer.valueOf(price.getText().toString()));
//здесь была строка,но из за нее создаётся два объекта adapter.add(item);
myRef.push().setValue(item);
}
});
Query myQuery = myRef;
myQuery.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Item item = dataSnapshot.getValue(Item.class);
adapter.add(item);
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
private class ItemsAdapter extends ArrayAdapter<Item> {
ItemsAdapter() {
super(MainActivity.this, R.layout.item);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
final View view = getLayoutInflater().inflate(R.layout.item, null);
final Item item = getItem(position);
((TextView) view.findViewById(R.id.name)).setText(item.name);
((TextView) view.findViewById(R.id.price)).setText(String.valueOf(item.price));
return view;
}
}
//================================================================================
public void delete(final View view){
AlertDialog.Builder a_builder = new AlertDialog.Builder(this);
a_builder.setMessage("Вы хотите удалить товар?")
.setCancelable(false)
.setPositiveButton("Да", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//+++++++++++++++++++++
}
})
.setNegativeButton("Нет", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog alert = a_builder.create();
alert.setTitle("Удаление элемента");
alert.show();
}
//=================================================================================
}