在Firebase中搜索产品

时间:2017-07-27 04:41:36

标签: android firebase firebase-realtime-database

public class SearchActivity extends AppCompatActivity {

    private EditText InputString;
    private Button btnScan, btnSearch;
    private DatabaseReference mFirebaseDatabaseReference;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);

        InputString = (EditText) findViewById(R.id.input_search);
        btnScan = (Button) findViewById(R.id.btn_Scan);
        btnSearch = (Button) findViewById(R.id.btn_Search);
        mFirebaseDatabaseReference = FirebaseDatabase.getInstance().getReference();


        btnSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String checkString = "InputString";

                Query query = mFirebaseDatabaseReference.child("Stock").orderByChild("productName").orderByChild("productId").equalTo(checkString);
                query.addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {

                      setContentView(R.layout.activity_product__detail_);
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }
                });
            }
        });
    }
}

I am saving the data as follow in Firebase,

我想让用户搜索用户是否键入“产品名称”,“产品代码”或使用“条形码”搜索产品,然后应用程序将显示产品详细信息。例如,用户输入“低碳钢”,应用程序将显示产品名称,产品代码,条形码和数量

2 个答案:

答案 0 :(得分:0)

此处示例代码

final String checkString = "inputString";

Query query = mDatabase.child("user").orderByChild("productName").equalTo(checkString);

 query.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {

    }

答案 1 :(得分:0)

 mDatabase.child("e-inventory-store").chile("Stock")
  .addListenerForSingleValueEvent(new 
  ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            Map<String, String> map = (Map<String, String>) dataSnapshot.getValue();
            String productName = map.get("productName");

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

试试这个,