AndroidManifest.xml 拡張子:txtvbswsfjsphpjavahtmlutf8sjis <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.listrss" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <uses-permission android:name="android.permission.INTERNET"/> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.listrss.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="com.example.listrss.MyList"> <intent-filter> <action android:name="android.intent.action.MAIN"/> </intent-filter> </activity> </application> </manifest> ListItem.java 拡張子:txtvbswsfjsphpjavahtmlutf8sjis package com.example.listrss; public class ListItem { public String title; public String iamge_url; public ListItem(String title, String iamge_url){ this.title = title; this.iamge_url = iamge_url; } }
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.listrss" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <uses-permission android:name="android.permission.INTERNET"/> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.listrss.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="com.example.listrss.MyList"> <intent-filter> <action android:name="android.intent.action.MAIN"/> </intent-filter> </activity> </application> </manifest>
package com.example.listrss; public class ListItem { public String title; public String iamge_url; public ListItem(String title, String iamge_url){ this.title = title; this.iamge_url = iamge_url; } }
ボタンからの呼び出しのみ 拡張子:txtvbswsfjsphpjavahtmlutf8sjis package com.example.listrss; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button button = (Button)this.findViewById(R.id.button1); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(); intent.setClassName("com.example.listrss", "com.example.listrss.MyList"); startActivity(intent); } }); } }
package com.example.listrss; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button button = (Button)this.findViewById(R.id.button1); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(); intent.setClassName("com.example.listrss", "com.example.listrss.MyList"); startActivity(intent); } }); } }
拡張子:txtvbswsfjsphpjavahtmlutf8sjis package com.example.listrss; import java.util.ArrayList; import java.util.List; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; import android.app.ListActivity; import android.os.Bundle; import android.util.Log; import android.view.ViewGroup; public class MyList extends ListActivity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO 自動生成されたメソッド・スタブ super.onCreate(savedInstanceState); String url = "http://matome.naver.jp/feed/hot"; new MyTask() { @Override protected void onPostExecute(Document result) { if ( result == null ) { return; } // ルート要素 Element root = result.getDocumentElement(); Log.i("MyList", root.getNodeName()); List<ListItem> listItems = new ArrayList<ListItem>(); listItems.clear(); NodeList node_list = root.getElementsByTagName("item"); for( int i = 0; i < node_list.getLength(); i++ ) { NodeList nl = ((Element)node_list.item(i)).getElementsByTagName("title"); Log.i("MyList", nl.item(0).getFirstChild().getNodeValue()); listItems.add(new ListItem(nl.item(0).getFirstChild().getNodeValue(),"")); } MyList.this.setListAdapter(new MyAdapter(MyList.this, 0, listItems)); } }.execute(url); } }
package com.example.listrss; import java.util.ArrayList; import java.util.List; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; import android.app.ListActivity; import android.os.Bundle; import android.util.Log; import android.view.ViewGroup; public class MyList extends ListActivity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO 自動生成されたメソッド・スタブ super.onCreate(savedInstanceState); String url = "http://matome.naver.jp/feed/hot"; new MyTask() { @Override protected void onPostExecute(Document result) { if ( result == null ) { return; } // ルート要素 Element root = result.getDocumentElement(); Log.i("MyList", root.getNodeName()); List<ListItem> listItems = new ArrayList<ListItem>(); listItems.clear(); NodeList node_list = root.getElementsByTagName("item"); for( int i = 0; i < node_list.getLength(); i++ ) { NodeList nl = ((Element)node_list.item(i)).getElementsByTagName("title"); Log.i("MyList", nl.item(0).getFirstChild().getNodeValue()); listItems.add(new ListItem(nl.item(0).getFirstChild().getNodeValue(),"")); } MyList.this.setListAdapter(new MyAdapter(MyList.this, 0, listItems)); } }.execute(url); } }
拡張子:txtvbswsfjsphpjavahtmlutf8sjis package com.example.listrss; import java.net.HttpURLConnection; import java.net.URL; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import android.os.AsyncTask; import android.util.Log; public class MyTask extends AsyncTask<String, Void, Document> { @Override protected Document doInBackground(String... params) { Document xml_data = null; try { // ターゲット URL url = new URL(params[0]); // 接続オブジェクト HttpURLConnection http = (HttpURLConnection)url.openConnection(); // GET メソッド http.setRequestMethod("GET"); // 接続 http.connect(); DocumentBuilderFactory dbfactory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = dbfactory.newDocumentBuilder(); // InputStream から Documentオブジェクトを取得 xml_data = builder.parse(http.getInputStream()); // 接続解除 http.disconnect(); } catch(Exception e) { Log.i("MyList", e.getMessage()); } return xml_data; } }
package com.example.listrss; import java.net.HttpURLConnection; import java.net.URL; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import android.os.AsyncTask; import android.util.Log; public class MyTask extends AsyncTask<String, Void, Document> { @Override protected Document doInBackground(String... params) { Document xml_data = null; try { // ターゲット URL url = new URL(params[0]); // 接続オブジェクト HttpURLConnection http = (HttpURLConnection)url.openConnection(); // GET メソッド http.setRequestMethod("GET"); // 接続 http.connect(); DocumentBuilderFactory dbfactory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = dbfactory.newDocumentBuilder(); // InputStream から Documentオブジェクトを取得 xml_data = builder.parse(http.getInputStream()); // 接続解除 http.disconnect(); } catch(Exception e) { Log.i("MyList", e.getMessage()); } return xml_data; } }
拡張子:txtvbswsfjsphpjavahtmlutf8sjis package com.example.listrss; import java.util.List; import android.app.Activity; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.TextView; public class MyAdapter extends ArrayAdapter<ListItem> { public MyAdapter(Context context, int textViewResourceId, List<ListItem> objects) { super(context, textViewResourceId, objects); // TODO 自動生成されたコンストラクター・スタブ } @Override public View getView(int position, View convertView, ViewGroup parent) { View rowView = convertView; // ********************************* // 行のビューを作成する // ********************************* if ( rowView == null ) { LayoutInflater inflater = ((Activity)this.getContext()).getLayoutInflater(); rowView = inflater.inflate(R.layout.list_item, null); } // 行のビューに値を TextView textView1 = (TextView)rowView.findViewById(R.id.text1); textView1.setText((this.getItem(position)).title); return rowView; } }
package com.example.listrss; import java.util.List; import android.app.Activity; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.TextView; public class MyAdapter extends ArrayAdapter<ListItem> { public MyAdapter(Context context, int textViewResourceId, List<ListItem> objects) { super(context, textViewResourceId, objects); // TODO 自動生成されたコンストラクター・スタブ } @Override public View getView(int position, View convertView, ViewGroup parent) { View rowView = convertView; // ********************************* // 行のビューを作成する // ********************************* if ( rowView == null ) { LayoutInflater inflater = ((Activity)this.getContext()).getLayoutInflater(); rowView = inflater.inflate(R.layout.list_item, null); } // 行のビューに値を TextView textView1 = (TextView)rowView.findViewById(R.id.text1); textView1.setText((this.getItem(position)).title); return rowView; } }
リストビューの中の行の定義(rowView) 拡張子:txtvbswsfjsphpjavahtmlutf8sjis <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <ImageView android:id="@+id/icon" android:layout_width="60dip" android:layout_height="60dip" android:src="@drawable/ic_launcher" /> <TextView android:id="@+id/text1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal"/> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <ImageView android:id="@+id/icon" android:layout_width="60dip" android:layout_height="60dip" android:src="@drawable/ic_launcher" /> <TextView android:id="@+id/text1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal"/> </LinearLayout>