如何在一个ListView中的每个条目显示复选框 [英] How to display checkbox on every entry in a listView
问题描述
从这个活动,我从文本字段获取文本,并在ListView显示。
现在我想以添加在ListView小区的每个条目的复选框,也想知道如何显示在一个单一的ListView细胞多个文本。
帮助code将AP preciated。
这里是我的code ....
公共类AfterRegister扩展AppCompatActivity
{
ListView控件的ListView;
EDITTEXT的EditText;
按钮insertItemButton;
ArrayList的<串GT; ArrayList的=新的ArrayList<串GT;();
ArrayAdapter<串GT;适配器;
复选框复选框; StoreRegistrationDataBase storeRegistrationDataBase; @覆盖
保护无效的onCreate(捆绑savedInstanceState)
{
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_after_register);
storeRegistrationDataBase =新StoreRegistrationDataBase(本);
storeRegistrationDataBase = storeRegistrationDataBase.open(); 复选框=(复选框)findViewById(R.id.checkbox); insertItemButton =(按钮)findViewById(R.id.button4);
insertItemButton.setOnClickListener(新View.OnClickListener()
{
@覆盖
公共无效的onClick(视图v)
{
EDITTEXT =(EditText上)findViewById(R.id.editText2);
ListView控件=(ListView控件)findViewById(R.id.listView);
。字符串getEditTextString = editText.getText()的toString(); 如果(isAlphaNumeric(getEditTextString))
{
如果(!getEditTextString.equals()) {
arrayList.add(getEditTextString); 适配器=新ArrayAdapter<串GT;(getBaseContext(),R.layout.text_view_layout,R.id.achView1,ArrayList的);
listView.setAdapter(适配器); adapter.notifyDataSetChanged();
editText.setText(); }
其他
{
Toast.makeText(AfterRegister.this,你不能插入空场,Toast.LENGTH_SHORT).show();
}
}
其他
{
Toast.makeText(AfterRegister.this,删除空间,Toast.LENGTH_SHORT).show();
}
}
});
listView.setOnTouchListener(新View.OnTouchListener()
{
@覆盖
公共布尔onTouch(视图V,MotionEvent事件)
{ 返回false;
}
});
} 公共布尔isAlphaNumeric(String s)将
{
字符串模式=^ [A-ZA-Z0-9] * $;
如果(s.matches(模式))
{
返回true;
}
返回false;
}
}
您必须使用一个BaseAdapter和一些getter / setter方法在你的列表视图中的每一项添加多个文字/图像/其他UI元素。
您必须实施多事情得到这样的结果。在这里,他们是 -
- 为你的ListView的每个项目创建一个自定义布局。
listview_item_layout.xml
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:工具=http://schemas.android.com/tools
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:方向=垂直> <的TextView
机器人:ID =@ + ID / layout_textview1
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:TEXTSIZE =15sp
机器人:layout_marginRight =5dip
机器人:文字样式=大胆/> <的TextView
机器人:ID =@ + ID / layout_textview2
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:TEXTSIZE =15sp
机器人:layout_marginLeft =5dip
机器人:文字样式=大胆/> <复选框
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:ID =@ + ID /复选框
机器人:文字=测试/>< / LinearLayout中>
<醇开始=2>
ListRowItem.java
公共类ListRowItem实现Serializable {
串载体,数量;公共字符串getCarrier(){
返回载体;
}公共字符串getNumber(){
返回数;
}公共无效setCarrier(字符串ba_carrier){
载体= ba_carrier;
}公共无效setNumber(字符串ba_number){
数= ba_number;
}
}
<醇开始=3>
创建一个自定义类,并扩展了BaseAdapter类。
公共类MyBaseAdapter延伸BaseAdapter {公共语境ba_context;
公众的ArrayList&LT; ListRowItem&GT;列表项=新的ArrayList&LT;&GT;();
公共LayoutInflater吹气;
ListRowItem currentlistitem;公共MyBaseAdapter(上下文ma_context,ArrayList的&LT; ListRowItem&GT; ma_listitem){
超();
this.ba_context = ma_context;
this.listitem = ma_listitem;吹气=(LayoutInflater)ba_context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}@覆盖
公众诠释的getCount(){
返回this.listitem.size();
}@覆盖
公共对象的getItem(INT位置){
返回this.listitem.get(位置);
}@覆盖
众长getItemId(INT位置){
返回(长)的位置;
}@覆盖
公共查看getView(INT位置,查看convertView,父母的ViewGroup){
查看VI = convertView;如果(convertView == NULL)
VI = inflater.inflate(R.layout.listview_item_layout,父母,假);TextView的载体=(TextView中)vi.findViewById(R.id.layout_textview1);
TextView的数=(TextView中)vi.findViewById(R.id.layout_textview2);currentlistitem = listitem.get(位置);串str_carrier = currentlistitem.getCarrier();
串str_number = currentlistitem.getNumber();carrier.setText(str_carrier);
number.setText(str_number);返回VI;
}
}
最后,填充您的ArrayList和你MainActivity设定的适配器。
的ArrayList&LT; ListRowItem&GT;列表项=新的ArrayList&LT;&GT;();
上下文的背景下= TestActivity.this;
MyBaseAdapter baseAdapter;ListRowItem LR =新ListRowItem();
lr.setNumber(数);
lr.setCarrier(载体);listitem.add(LR);baseAdapter =新MyBaseAdapter(背景下,列表项);的setContentView(R.layout.activity_test);
ListView控件=(ListView控件)findViewById(R.id.list_view);
listView.setAdapter(baseAdapter);
希望这有助于!
From this Activity i get text from textField and display it in a ListView. Now i want to to add check box on every entry in a listView Cell and also like to know how to display more than one text in a single ListView Cell. Help with code will be appreciated. Here is my code ....
public class AfterRegister extends AppCompatActivity
{
ListView listView;
EditText editText;
Button insertItemButton;
ArrayList<String> arrayList = new ArrayList<String>();
ArrayAdapter<String> adapter;
CheckBox checkBox;
StoreRegistrationDataBase storeRegistrationDataBase;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_after_register);
storeRegistrationDataBase = new StoreRegistrationDataBase(this);
storeRegistrationDataBase = storeRegistrationDataBase.open();
checkBox = (CheckBox) findViewById(R.id.checkbox);
insertItemButton = (Button) findViewById(R.id.button4);
insertItemButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
editText = (EditText) findViewById(R.id.editText2);
listView = (ListView) findViewById(R.id.listView);
String getEditTextString = editText.getText().toString();
if(isAlphaNumeric(getEditTextString))
{
if(!getEditTextString.equals(""))
{
arrayList.add(getEditTextString);
adapter = new ArrayAdapter<String>(getBaseContext(), R.layout.text_view_layout, R.id.achView1, arrayList);
listView.setAdapter(adapter);
adapter.notifyDataSetChanged();
editText.setText("");
}
else
{
Toast.makeText(AfterRegister.this, "You can not insert empty field", Toast.LENGTH_SHORT).show();
}
}
else
{
Toast.makeText(AfterRegister.this, "Remove Space", Toast.LENGTH_SHORT).show();
}
}
});
listView.setOnTouchListener(new View.OnTouchListener()
{
@Override
public boolean onTouch(View v, MotionEvent event)
{
return false;
}
});
}
public boolean isAlphaNumeric(String s)
{
String pattern= "^[a-zA-Z0-9]*$";
if(s.matches(pattern))
{
return true;
}
return false;
}
}
You have to use a BaseAdapter and some Getter/Setter methods to add multiple texts/images/other UI elements in each item of your list view.
You have to implement multiple things to get this result. Here they are --
- Create a Custom Layout for each item of your ListView.
listview_item_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/layout_textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:layout_marginRight="5dip"
android:textStyle="bold"/>
<TextView
android:id="@+id/layout_textview2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:layout_marginLeft="5dip"
android:textStyle="bold"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/checkbox"
android:text="Test"/>
</LinearLayout>
- Create a custom class and add some Getter/Setter methods.
ListRowItem.java
public class ListRowItem implements Serializable{
String carrier,number;
public String getCarrier(){
return carrier;
}
public String getNumber(){
return number;
}
public void setCarrier(String ba_carrier){
carrier = ba_carrier;
}
public void setNumber(String ba_number){
number = ba_number;
}
}
Create a custom class and extend the BaseAdapter class.
public class MyBaseAdapter extends BaseAdapter { public Context ba_context; public ArrayList<ListRowItem> listitem = new ArrayList<>(); public LayoutInflater inflater; ListRowItem currentlistitem; public MyBaseAdapter(Context ma_context, ArrayList<ListRowItem> ma_listitem) { super(); this.ba_context = ma_context; this.listitem = ma_listitem; inflater = (LayoutInflater) ba_context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); } @Override public int getCount() { return this.listitem.size(); } @Override public Object getItem(int position) { return this.listitem.get(position); } @Override public long getItemId(int position) { return (long) position; } @Override public View getView(int position, View convertView, ViewGroup parent) { View vi = convertView; if (convertView == null) vi = inflater.inflate(R.layout.listview_item_layout, parent, false); TextView carrier = (TextView) vi.findViewById(R.id.layout_textview1); TextView number = (TextView) vi.findViewById(R.id.layout_textview2); currentlistitem = listitem.get(position); String str_carrier = currentlistitem.getCarrier(); String str_number = currentlistitem.getNumber(); carrier.setText(str_carrier); number.setText(str_number); return vi; } }
Finally, populate your ArrayList and set the Adapter in your MainActivity.
ArrayList<ListRowItem> listitem = new ArrayList<>(); Context context = TestActivity.this; MyBaseAdapter baseAdapter; ListRowItem lr = new ListRowItem(); lr.setNumber(number); lr.setCarrier(carrier); listitem.add(lr); baseAdapter = new MyBaseAdapter(context,listitem); setContentView(R.layout.activity_test); listView = (ListView) findViewById(R.id.list_view); listView.setAdapter(baseAdapter);
Hope this helps!!
这篇关于如何在一个ListView中的每个条目显示复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!