本文介绍了如何禁用列表视图的项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我这是通过从数据库中记录填充的列表视图。现在,我必须作出一些记录可见,但无法进行选择,我怎样才能做到这一点?

这是我的code

 公共类SomeClass的扩展ListActivity {
    私有静态列表<字符串>产品;
    私人DataHelper DH;
    公共无效的onCreate(包savedInstanceState){
        DH =新DataHelper(本);
        产品= dh.GetMyProducts(); / *返回一个列表<字符串> * /
        super.onCreate(savedInstanceState);
        setListAdapter(新ArrayAdapter<字符串>(这一点,R.layout.myproducts,产品));
        ListView的LV = getListView();
        lv.setTextFilterEnabled(真正的);
        lv.setOnItemClickListener(新OnItemClickListener(){
            @覆盖
            公共无效onItemClick(适配器视图<>为arg0,查看ARG1,INT ARG2,长ARG3){
                // TODO自动生成方法存根
                Toast.makeText(getApplicationContext(),((TextView的)ARG1).getText(),Toast.LENGTH_SHORT).show();
            }
        });
    }
}
 

布局文件myproducts.xml如下:

 < XML版本=1.0编码=UTF-8&GT?;
< TextView中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    机器人:填充=10dp
    机器人:TEXTSIZE =16SP>
< / TextView的>
 

解决方案

请你自己ArrayAdapter有AreAllItemsEnabled()返回false,并定义isEnabled(int位置)在你的数据集中返回true / false来一个给定的项目。

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>
解决方案

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.

这篇关于如何禁用列表视图的项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 02:52