2015-02-08 35 views
0

我有一個簡單的ParseQuery,它檢索一個類'PrivateData'的單個對象。如何使用從parse.com數據庫檢索的數據動態添加cardslib CardListView標題文本?

ParseQuery<PrivateData> query = ParseQuery.getQuery(PrivateData.class); 
query.whereEqualTo("user", ParseUser.getCurrentUser()); 

此代碼可以用來填充我的listView與來自PrivateData類的數據。

但我的要求是有一個CardListView和填充標題。

如何設置卡片標題與我從parse.com得到的數據?

public class HistoryActivity extends ActionBarActivity { 


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

    // get action bar 
    android.support.v7.app.ActionBar actionBar = getSupportActionBar(); 

    // Enabling Up/Back navigation 
    actionBar.setDisplayHomeAsUpEnabled(true); 

    /* 
      1. Issue the parse query 

      2. Set the result from the query to each card title of CardListView 

      3. Repeat step 1 and step 2 until we get all the results and they are populated dynamically in CardsListView 
    */ 




} 

回答

0

謝謝你Gabriele Mariotti!所以我想我已經明白了。這是代碼。 歡迎提出建議和/或改進。

ParseQuery<PrivateData> query = new ParseQuery<>("PrivateData"); 
    query.whereEqualTo("user", ParseUser.getCurrentUser()); 
    query.orderByDescending("createdAt"); 
    query.setCachePolicy(ParseQuery.CachePolicy.CACHE_THEN_NETWORK); 
    query.findInBackground(new FindCallback<PrivateData>() { 

     @Override 
     public void done(List<PrivateData> data, ParseException error) { 
      if (data != null) { 

       CardArrayAdapter mCardArrayAdapter; 
       CardListView cardListView; 
       Card card; 
       ArrayList<Card> cardArrayList = new ArrayList<>(); 

       for (int i = 0; i < data.size(); i++) { 
        card = new Card(getApplicationContext()); 
        card.setTitle("Created At : " + data.get(i).getCreatedAt().toString()); 

        cardArrayList.add(card); 
       } 


       mCardArrayAdapter = new CardArrayAdapter(getApplicationContext(), cardArrayList); 
       cardListView = (CardListView) findViewById(R.id.history_card_view); 


       cardListView.setAdapter(mCardArrayAdapter); 


      } 
     } 
    }); 
0

問題在哪裏?

ArrayList<Card> cards = new ArrayList<Card>(); 
for (int i = 0; i < query.count; i++) { 

    Card card = new Card(getActivity()); 
    card.setTitle(xxx); 
    cards.add(card); 
}  
mCardArrayAdapter.addAll(cards); 
mCardArrayAdapter.notifyDataSetChanged(); 

當然,您可以使用自定義卡片和自定義佈局來標題。

相關問題