私は自分のアプリ用のカスタムアダプターを作成しようとしています。メイン画面はListActivity
中央ListView
にがあります。アダプターをどのように作成するかについてはまったくわかりません。そのため、アダプターを説明するための優れたチュートリアルを誰かが知っているかどうか疑問に思いました。
2 に答える
ArrayAdaptersを学んだことを覚えていますが、それは簡単ではありませんでした。インターネット上にはたくさんのチュートリアルがあります。
ArrayAdapterを拡張すると、メソッドをオーバーライドしたくなるでしょう。
getCount() -リスト内のアイテムの数。
public int getViewTypeCount() -データがArrayListによって提示される方法の数。通常、これは1です。ただし、リストに複数のビューを持つ複数のタイプのデータが含まれている場合は、この数を変更する必要があります。例は連絡先リストです。2種類あります。1は連絡先自体、もう1つはA、Bなどに表示される文字カテゴリです。
getItemViewType(position) -位置については、どのタイプを取得する必要がありますか?通常、1。複数のタイプがない限り。
public long getItemId(int position) -提示するアイテムのタイプ。
本当に重要なものです! public View getView(int position、View convertView、ViewGroup parent)
- 位置は明らかにリストの現在の項目です。
- convertViewは、ビューのリサイクル用です。つまり、アイテムが画面からスクロールアウトすると、そのビューは保持され、画面に表示される別のアイテムによって再利用されるのを待機します。これにより、画面に同じビューが最大で約8程度しか表示されないため、パフォーマンスが向上します。何も再利用できないため、最初はconvertViewはnullになりますが、ユーザーが画面をスクロールすると、それらが表示されます。ViewGroupの親ビューインフレで使用する以外に、よくわかりません。
以下は、ViewHolderパターンを使用したgetViewの例です。ViewHolderを使用すると、findViewByIdを何度もやり直す必要がないため、スクロールがスムーズになります。ビューを新しい情報で更新するたびに、holder.WidgetNameを使用して更新します。
/* Hypotetical list of names. */
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder holder = null;
ListItemWithSideButton view;
if(convertView == null) {
view = (ListItemWithSideButton)_inflater.inflate(R.layout.some_line_item, parent, false);
holder = new ViewHolder();
view.setTag(holder);
holder.SomeButton = view.findViewById(R.id.some_button);
/* Other views. */
}
else {
view = (ListItemWithSideButton) convertView;
holder = (ViewHolder) convertView.getTag();
}
/* Get the item. */
final String name = getItem(position);
/* Update the data of the view with the new information. */
holder.SomeButton.setText(name);
return view;
}
static class ViewHolder {
Button SomeButton;
/* Other views. */
}
}
現在、私はあなたに与えるための参照を持っていませんが、これはあなたが望むものを得るためにあなたがしているかもしれないことです:
XMLにListViewがある可能性があるため、コードでListViewオブジェクトをインスタンス化します。
ListView myList = (ListView)findViewById(R.id.myListView)
それへの参照を取得したらすぐに、BaseAdapterを拡張するカスタムクラスを作成します。
このクラスをActivityクラス内に配置して、Activityが保持するすべてのデータにアクセスできるようにすることをお勧めします。
BaseAdapterを拡張している間、物事を機能させるためにあなたがしなければならないことがいくつかあります。
以下でそれらすべてを説明しますが、BaseAdapterのgetView()メソッドを実装することが最も重要です。このメソッドは、ListViewが行を描画するたびにランタイムシステムによって呼び出されます。
したがって、このメソッド内ですべてを実行する必要があります。つまり、単一の行に対して実行する必要があります。
以下のコードを見つけてください。
Class myListActivity extends Activity{
... some code here...
public void onCreate(Bundle savedInstanceState){
.....
myList.setAdapter(new myCustomAdapter());
.....
}
/**
*Custom Adapter class for the ListView containing data
*/
class myCustomAdapter extends BaseAdapter{
TextView userName;
/**
* returns the count of elements in the Array that is used to draw the text in rows
* @see android.widget.Adapter#getCount()
*/
@Override
public int getCount() {
//return the length of the data array, so that the List View knows how much rows it has to draw
return DataArr.length;
}
/**
* @param position The position of the row that was clicked (0-n)
* @see android.widget.Adapter#getItem(int)
*/
@Override
public String getItem(int position) {
return null;
}
/**
* @param position The position of the row that was clicked (0-n)
* @see android.widget.Adapter#getItemId(int)
*/
@Override
public long getItemId(int position) {
return position;
}
/**
* Returns the complete row that the System draws.
* It is called every time the System needs to draw a new row;
* You can control the appearance of each row inside this function.
* @param position The position of the row that was clicked (0-n)
* @param convertView The View object of the row that was last created. null if its the first row
* @param parent The ViewGroup object of the parent view
* @see android.widget.Adapter#getView(int, android.view.View, android.view.ViewGroup)
*/
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
final int pos = position;
if(row == null){
//getting custom layout to the row
LayoutInflater inflater=getLayoutInflater();
row=inflater.inflate(R.layout.list_row, parent, false);
}
//get the reference to the textview of your row. find the item with row.findViewById()
userName= (TextView)row.findViewById(R.id.user_name);
userName.setText(DataArr[position]);
return row; //the row that ListView draws
}
}
}
お役に立てば幸いです。
別のレイアウトファイルで行のレイアウトを作成することを忘れないでください。
さらに深く知りたい場合は、CommonsGuyのWebサイトでこのリンクを試してください。これは彼の素晴らしい本の抜粋であり、特にListViewカスタムアダプタを扱っています。
編集:これとサンプルプロジェクトについての私のブログ投稿もあります:http ://thetechnib.blogspot.com/2010/12/android-tutorial-custom-adapter-for.html