日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

android自定义listview 显示数组,android中使用arrayadapter类的自定义列表视图

發布時間:2024/9/19 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android自定义listview 显示数组,android中使用arrayadapter类的自定义列表视图 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

從https://groups.google.com/forum/?fromgroups#!topic/android-developers/No0LrgJ6q2M繪制

public class MainActivity extends Activity implements AdapterView.OnItemClickListener {

String[] GENRES = new String[] {"Action", "Adventure", "Animation", "Children", "Comedy", "Documentary", "Drama", "Foreign", "History", "Independent", "Romance", "Sci-Fi", "Television", "Thriller"};

private CheckBoxAdapter mCheckBoxAdapter;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

final ListView listView = (ListView) findViewById(R.id.lv);

listView.setItemsCanFocus(false);

listView.setTextFilterEnabled(true);

listView.setOnItemClickListener(this);

mCheckBoxAdapter = new CheckBoxAdapter(this, GENRES);

listView.setAdapter(mCheckBoxAdapter);

Button b = (Button) findViewById(R.id.button1);

b.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

StringBuilder result = new StringBuilder();

for (int i = 0; i < GENRES.length; i++) {

if (mCheckBoxAdapter.mCheckStates.get(i) == true) {

result.append(GENRES[i]);

result.append("\n");

}

}

Toast.makeText(MainActivity.this, result, 1000).show();

}

});

}

public void onItemClick(AdapterView parent, View view, int position, long id) {

mCheckBoxAdapter.toggle(position);

}

class CheckBoxAdapter extends ArrayAdapter implements CompoundButton.OnCheckedChangeListener {

LayoutInflater mInflater;

TextView tv1, tv;

CheckBox cb;

String[] gen;

private SparseBooleanArray mCheckStates;

private SparseBooleanArray mCheckStates;

CheckBoxAdapter(MainActivity context, String[] genres) {

super(context, 0, genres);

mCheckStates = new SparseBooleanArray(genres.length);

mInflater = (LayoutInflater) MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

gen = genres;

}

@Override

public int getCount() {

// TODO Auto-generated method stub

return gen.length;

}

}

}

activity_main.xml

xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="fill_parent">

android:id="@+id/lv"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_above="@+id/button1"/>

android:id="@+id/button1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentBottom="true"

android:layout_centerHorizontal="true"

android:text="Button" />

以及復選框的XML文件:

android:layout_width="match_parent"

android:layout_height="match_parent" >

android:id="@+id/textView1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentLeft="true"

android:layout_alignParentTop="true"

android:layout_marginLeft="15dp"

android:layout_marginTop="34dp"

android:text="TextView" />

android:id="@+id/checkBox1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentRight="true"

android:layout_below="@+id/textView1"

android:layout_marginRight="22dp"

android:layout_marginTop="23dp" />

當您單擊按鈕時,將顯示帶有所選項目列表的吐司消息。您可以根據需要修改以上內容。

總結

以上是生活随笔為你收集整理的android自定义listview 显示数组,android中使用arrayadapter类的自定义列表视图的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。