如何禁用列表视图中的项目?
我有一个列表视图,它是通过数据库中的记录填充的。 现在我必须使一些记录可见但不可选择, 我怎样才能做到这一点?
这是我的代码
public class SomeClass extends ListActivity {
private static List<String> products;
private DataHelper dh;
public void onCreate(Bundle savedInstanceState) {
dh = new DataHelper(this);
products = dh.GetMyProducts(); /* Returns a List<String>*/
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this, R.layout.myproducts, products));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener( new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), ((TextView) arg1).getText(), Toast.LENGTH_SHORT).show();
}
});
}
}
布局文件myproducts.xml如下:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="16sp">
</TextView>
I have a list view which is populated via records from the database.
Now i have to make some records visible but unavailable for selection,
how can i achieve that?
here's my code
public class SomeClass extends ListActivity {
private static List<String> products;
private DataHelper dh;
public void onCreate(Bundle savedInstanceState) {
dh = new DataHelper(this);
products = dh.GetMyProducts(); /* Returns a List<String>*/
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this, R.layout.myproducts, products));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener( new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), ((TextView) arg1).getText(), Toast.LENGTH_SHORT).show();
}
});
}
}
The layout file myproducts.xml is as follows:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="16sp">
</TextView>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
创建您自己的 ArrayAdapter 子类,该子类具有 AreAllItemsEnabled()返回 false,并定义 isEnabled(intposition)为数据集中的给定项目返回 true/false。
Make your own subclass of ArrayAdapter that has AreAllItemsEnabled() return false, and define isEnabled(int position) to return true/false for a given item in your data set.