两次按下后显示按下按钮的结果

时间:2017-07-06 18:46:46

标签: android firebase firebase-realtime-database android-recyclerview

我有一个在firebase中搜索的活动,它包含用于在按下按钮时键入文本和按钮的edittext结果应该出现在recyclerview中,但是当我按下按钮时第一次没有任何内容出现,除非我按下按钮第二次 findDataActivity的代码

SELECT
    c.name as client,
    p.name as project,
    SUM(t.hours) as hours,
    SUM(ci.amount) as invoice_amount,
    COUNT(ci.id) as number_invoices
FROM
    projects p
    INNER JOIN client_master c 
        ON c.harvest_id = p.client_id
    INNER JOIN timesheet t 
        ON t.project_id = p.id 
    LEFT JOIN projects_contracts pc 
        ON pc.project_id = p.id
    LEFT JOIN contracts_invoices ci 
        ON ci.contract_id = pc.contract_id
GROUP BY
    c.name,
    p.name
ORDER BY
    c.name, 
    p.name,
    t.hours

firebase的代码

public class FindDataActivity extends AppCompatActivity {
    DatabaseReference db;
    FirebaseHelper helper;
    MyAdapter adapter;
    RecyclerView rv;
    EditText findETxt;
    Button findBtn;
    ArrayList<Paints> p;
    String s;

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

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        ActionBar actionBar = getSupportActionBar();
        if(actionBar != null)
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        //SETUP RECYCLER
        rv = (RecyclerView) findViewById(R.id.rvsearch);
        rv.setLayoutManager(new LinearLayoutManager(getApplicationContext()));

        //INITIALIZE FIREBASE DB
        db = FirebaseDatabase.getInstance().getReference("Database");

        adapter = new MyAdapter(FindDataActivity.this, p);
        rv.setAdapter(adapter);
        findBtn = (Button) findViewById(R.id.find_btn);
        helper = new FirebaseHelper(db);
    }

    public void search(View view) {

        db.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                findETxt = (EditText) findViewById(R.id.findETxt);
                s = findETxt.getText().toString();
                p=helper.retrieveSearch(s);
                adapter = new MyAdapter(FindDataActivity.this, p);
                rv.setAdapter(adapter);
                adapter.notifyDataSetChanged();
            }
            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
    }




    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id=item.getItemId();
        if(id == android.R.id.home){
            onBackPressed();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

1 个答案:

答案 0 :(得分:-1)

MyAdapter课程中,你必须创建一个这样的方法:

public void updateAdapter(ArrayList<Paints> paint){
    this.paint= paint;
    notifyDataSetChanged();
}

search method adapter.notifyDataSetChanged();替换adapter.updateAdapter(p);