日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

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

發(fā)布時(shí)間:2024/9/19 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android自定义listview 显示数组,android中使用arrayadapter类的自定义列表视图 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

從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" />

以及復(fù)選框的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" />

當(dāng)您單擊按鈕時(shí),將顯示帶有所選項(xiàng)目列表的吐司消息。您可以根據(jù)需要修改以上內(nèi)容。

總結(jié)

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

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。