ListActivity

完全に忘却の彼方。リハビリと言っても過言ではなし。

行のレイアウト

android-10 なサンプルのレイアウトが以下な模様。

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2006 The Android Open Source Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at
  
          http://www.apache.org/licenses/LICENSE-2.0
  
     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="fill_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:gravity="center_vertical"
    android:paddingLeft="5dip"
    android:singleLine="true"
/>

む、android:attr て後で確認しとこ。NotesList.java の onCreate が以下。

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setDefaultKeyMode(DEFAULT_KEYS_SHORTCUT);

        // If no data was given in the intent (because we were started
        // as a MAIN activity), then use our default content provider.
        Intent intent = getIntent();
        if (intent.getData() == null) {
            intent.setData(NoteColumns.CONTENT_URI);
        }

        // Inform the list we provide context menus for items
        getListView().setOnCreateContextMenuListener(this);
        
        // Perform a managed query. The Activity will handle closing and requerying the cursor
        // when needed.
        Cursor cursor = managedQuery(getIntent().getData(), PROJECTION, null, null,
                                        NoteColumns.DEFAULT_SORT_ORDER);

        // Used to map notes entries from the database to views
        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.noteslist_item, cursor,
                new String[] { NoteColumns.TITLE }, new int[] { android.R.id.text1 });
        setListAdapter(adapter);
    }

で、一旦以下なソレがプロトタイプとしてでっち上がりました。

package jp.shuri.yamanetoshi.rorapiclient;

import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;

import jp.shuri.yamanetoshi.json.JSONFunctions;
import android.os.Bundle;
import android.os.Handler;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.util.Log;
import android.view.Menu;
import android.widget.ArrayAdapter;

public class RoRClientActivity extends ListActivity {

    private final String TAG = "RoRClientActivity";
    
    private final String BASE_URL = "http://shrouded-tundra-4125.herokuapp.com/";
    private final String TASKS = "tasks.json";
    private final String PLEASE_WAIT = "please wait...";
    
    private DefaultHttpClient mHttpClient = new DefaultHttpClient();
    private Handler mHandler = new Handler();
    
    private ProgressDialog mProgressDialog;
    
    private JSONArray mList;
    private String[] mArray;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        mProgressDialog = new ProgressDialog(this);
        mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        mProgressDialog.setMessage(PLEASE_WAIT);
        
        mProgressDialog.show();
        
        new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    mList = new JSONArray(JSONFunctions.GETfromURL(BASE_URL + TASKS, mHttpClient));
                } catch (Exception e) {
                    Log.d(TAG, "e is " + e);
                }
                mHandler.post(new Data2List());
            }
        }).start();    
    }
    
    private class Data2List implements Runnable {

        @Override
        public void run() {
            mArray = new String [mList.length()];
            for (int i = 0; i < mList.length(); i++) {
                try {
                    mArray[i] = mList.getJSONObject(i).getString("name");
                } catch (JSONException e) {
                    Log.e(TAG, e.toString());
                }
            }
            
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(RoRClientActivity.this,
                    android.R.layout.simple_list_item_1, mArray);
            setListAdapter(adapter);
            
            mProgressDialog.dismiss();
        }
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.ro_rclient, menu);
        return true;
    }
}

これをプロトタイプとして云々した後に順に膨らませていけば良いのかどうなのか。

  • 機能追記
    • 新規作成、更新、削除
    • タップで更新、長押しで削除とか
  • ActionBar で云々
    • 新規作成は ActionBar なのか

そういえば Adapter 自作なエントリって入れたかな。む、入れてました。

上記なナニも

別途追記するかもしれませんが、とりあえずここでエントリ投入。