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

当您单击按钮时,将显示带有所选项目列表的吐司消息。您可以根据需要修改以上内容。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值