动态列表查看Android

时间:2014-01-10 17:17:57

标签: android android-listview android-sqlite

我正在尝试根据标题,作者或ISBN的搜索查询创建书籍的动态列表视图。

我可以显示一个项目,但无论出于何种原因,我都看不到任何与搜索查询匹配的其他项目。

主要方法

    // Set up search array
    int i = 0;
    String searchEntries[] = InventoryAdapter.getTitleAndAuthorByISBN(isbn);
    searchArray.add(new InventoryItem(searchQuery, searchEntries));


    // add data in custom adapter
    adapter = new CustomAdapter(this, R.layout.list, searchArray);
    ListView dataList = (ListView) findViewById(R.id.list);
    dataList.setAdapter(adapter);

CustomAdapter

public class CustomAdapter extends ArrayAdapter<InventoryItem> {
   Context context;
   int layoutResourceId;
   LinearLayout linearMain;
   ArrayList<InventoryItem> data = new ArrayList<InventoryItem>();

   public CustomAdapter(Context context, int layoutResourceId,
                 ArrayList<InventoryItem> data) {
          super(context, layoutResourceId, data);
          this.layoutResourceId = layoutResourceId;
          this.context = context;
          this.data = data;
   }

   @Override
   public View getView(int position, View convertView, ViewGroup parent) {
          View row = convertView;

          if (row == null) {
                 LayoutInflater inflater = ((Activity) context).getLayoutInflater();
                 row = inflater.inflate(layoutResourceId, parent, false);

                 linearMain = (LinearLayout) row.findViewById(R.id.lineraMain);

                 InventoryItem myItem = data.get(position);
                 for (int j = 0; j < myItem.getDetails().length; j++) {
                       TextView label = new TextView(context);
                       label.setText(myItem.details[j]);
                       linearMain.addView(label);
                 }
          }

          return row;

   }

}

InventoryItem

public class InventoryItem {

   public String query;
   public String details[];

   public InventoryItem(String query, String[] details) {
          super();
          this.query = query;
          this.details = details;
   }
   public String getQuery() {
          return query;
   }
   public void setQuery(String query) {
          this.query = query;
   }
   public String[] getDetails() {
          return details;
   }
   public void setDetails(String[] details) {
          this.details = details;
   }

}

list.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:id="@+id/lineraMain"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:padding="10dp" >

</LinearLayout>

主要方法xml

<LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="95"
            android:orientation="vertical">


             <ScrollView  
                    android:layout_width="fill_parent" 
                    android:layout_height="fill_parent" 
                    android:overScrollMode="always" 
                    android:isScrollContainer="true" 
                    android:scrollbarAlwaysDrawVerticalTrack="true" 
                    android:scrollbarStyle="outsideInset" 
                    android:scrollbars="vertical">

                 <LinearLayout
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:orientation="vertical" >

                         <ListView
                              android:id="@+id/list"
                              android:layout_width="fill_parent"
                              android:layout_height="fill_parent">
                         </ListView>    

                </LinearLayout>

             </ScrollView>

    </LinearLayout>

任何想法我做错了什么?

1 个答案:

答案 0 :(得分:1)

ListView内有ScrollView。不推荐使用不良设计。

http://developer.android.com/reference/android/widget/ScrollView.html

引用文档

  

你永远不应该使用带有ListView的ScrollView,因为ListView   负责自己的垂直滚动。最重要的是,这样做   击败ListView中的所有重要优化以进行处理   使用大型列表,因为它有效地强制ListView显示   它的整个项目列表,用于填充由提供的无限容器   滚动型。

同时将getView更改为

 @Override
 public View getView(int position, View convertView, ViewGroup parent) {
      View row = convertView;

      if (row == null) {
             LayoutInflater inflater = ((Activity) context).getLayoutInflater();
             row = inflater.inflate(layoutResourceId, parent, false);
             linearMain = (LinearLayout) row.findViewById(R.id.lineraMain);
             }

             InventoryItem myItem = data.get(position);
             TextView label = new TextView(context);
             label.setText(myItem.details[positon]);
             linearMain.addView(label);

      return row;

   }

编辑:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:id="@+id/lineraMain"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:padding="10dp" >
    <TextView
       android:id="@+id/tv"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"/>

 </LinearLayout>

然后在getView

@Override
 public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

      if ( convertView== null) {
             LayoutInflater inflater = ((Activity) context).getLayoutInflater();
             convertView = inflater.inflate(layoutResourceId, parent, false);
             holder = ViewHolder();
             holder.tv = (TextView) convertView.findViewById(R.id.tv); 
             convertView.setTag(holder);  
       }
       else
       {
             holder = (ViewHolder) convertView.getTag();  
       } 

             InventoryItem myItem = data.get(position);
             holder.tv.setText(myItem.details[positon]);


      return convertView;

   }

   static class ViewHolder
   {
         TextView tv;
   }