失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > android自定义listview 显示数组 android中使用arrayadapter类的自定义列表视图

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

时间:2018-12-28 22:40:15

相关推荐

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

从/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="/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类的自定义列表视图》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。