1/* //device/apps/Notes/NotesList.java
2**
3** Copyright 2006, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9**     http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17package com.android.development;
18
19import android.app.ListActivity;
20import android.content.Intent;
21import android.database.Cursor;
22import android.net.Uri;
23import android.os.Bundle;
24import android.view.Menu;
25import android.view.MenuItem;
26import android.view.View;
27import android.widget.ListView;
28import android.widget.SimpleCursorAdapter;
29
30import java.util.ArrayList;
31
32public class DataList extends ListActivity
33{
34    public void onCreate(Bundle icicle)
35    {
36        super.onCreate(icicle);
37
38        Intent intent = getIntent();
39
40        mCursor = getContentResolver().query(intent.getData(), null, null, null, null);
41        mDisplay = intent.getStringExtra("display");
42        if (mDisplay == null) {
43            mDisplay = "_id";
44        }
45
46        if (mCursor != null) {
47            setListAdapter(new SimpleCursorAdapter(
48                    this,
49                    R.layout.url_list,
50                    mCursor,
51                    new String[] {mDisplay},
52                    new int[] {android.R.id.text1}));
53        }
54    }
55
56    public void onStop()
57    {
58        super.onStop();
59
60        if (mCursor != null) {
61            mCursor.deactivate();
62        }
63    }
64
65    public void onResume()
66    {
67        super.onResume();
68
69        if (mCursor != null) {
70            mCursor.requery();
71        }
72
73        setTitle("Showing " + mDisplay);
74    }
75
76    public boolean onCreateOptionsMenu(Menu menu)
77    {
78        super.onCreateOptionsMenu(menu);
79        menu.add(0, 0, 0, "Requery").setOnMenuItemClickListener(mRequery);
80        return true;
81    }
82
83    protected void onListItemClick(ListView l, View v, int position, long id)
84    {
85        mCursor.moveToPosition(position);
86
87        ArrayList<ColumnData> data = new ArrayList<ColumnData>();
88
89        String[] columnNames = mCursor.getColumnNames();
90        for (int i=0; i<columnNames.length; i++) {
91            String str = mCursor.getString(i);
92            ColumnData cd = new ColumnData(columnNames[i], str);
93            data.add(cd);
94        }
95
96
97        Uri uri = null;
98        int idCol = mCursor.getColumnIndex("_id");
99        if (idCol >= 0) {
100            uri = Uri.withAppendedPath(getIntent().getData(), mCursor.getString(idCol));
101        }
102        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
103        intent.setClass(this, Details.class);
104
105        intent.putExtra("data", data);
106        int displayColumn = mCursor.getColumnIndex(mDisplay);
107        if (displayColumn >= 0) {
108            intent.putExtra("title",
109                                ((ColumnData)data.get(displayColumn)).value);
110        }
111
112        startActivity(intent);
113    }
114
115    MenuItem.OnMenuItemClickListener mRequery = new MenuItem.OnMenuItemClickListener() {
116        public boolean onMenuItemClick(MenuItem item) {
117            // Should just do requery on cursor, but
118            // doesn't work right now.  So do this instead.
119            mCursor.requery();
120            if (mCursor != null) {
121                setListAdapter(new SimpleCursorAdapter(
122                        DataList.this,
123                        R.layout.url_list,
124                        mCursor,
125                        new String[] {mDisplay},
126                        new int[] {android.R.id.text1}));
127            }
128            return true;
129        }
130    };
131
132    private String mDisplay;
133    private Cursor mCursor;
134}
135