|
| 1 | +package com.parse.ui.widget.sample; |
| 2 | + |
| 3 | +import android.os.Bundle; |
| 4 | +import android.support.annotation.Nullable; |
| 5 | +import android.support.v7.app.AppCompatActivity; |
| 6 | +import android.widget.ListView; |
| 7 | +import android.widget.Toast; |
| 8 | + |
| 9 | +import com.parse.ParseException; |
| 10 | +import com.parse.ParseObject; |
| 11 | +import com.parse.ParseQuery; |
| 12 | +import com.parse.ParseQueryAdapter; |
| 13 | + |
| 14 | +import java.util.List; |
| 15 | + |
| 16 | + |
| 17 | +public class ListActivity extends AppCompatActivity { |
| 18 | + |
| 19 | + @Override |
| 20 | + protected void onCreate(@Nullable Bundle savedInstanceState) { |
| 21 | + super.onCreate(savedInstanceState); |
| 22 | + setContentView(R.layout.activity_list); |
| 23 | + |
| 24 | + ListView listView = (ListView) findViewById(R.id.list); |
| 25 | + |
| 26 | + ParseQueryAdapter<ParseObject> adapter = new ParseQueryAdapter<>(this, |
| 27 | + new ParseQueryAdapter.QueryFactory<ParseObject>() { |
| 28 | + @Override |
| 29 | + public ParseQuery<ParseObject> create() { |
| 30 | + return ParseQuery.getQuery("Contact") |
| 31 | + .orderByAscending("name") |
| 32 | + .setCachePolicy(ParseQuery.CachePolicy.CACHE_THEN_NETWORK); |
| 33 | + } |
| 34 | + }, android.R.layout.simple_list_item_1); |
| 35 | + adapter.setTextKey("name"); |
| 36 | + adapter.addOnQueryLoadListener(new ParseQueryAdapter.OnQueryLoadListener<ParseObject>() { |
| 37 | + @Override |
| 38 | + public void onLoading() { |
| 39 | + |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public void onLoaded(List<ParseObject> objects, Exception e) { |
| 44 | + if (e != null |
| 45 | + && e instanceof ParseException |
| 46 | + && ((ParseException) e).getCode() != ParseException.CACHE_MISS) { |
| 47 | + Toast.makeText(ListActivity.this, "Error: " + e.getMessage(), Toast.LENGTH_LONG).show(); |
| 48 | + } |
| 49 | + } |
| 50 | + }); |
| 51 | + listView.setAdapter(adapter); |
| 52 | + } |
| 53 | +} |
0 commit comments