【问题标题】:Unable to see Search Box (EditText) when using ListView in Android在 Android 中使用 ListView 时看不到搜索框(EditText)
【发布时间】:2014-06-21 06:29:19
【问题描述】:

我正在尝试使用搜索框实现自定义 ListView(即带有自定义适配器的 ListView),为此我编写了以下 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="match_parent"
    android:orientation="vertical"
    tools:context=".CustomListViewAndroidExample" >

    <EditText
        android:id="@+id/txtInputSearch"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="Search item.."
        android:inputType="textVisiblePassword" />

    <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginTop="20dp"
        android:layout_weight="1" />

</LinearLayout>

我的 MenuListActivity 代码是---

 public class MenuListActivity extends Activity
    {
        ListView list;
        TextView txtInputSearch;
        MenuListAdapter adapter;
        public  MenuListActivity CustomListView = null;
        public  ArrayList<ListModel> CustomListViewValuesArr = new ArrayList<ListModel>();
        @Override
        protected void onCreate(Bundle savedInstanceState) 
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_menu_list);

            CustomListView = this;


            setListData();

            Resources res =getResources();
            txtInputSearch=(TextView)findViewById(R.id.txtInputSearch);
            list= ( ListView )findViewById( R.id.list );  // List defined in XML ( See Below )


            adapter=new MenuListAdapter( CustomListView, CustomListViewValuesArr,res );
            list.setAdapter( adapter );        

            txtInputSearch.addTextChangedListener(new TextWatcher() 
            {

                @Override
                public void afterTextChanged(Editable arg0)
                {
                    // TODO Auto-generated method stub
                    String text = txtInputSearch.getText().toString().toLowerCase(Locale.getDefault());
                    adapter.filter(text);
                }

                @Override
                public void beforeTextChanged(CharSequence arg0, int arg1,int arg2, int arg3)
                {
                    // TODO Auto-generated method stub
                }

                @Override
                public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) 
                {
                    // TODO Auto-generated method stub
                }
            });
        }   


    public void setListData()
    {

        for (int i = 0; i < 11; i++) 
        {

            final ListModel objListModel = new ListModel();


            objListModel.setMenu("Company "+i);
            objListModel.setPrice("com.androidexample.customlistview:drawable/rihanna");
              // sched.setUrl("http:\\www."+i+".com");

            CustomListViewValuesArr.add(objListModel);
        }

    }


    public void onItemClick(int mPosition)
    {
        ListModel tempValues = ( ListModel ) CustomListViewValuesArr.get(mPosition);

       // SHOW ALERT                  

        Toast.makeText(CustomListView,"Clicked",Toast.LENGTH_LONG).show();
    }

}

在下面查看我的适配器类--

public class MenuListAdapter extends BaseAdapter implements OnClickListener
{

    private Activity activity;
    private ArrayList data;
    private ArrayList<ListModel> arrayList;
    private static LayoutInflater inflater=null;
    public Resources res;
    ListModel tempValues=null;
    int i=0;

    public MenuListAdapter(Activity paramActivity, ArrayList paramDataList, Resources paramResource) 
    {
        activity = paramActivity;
        data=paramDataList;
        res = paramResource;
        arrayList=new ArrayList<ListModel>();
        arrayList.addAll(data);

        inflater = ( LayoutInflater )activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }

    public int getCount()
    {

        if(data.size()<=0)
            return 1;
        return data.size();
    }

    public Object getItem(int position) 
    {
        return position;
    }

    public long getItemId(int position)
    {
        return position;
    }

    public static class ViewHolder
    {         
        public TextView item_name;
        public TextView item_price;       
    }

    public View getView(int position, View convertView, ViewGroup parent) 
    {

        View vi = convertView;
        ViewHolder holder;

        if(convertView==null)
        {

            vi = inflater.inflate(R.layout.menu_list_row, null);             
            holder = new ViewHolder();
            holder.item_name = (TextView) vi.findViewById(R.id.item_name);
            holder.item_price=(TextView)vi.findViewById(R.id.item_price);
           // holder.image=(ImageView)vi.findViewById(R.id.list_image);

            vi.setTag( holder );
        }
        else 
            holder=(ViewHolder)vi.getTag();

        if(data.size()<=0)
        {
            holder.item_name.setText("No Data");

        }
        else
        {
            tempValues=null;
            tempValues = ( ListModel ) data.get( position );


             holder.item_name.setText( tempValues.getMenu() );
             holder.item_price.setText( tempValues.getPrice());
             /* holder.image.setImageResource(
                          res.getIdentifier(
                          "com.androidexample.customlistview:drawable/"+tempValues.getImage()
                          ,null,null));*/                           

             vi.setOnClickListener(new OnItemClickListener( position ));
        }
        return vi;
    }

    public void onClick(View v) 
    {
            Log.v("CustomAdapter", "=====Row button clicked=====");
    }

    private class OnItemClickListener  implements OnClickListener
    {           
        private int mPosition;

        OnItemClickListener(int position)
        {
             mPosition = position;
        }

        public void onClick(View arg0) 
        {


          MenuListActivity sct = (MenuListActivity)activity;   

            sct.onItemClick(mPosition);
        }               
    }  

 // Filter Class
    public void filter(String charText)
    {
        charText = charText.toLowerCase(Locale.getDefault());
        data.clear();
        if (charText.length() == 0) 
        {
            data.addAll(arrayList);
        } 
        else 
        {           
            for (ListModel lm : arrayList)
            {
                if (lm.getMenu().toLowerCase(Locale.getDefault()).contains(charText)) 
                {
                    data.add(lm);
                }
            }
        }
        notifyDataSetChanged();
    }
}

在 设计模式,当我点击 Graphical Layout 显示正确/预期的视图(即搜索框到顶部和 ListView 下面到搜索框)但是当我运行我的应用程序时,它只显示 ListView 和 SearchBox (EditText) 是不可见的。请帮忙。

【问题讨论】:

    标签: android listview android-listview


    【解决方案1】:

    将列表视图布局高度更改为android:layout_height="0dp" 而不是android:layout_height="fill_parent"

    <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="match_parent"
        android:orientation="vertical"
        tools:context=".CustomListViewAndroidExample" >
    
        <EditText
            android:id="@+id/txtInputSearch"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:hint="Search item.."
            android:inputType="textVisiblePassword" />
    
        <ListView
            android:id="@+id/list"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_marginTop="20dp"
            android:layout_weight="1" />
    
    </LinearLayout>
    

    编辑使用RelativeLayout而不是LinearLayout作为

    <?xml version="1.0" encoding="utf-8"?>
    
        <RelativeLayout 
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:padding="5dp">
    
            <EditText  
                android:id="@+id/txtInputSearch" 
                android:layout_width="fill_parent" 
                android:layout_height="wrap_content" 
                android:layout_marginLeft="5dp" 
                android:completionThreshold="1" 
                android:singleLine="true"/> 
    
            <ListView  
                android:id="@+id/list"
                android:layout_width="fill_parent" 
                android:layout_height="fill_parent" 
                android:background="#000000" 
                android:layout_weight="1" 
                android:layout_below="@+id/txtInputSearch"
                android:drawSelectorOnTop="false"/> 
    
        </RelativeLayout>
    

    编辑

    在你的代码中使用EditText而不是TextView,所以改变

    TextView txtInputSearch;
    

    到

    EditText txtInputSearch;
    

    还有

    txtInputSearch=(TextView)findViewById(R.id.txtInputSearch);
    

    到

    txtInputSearch=(EditText )findViewById(R.id.txtInputSearch);
    

    【讨论】:

    • @namo 然后从这个布局中删除 android:layout_weight="1" 并添加 android:layout_height="wrap_content" 而不是 android:layout_height="fill_parent"
    • @Namo 检查已编辑的答案,如果有效,请回复。
    • @Namo 尝试相对布局。请参阅编辑后的答案。
    • @Namo 你用的是相对布局吗,我也编辑了。
    • 是的,即使我复制了你的 xml。
    【解决方案2】:

    问题是您在 ListView 中添加了一个权重,这将占用您布局的所有空间,而不是在其上放置任何权重,只需简单地包裹其高度并删除 android:layout_weight="1"

    样本:

    <?xml version="1.0" encoding="utf-8"?>
    <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="match_parent"
        android:orientation="vertical"
        tools:context=".CustomListViewAndroidExample" >
    
        <EditText
            android:id="@+id/txtInputSearch"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Search item.."
            android:inputType="textVisiblePassword" />
    
        <ListView
            android:id="@+id/list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp" />
    
    </LinearLayout>
    

    问题:

    txtInputSearch=(TextView)findViewById(R.id.txtInputSearch);
    

    您将其投射到 TextView 而不是 EditText。

    改成这样:

    EditText txtInputSearch;
    
    txtInputSearch=(EditText)findViewById(R.id.txtInputSearch);
    

    【讨论】:

    • Algonquin 我已删除 android:layout_weight="1" 但仍然无法正常工作。
    • @Namo 你复制了我上面的xml?
    • 是的,我复制了你的 xml。
    • 哪个代码..?我的意思是适配器类或 MenuList Activity..?
    • @Namo 在您初始化列表视图和编辑文本的主要活动中
    猜你喜欢
    • 2012-06-04
    • 1970-01-01
    • 2017-11-21
    • 1970-01-01
    • 1970-01-01
    • 2012-11-30
    • 1970-01-01
    • 2016-04-30
    • 2015-09-20
    相关资源
    最近更新 更多