Android - Firebase实时数据库无法使用此库

时间:2017-03-03 19:42:07

标签: android android-layout listview firebase firebase-realtime-database

我一直在为我的应用程序使用firebase,一切正常,但是,我最近更新了我的用户界面,添加了这个library,我的列表视图中的其他数据但这个库的视图没有更新,有人可以帮我解决这个问题吗,我的代码还有一个问题,当我更新状态时,即图书馆的视图,只应更新所选项目视图,而不是所有视图在我的列表中得到更新,如果我选择"已收到"对于列表中的一个项目,所有其他项目都更新为"已收到",这不会反映在firebase控制台中,当我重新打开应用程序时,一切都将得到修复。请帮助我。这里'如果需要,我的git repository链接。 View does not get updated in realtime

  

AddOrUpdateItem.java

package com.fayaz.uniabex;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;

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.ValueEventListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Spinner;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class AddOrUpdateItem extends AppCompatActivity implements OnItemSelectedListener {

    Button bOK,bCancel;
    Item item;
    int position;
    EditText iName,iPono,iQty,iSupplier,iContact,iTranport,iLrno,iRemarks;
    CoordinatorLayout cl;
    public Spinner spinner;
    String status, selectedStatus;
    public ArrayAdapter<String> dataAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_or_update_item);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);

        position = getIntent().getIntExtra("Position", -1);

        cl = (CoordinatorLayout) findViewById(R.id.cdlayout);

        final Intent intent = new Intent(AddOrUpdateItem.this, MainActivity.class);

         iName= (EditText) findViewById(R.id.iName);
        iPono = (EditText) findViewById(R.id.iPoNo);
        iQty = (EditText) findViewById(R.id.iQty);
        iSupplier = (EditText) findViewById(R.id.iSupplier);
        iContact = (EditText) findViewById(R.id.iContact);
        iTranport = (EditText) findViewById(R.id.iTransport);
        iLrno = (EditText) findViewById(R.id.iLrno);
        iRemarks = (EditText) findViewById(R.id.iRemarks);

        // Spinner element
        spinner = (Spinner) findViewById(R.id.status);

        // Spinner click listener
        spinner.setOnItemSelectedListener(this);

        // Spinner Drop down elements
        List<String> categories = new ArrayList<String>();
        categories.add("Enquiry");
        categories.add("Quotation");
        categories.add("Po Rel");
        categories.add("Dispatched");
        categories.add("Received");

        // Creating adapter for spinner
        dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);

        // Drop down layout style - list view with radio button
        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        // attaching data adapter to spinner
        spinner.setAdapter(dataAdapter);

        bOK = (Button) findViewById(R.id.bOk);
        bCancel = (Button) findViewById(R.id.bCancel);

        if(position != -1) {
            getSupportActionBar().setTitle("Edit Entry");
            searchPerson(position);
            item = new Item();
        }
        else {
            getSupportActionBar().setTitle("Add Entry");
            item = null;
        }

        bOK.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                    String test = iRemarks.getText().toString();
                    Item i = new Item();
                    i.setItem(iName.getText().toString());
                    i.setPonum(iPono.getText().toString());
                    i.setQty(iQty.getText().toString());
                    i.setSupplier(iSupplier.getText().toString());
                    i.setContact(iContact.getText().toString());
                    i.setTransporter(iTranport.getText().toString());
                    i.setLrnum(iQty.getText().toString());
                    i.setStatus(selectedStatus);

                    if(test.equals("\n")){
                        i.setRemarks(iRemarks.getText().toString());
                    }
                    else{
                        i.setRemarks(iRemarks.getText().toString() + "\n");
                    }

                    if (item == null)
                        MainActivity.getInstance().addItem(i);
                    else
                        MainActivity.getInstance().updateItemDetails(i, position);
                        finish();

            }
        });

        bCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }

    public void searchPerson(int position) {
        String clickedKey = MainActivity.getInstance().getKeysArray().get(position);
        MainActivity.getInstance().getmDatabase().child(clickedKey).addListenerForSingleValueEvent(
                new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        // Get user value
                        String test = iRemarks.getText().toString();
                        Item person = dataSnapshot.getValue(Item.class);
                        iName.setText(person.getItem());
                        iPono.setText(person.getPonum());
                        iSupplier.setText(person.getSupplier());
                        iQty.setText(person.getQty());
                        iTranport.setText(person.getTransporter());
                        iContact.setText(person.getContact());
                        iLrno.setText(person.getLrnum());
                        spinner.setSelection(dataAdapter.getPosition(person.getStatus()));

                        if(test.equals("\n")){
                            iRemarks.setText(person.getRemarks());
                        }
                        else{
                            iRemarks.setText(person.getRemarks() + "\n");
                        }
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {
                    }
                });
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // handle arrow click here
        if (item.getItemId() == android.R.id.home) {
            finish(); // close this activity and return to preview activity (if there is any)
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        status = parent.getItemAtPosition(position).toString();

        selectedStatus = Integer.toString(position);

    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }
}
  

ItemDetailsAdapter.java

package com.fayaz.uniabex;

/**
 * Created by Fayaz on 21/12/2016.
 */

        import android.widget.BaseAdapter;
        import android.content.Context;
        import android.content.DialogInterface;
        import android.content.Intent;
        import android.support.v7.app.AlertDialog;
        import android.view.LayoutInflater;
        import android.view.View;
        import android.view.ViewGroup;
        import android.widget.Button;
        import android.widget.TextView;

        import com.anton46.stepsview.StepsView;

        import java.util.ArrayList;

public class ItemDetailsAdapter extends BaseAdapter {
    private ArrayList<Item> arrayListItem;
    private Context context;
    private LayoutInflater inflater;

    public String curStatus;

    public ItemDetailsAdapter(Context context, ArrayList<Item> arrayListItem) {
        this.context = context;
        this.arrayListItem = arrayListItem;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

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

    @Override
    public Object getItem(int position) {
        return arrayListItem.get(position);
    }

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

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        final String[] labels = {"Enq", "Qtn", "PO", "Dspd", "Rcvd"};
        View v = convertView;
        Holder holder;
        if (v == null) {
            v = inflater.inflate(R.layout.list_item, null);
            holder = new Holder();
            holder.ItemName = (TextView) v.findViewById(R.id.ItemNametv);
            holder.qty = (TextView) v.findViewById(R.id.qtytv);
            holder.EditItem = (Button) v.findViewById(R.id.EditItem);
            holder.DeleteItem = (Button) v.findViewById(R.id.DeleteItem);
            holder.mStepsView = (StepsView) v.findViewById(R.id.stepsView);

            curStatus = arrayListItem.get(position).getStatus();
            int n = Integer.parseInt(curStatus);

            holder.mStepsView.setLabels(labels)
                    .setBarColorIndicator(context.getResources().getColor(R.color.material_blue_grey_800))
                    .setProgressColorIndicator(context.getResources().getColor(R.color.colorPrimary))
                    .setLabelColorIndicator(context.getResources().getColor(R.color.colorPrimaryDark))
                    .setCompletedPosition(n)
                    .drawView();

            v.setTag(holder);
        } else {
            holder = (Holder) v.getTag();
        }

        holder.ItemName.setText("Item: " + arrayListItem.get(position).getItem());
        holder.qty.setText("Qty: " + arrayListItem.get(position).getQty());
        holder.EditItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context,AddOrUpdateItem.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.putExtra("Position", position);
                context.getApplicationContext().startActivity(intent);
            }
        });
        holder.DeleteItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ShowConfirmDialog(context, position);
            }
        });
        return v;
    }

    class Holder {
        TextView ItemName,qty;
        Button DeleteItem, EditItem;
        StepsView mStepsView;
    }

    public static void ShowConfirmDialog(Context context, final int position) {
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
        alertDialogBuilder
                .setMessage("Are you sure you want to delete this entry?")
                .setCancelable(true)
                .setPositiveButton("YES", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        MainActivity.getInstance().deleteItem(position);
                    }
                })
                .setNegativeButton("NO", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        dialog.cancel();
                    }
                });
        AlertDialog alertDialog = alertDialogBuilder.create();
        alertDialog.show();
    }
}

0 个答案:

没有答案