Revisão | 116 (tree) |
---|---|
Hora | 2013-12-05 16:40:42 |
Autor | ![]() |
#32526 [チーム選択画面]チーム取得処理についてCursorLoaderを使用するようにする。
@@ -2,26 +2,30 @@ | ||
2 | 2 | |
3 | 3 | import jp.co.versus.R; |
4 | 4 | import jp.co.versus.provider.VersusContract.Team; |
5 | -import android.app.Activity; | |
6 | 5 | import android.content.ContentResolver; |
7 | 6 | import android.content.Intent; |
8 | 7 | import android.database.Cursor; |
9 | 8 | import android.os.Bundle; |
9 | +import android.support.v4.app.FragmentActivity; | |
10 | +import android.support.v4.app.LoaderManager.LoaderCallbacks; | |
11 | +import android.support.v4.content.CursorLoader; | |
12 | +import android.support.v4.content.Loader; | |
13 | +import android.util.Log; | |
10 | 14 | import android.view.View; |
11 | 15 | import android.widget.AdapterView; |
16 | +import android.widget.ArrayAdapter; | |
12 | 17 | import android.widget.ListView; |
13 | -import android.widget.SimpleCursorAdapter; | |
14 | 18 | import android.widget.TextView; |
15 | 19 | |
16 | -public class TeamSelectActivity extends Activity{ | |
20 | +public class TeamSelectActivity extends FragmentActivity implements LoaderCallbacks<Cursor>{ | |
17 | 21 | final static public String KEY_NUMBER = "key.number"; |
18 | 22 | final static public String KEY_EXCLUDE = "key.exclude"; |
19 | 23 | final static public String KEY_STRINGDATA = "key.Stringdata"; |
20 | 24 | |
21 | - private ContentResolver contentResolver; | |
22 | - private SimpleCursorAdapter adapter; | |
25 | + private ArrayAdapter<String> mAdapter; | |
23 | 26 | private int team; |
24 | 27 | private String excludeTeam; |
28 | + final static private String TAG = "TeamSelectActivity"; | |
25 | 29 | |
26 | 30 | @Override |
27 | 31 | public void onCreate(Bundle savedInstanceState){ |
@@ -36,38 +40,25 @@ | ||
36 | 40 | excludeTeam = bundle.getString(KEY_EXCLUDE); |
37 | 41 | } |
38 | 42 | |
39 | - contentResolver = getContentResolver(); | |
40 | - Cursor cursor = contentResolver.query(Team.CONTENT_URI, new String[] { | |
41 | - Team._ID, Team.TEAM }, Team.TEAM + "<>?", new String[]{excludeTeam}, Team.TEAM + " asc"); | |
42 | - startManagingCursor(cursor); | |
43 | + mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1); | |
43 | 44 | |
44 | - adapter = new SimpleCursorAdapter(this, | |
45 | - android.R.layout.simple_list_item_1, cursor, new String[] { | |
46 | - Team._ID, Team.TEAM }, new int[] { | |
47 | - android.R.id.text2, android.R.id.text1 }); | |
45 | + getSupportLoaderManager().initLoader(0, null, this); | |
48 | 46 | |
49 | 47 | ListView listView = (ListView)findViewById(R.id.listview); |
50 | - listView.setAdapter(adapter); | |
48 | + listView.setAdapter(mAdapter); | |
51 | 49 | listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){ |
52 | 50 | @Override |
53 | 51 | public void onItemClick(AdapterView<?> parent, View view, int position, |
54 | 52 | long id) { |
55 | 53 | ListView listView = (ListView)parent; |
56 | - long item = listView.getItemIdAtPosition(position); | |
57 | - returnIntent(getTeamDB(item)); | |
54 | + String data = (String)listView.getItemAtPosition(position); | |
55 | + | |
56 | + returnIntent(data); | |
58 | 57 | finish(); |
59 | 58 | } |
60 | 59 | }); |
61 | 60 | } |
62 | 61 | |
63 | - public String getTeamDB(long id){ | |
64 | - Cursor c = contentResolver.query(Team.CONTENT_URI, new String[] {Team.TEAM}, Team._ID + "=?", new String[] {String.valueOf(id)}, null); | |
65 | - if(c.moveToFirst()){ | |
66 | - return c.getString(0); | |
67 | - } | |
68 | - return null; | |
69 | - } | |
70 | - | |
71 | 62 | public void returnIntent(String teamName){ |
72 | 63 | Intent intent = new Intent(); //渡すインテント |
73 | 64 | Bundle bundle = new Bundle(); //データ格納先 |
@@ -84,4 +75,32 @@ | ||
84 | 75 | TextView headerText = (TextView)findViewById(R.id.header_title); |
85 | 76 | headerText.setText(R.string.header_team_select); |
86 | 77 | } |
78 | + | |
79 | + @Override | |
80 | + public Loader<Cursor> onCreateLoader(int id, Bundle args) { | |
81 | + return new CursorLoader(getApplicationContext(), Team.CONTENT_URI, | |
82 | + new String[] {Team._ID, Team.TEAM }, Team.TEAM + "<>?", | |
83 | + new String[]{excludeTeam}, Team.TEAM + " asc"); | |
84 | + } | |
85 | + | |
86 | + @Override | |
87 | + public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) { | |
88 | + if (cursor != null) { | |
89 | + cursor.moveToFirst(); | |
90 | + do { | |
91 | + try { | |
92 | + String id = cursor.getString(0); | |
93 | + String team_name = cursor.getString(1); | |
94 | + mAdapter.add(team_name); | |
95 | + Log.d(TAG, "ID = " + id); | |
96 | + Log.d(TAG, "Name = " + team_name); | |
97 | + } catch (Exception e) { | |
98 | + } | |
99 | + } while (cursor.moveToNext()); | |
100 | + } | |
101 | + } | |
102 | + | |
103 | + @Override | |
104 | + public void onLoaderReset(Loader<Cursor> arg0) { | |
105 | + } | |
87 | 106 | } |