2017-04-19 175 views
0

我開發的應用程序,保持用戶的開支,我寫CustomAdapter如何從Firebase數據庫正確獲取數據?

public class CustomAdapter extends BaseAdapter{ 
Context c; 
ArrayList<Income> incomes; 



public CustomAdapter(){} 

public CustomAdapter(Context c,ArrayList<Income> incomes){ 
    this.incomes=incomes; 
    this.c=c; 


} 

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

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

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

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

    if(convertView==null){ 
     convertView= LayoutInflater.from(c).inflate(R.layout.model,parent,false); 
    } 

    TextView nameTxt= (TextView) convertView.findViewById(R.id.nameTxt); 
    TextView propTxt= (TextView) convertView.findViewById(R.id.aciklama); 
    TextView descTxt= (TextView) convertView.findViewById(R.id.descTxt); 



    Income s= (Income) this.getItem(position); 
    nameTxt.setText(s.getName()); 
    propTxt.setText(s.getType()); 
    descTxt.setText((String.valueOf(s.getMiktar()))); 

    return convertView; 
} 

,並在活動中實現

public class ExpenseActivity extends Activity{ 

DatabaseReference db; 

FloatingActionButton fab; 
ExpenseAdapter adapter; 
ListView lv; 
Boolean saved; 
EditText nameEditTxt, propTxt, descTxt; 
ArrayList<Expense> expenselist = new ArrayList<>(); 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.expense_content); 

    //Toolbar toolbar=(Toolbar)findViewById(R.id.toolbar); 
    // setSupportActionBar(toolbar); 
    lv = (ListView) findViewById(R.id.listview); 

    db = FirebaseDatabase.getInstance().getReference(); 
    //isa 



    adapter = new ExpenseAdapter(ExpenseActivity.this, getExpenses()); 
    lv.setAdapter(adapter); 
    adapter.notifyDataSetChanged(); 
    fab = (FloatingActionButton) findViewById(R.id.fab); 
    fab.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      displayInput(); 
     } 
    }); 


} 

public Boolean save(Expense expense){ 

    if(expense==null){ 

     saved=false; 

    }else{ 

     try{ 


      db.child("Expense").push().setValue(expense); 

      saved=true; 
     }catch (Exception ex){ 

      ex.printStackTrace();; 
      saved=false; 

     } 
    } 

    return saved; 


} 



public void fetchData(DataSnapshot dataSnapshot) 
{ 



    expenselist.clear(); 


    for (DataSnapshot ds : dataSnapshot.getChildren()) 
    { 
     Expense expense=ds.getValue(Expense.class); 
     expenselist.add(expense); 
     adapter.notifyDataSetChanged(); 

    } 

} 




public ArrayList<Expense> getExpenses(){ 

    db.addChildEventListener(new ChildEventListener() { 
     @Override 
     public void onChildAdded(DataSnapshot dataSnapshot, String s) { 
      fetchData(dataSnapshot); 



     } 

     @Override 
     public void onChildChanged(DataSnapshot dataSnapshot, String s) { 

      fetchData(dataSnapshot); 
     } 

     @Override 
     public void onChildRemoved(DataSnapshot dataSnapshot) { 

     } 

     @Override 
     public void onChildMoved(DataSnapshot dataSnapshot, String s) { 

     } 

     @Override 
     public void onCancelled(DatabaseError databaseError) { 

     } 
    }); 

    return expenselist; 

} 






private void displayInput() { 
    final Dialog d=new Dialog(this); 

    d.setTitle("Gelir Girin"); 

    d.setContentView(R.layout.expense_layout); 

    nameEditTxt = (EditText) d.findViewById(R.id.name); 
    propTxt = (EditText) d.findViewById(R.id.kategori); 
    descTxt = (EditText) d.findViewById(R.id.miktar); 
    Button saveBtn = (Button) d.findViewById(R.id.saveBtn); 


    saveBtn.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      String name = nameEditTxt.getText().toString(); 
      String type = propTxt.getText().toString(); 
      String amount= String.valueOf(descTxt.getText().toString()); 

      Expense expense=new Expense(name,type,amount); 




      if (name != null && name.length() > 0) { 
       //THEN SAVE 
       if (save(expense)) { 


        //IF SAVED CLEAR EDITXT 
        d.cancel(); 
        nameEditTxt.setText(""); 
        propTxt.setText(""); 
        descTxt.setText(""); 


       } 
      } else { 
       Toast.makeText(ExpenseActivity.this, "Name Must Not Be Empty", Toast.LENGTH_SHORT).show(); 
      } 

     } 
    }); 

    d.show(); 




} 

,但連我都保存在火力返回null和傳遞空到田地的一些數據(textview),但是當我添加新項目時,它會顯示Firebase中的現有數據。我的一位朋友說,我正在異步調用數據,但我不明白這一點。我想要在活動打開時直接顯示數據。任何幫助表示讚賞,感謝

當我打開活動:

enter image description here

我添加新的數據後:

enter image description here

回答

0
myRef.addListenerForSingleValueEvent(new ValueEventListener() { 
     @Override 
     public void onDataChange(DataSnapshot dataSnapshot) { 
      fetchdata(); 
      } 

     @Override 
     public void onCancelled(DatabaseError databaseError) { 

     } 
    }); 

這將獲取相應的數據

+0

不幸的是它仍然表現相同。 –

0

你的問題是t在從數據庫獲取數據之前,您正在初始化您的適配器。

adapter = new ExpenseAdapter(ExpenseActivity.this, getExpenses()); 

的getExpenses()方法執行異步操作

嘗試直接傳遞代替列表和調用getExpenses方法以及

adapter = new ExpenseAdapter(ExpenseActivity.this, expenselist); 
getExpenses() 

然後,當notifyDatasetChanged被稱爲在fetchData方法它會更新列表。

相關問題