【发布时间】:2016-03-03 00:18:21
【问题描述】:
从此活动中,我从 textField 获取文本并将其显示在 ListView 中。 现在我想在 listView 单元格中的每个条目上添加复选框,并且还想知道如何在单个 ListView 单元格中显示多个文本。 代码帮助将不胜感激。 这是我的代码....
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;
}
}
【问题讨论】:
-
您需要编写自定义列表视图适配器,而不是数组适配器。查看有关适配器的文档developer.android.com/reference/android/widget/Adapter.html
-
Hi Muhammad Khan as @Ameya Kulkarni 建议您需要编写一个自定义适配器来执行此操作。 Example我想这就是你想要的
-
在使用自定义视图和适配器(选择框架,使用“完整搜索”字段)androidxref.com/6.0.1_r10/… 时检查源代码是非常宝贵的
-
在 listview item xml 布局中,您必须在 Adapter 中创建一个复选框列表后插入一个复选框,您将调用并浏览每个复选框并设置 visiblity = true