またもやコレジャナイ感。
1時間くらいかかった_| ̄|○
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" > | |
<ListView | |
android:id="@+id/listview" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" /> | |
</RelativeLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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" > | |
<TextView | |
android:id="@+id/index" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_weight="1" /> | |
<TextView | |
android:id="@+id/value" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_weight="1" /> | |
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.android_practice; | |
import java.util.ArrayList; | |
import java.util.List; | |
import android.app.Activity; | |
import android.content.Context; | |
import android.os.Bundle; | |
import android.view.LayoutInflater; | |
import android.view.Menu; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.ArrayAdapter; | |
import android.widget.ListView; | |
import android.widget.TextView; | |
public class MainActivity extends Activity { | |
private ListView _listView; | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
this.setContentView(R.layout.activity_main); | |
this._listView = (ListView) this.findViewById(R.id.listview); | |
this._createListView(); | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
getMenuInflater().inflate(R.menu.activity_main, menu); | |
return true; | |
} | |
private void _createListView() { | |
List<RowItem> rowItems = new ArrayList<RowItem>(); | |
for (int i = 1; i <= 100; i++) { | |
String indexAsString = String.valueOf(i); | |
if (i % 3 == 0 && i % 5 == 0) { | |
rowItems.add(new RowItem(indexAsString, "FizzBuzz")); | |
} else if (i % 3 == 0) { | |
rowItems.add(new RowItem(indexAsString, "Fizz")); | |
} else if (i % 5 == 0) { | |
rowItems.add(new RowItem(indexAsString, "Buzz")); | |
} else { | |
rowItems.add(new RowItem(indexAsString, indexAsString)); | |
} | |
} | |
RowItemArrayAdapter arrayAdapter = new RowItemArrayAdapter(this, | |
R.layout.activity_main_list_row, rowItems); | |
this._listView.setAdapter(arrayAdapter); | |
} | |
private static class RowItem { | |
private String _index; | |
private String _value; | |
public RowItem(String index, String value) { | |
this._index = index; | |
this._value = value; | |
} | |
public String getIndex() { | |
return this._index; | |
} | |
public String getValue() { | |
return this._value; | |
} | |
} | |
private static class ViewHolder { | |
public TextView indexTextView; | |
public TextView valueTextView; | |
} | |
private static class RowItemArrayAdapter extends ArrayAdapter<RowItem> { | |
private LayoutInflater _layoutInflater; | |
private int _textViewResourceId; | |
public RowItemArrayAdapter(Context context, int resource, List<RowItem> objects) { | |
super(context, resource, resource, objects); | |
this._layoutInflater = (LayoutInflater) context | |
.getSystemService(Context.LAYOUT_INFLATER_SERVICE); | |
this._textViewResourceId = resource; | |
} | |
/* | |
* (non-Javadoc) | |
* | |
* @see android.widget.ArrayAdapter#getView(int, android.view.View, | |
* android.view.ViewGroup) | |
*/ | |
@Override | |
public View getView(int position, View convertView, ViewGroup parent) { | |
final RowItem rowItem = (RowItem) this.getItem(position); | |
ViewHolder viewHolder; | |
if (null == convertView) { | |
convertView = this._layoutInflater.inflate(this._textViewResourceId, null); | |
viewHolder = new ViewHolder(); | |
viewHolder.indexTextView = (TextView) convertView.findViewById(R.id.index); | |
viewHolder.valueTextView = (TextView) convertView.findViewById(R.id.value); | |
convertView.setTag(viewHolder); | |
} else { | |
viewHolder = (ViewHolder) convertView.getTag(); | |
} | |
viewHolder.indexTextView.setText(rowItem.getIndex()); | |
viewHolder.valueTextView.setText(rowItem.getValue()); | |
return convertView; | |
} | |
} | |
} |