2011-05-29 147 views
1

所以我有一个类从数据库获取项目,然后将它们放入布局并膨胀。这循环遍历数据库中的每个项目,为每个项目添加一个新的布局。这最终会延伸过视口,并需要一个滚动条,我认为这将是一个ScrollView。我尝试了许多不同的方式,无法滚动浏览整个内容。请指教。Android:滚动滚动视图内的线性视图

的Java:

View myView = linflater.inflate(R.layout.myviews, null); 

TextView tvQuote = (TextView) myView.findViewById(R.id.lblQuote); 
//Set an id to know which quote is in use 
tvQuote.setId(i); 
TextView tvShared = (TextView) myView.findViewById(R.id.lblShared); 
TextView tvSaid = (TextView) myView.findViewById(R.id.lblSaid); 

//Change name dynamically 
tvQuote.setText((strQuote).toString()); 
tvShared.setText(("Shared by: " + strFName + " " + strLInitial).toString()); 
tvSaid.setText(("Said by: " + strFNameSaid + " " + strLInitialSaid).toString()); 

的main.xml:

<?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout 
     android:orientation="vertical" 
     android:id="@+id/myMainLayout" 
     android:layout_width="fill_parent" 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_height="wrap_content"> 
    <TextView 
     android:layout_height="wrap_content" 
     android:layout_width="fill_parent" 
     android:text="Quoter" 
     android:id="@+id/lblTitle" 
     android:textSize="16px" 
     android:padding="5px" 
     android:textStyle="bold" 
     android:gravity="center_horizontal"> 
    </TextView> 
</LinearLayout> 

myviews.xml:

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


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

      <TextView 
       android:id="@+id/lblQuote" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_weight="1"/> 
     </LinearLayout> 

     <LinearLayout 
      android:orientation="horizontal" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1"> 

      <TextView 
       android:id="@+id/lblShared" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_weight="1"/> 
      <TextView 
       android:id="@+id/lblSaid" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_weight="1"/> 
     </LinearLayout> 
     <LinearLayout 
      android:orientation="vertical" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1"> 
      <TextView 
      android:text=" " 
      android:textSize="1pt" 
      android:background="#6F7285" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1"/>  
     </LinearLayout> 
</LinearLayout> 

会是什么,通过这些充气布局滚动的最佳解决方案?

+0

我实际上发现将ScrollView添加到main.xml是修复。感谢所有的帮助,虽然家伙。我仍然想知道我是否做错了。 – O1DMBFan 2011-05-29 20:28:43

回答

0

我实际上发现将ScrollView添加到main.xml是修复。感谢所有的帮助,虽然家伙。我仍然想知道我是否做错了。

0

您在myviews.xml中使用了太多的线性布局。这应该为你做的伎俩:

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"> 
    <RelativeLayout 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1"> 
     <TextView 
      android:id="@+id/lblQuote" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="text" 
      /> 
     <TextView 
      android:id="@+id/lblShared" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@id/lblQuote" 
      android:text="text" 
      /> 
     <TextView 
      android:id="@+id/lblSaid" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@id/lblQuote" 
      android:layout_alignParentRight="true" 
      android:text="text" 
      /> 
     <TextView 
      android:text="text " 
      android:background="#6F7285" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_below="@id/lblShared" 
      />  
    </RelativeLayout> 
</ScrollView> 

我添加了一些占位符文本,所以你可以看到它。删除你不需要的东西。

+0

尝试此操作时,我会收到一个力量。 – O1DMBFan 2011-05-29 18:03:50

+0

强制关闭不能归因于xml。 logcat说什么? – Aleadam 2011-05-30 04:51:22

4

这听起来像你做了太多的工作。如果你的数据在SQLite数据库,那么你可以使用你的ListView一个SimpleCursorAdapter将做繁重的你:

public class MyListActivity extends ListActivity { 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     // The my_list_activity layout is the container for each 
     // item in the list. 
     setContentView(R.layout.my_list_activity); 

     // Create a database cursor for the data you want to query. 
     // Assumes your data is accessible via a ContentProvider. 

     // Note: db.CONTENT_URI is whatever URI your ContentProvider uses, 
     // and db.COL1 etc are constants that would name the columns you want. 
     cursor = getContentResolver().query(db.CONTENT_URI, 
      new String[] { db._ID, db.COL1, db.COL2, db.COL3 }, 
      null, null, null); 

     // Bind the value in db.COL1 to the resource with the id text1, and so 
     // on for the other two columns. 
     // 
     // Assumes that the view has TextViews with IDs text1, text2, and text3. 
     String[] from= new String[] { db.COL1, db.COL2, db.COL3 }; 
     int[] to = new int[] { R.id.text1, R.id.text2, R.id.text3 }; 

     // Create the adapter, bound to the cursor, layout, and the mapping from 
     // column name to TextView ID. 
     // 
     // Assumes a layout called my_list_activity_item exists, with at least 
     // 3 views called text1, text2, and text3. 
     SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, 
      R.layout.my_list_activity_item, cursor, from, to); 

     setListAdapter(mAdapter); 
    } 
} 

如果你的数据是不是在SQLite数据库然后查看派生其他类从android.widget.BaseAdapter看它们是否适合你。

+0

如果您之前没有使用过ListView,可能需要观看此视频:http://www.google.com/events/io/2010/sessions/world-of-listview-android.html它们相当从你的问题的方法概念上有点不同。 – adamp 2011-05-29 17:26:54

+0

我的数据位于Web服务器上的MS SQL数据库中。 – O1DMBFan 2011-05-29 18:03:39

+0

您仍然可以使用Content Provider作为Web服务的代理。 O'Reilly提供的这种材料在一定程度上涵盖了一些细节 - http://programming-android.labs.oreilly.com/ch13.html – Nik 2011-05-29 20:46:50

0

我认为最简单的答案就是你需要围绕所有这些LinearLayout s的ScrollView,所以你可以实际滚动(你的第一个LinearLayout有height = wrap_content)。长版本是,你可能做错了,你应该使用ListViewAdapter来代替。

+0

内容实际上只是文本,不可选择,不易处理。只是文字。 ListView仍然是这样做的正确方法吗?如果是的话有什么好处? – O1DMBFan 2011-05-29 19:29:08

+0

没问题,它只是一个演示标准。可伸缩性是一个很大的问题。您的数据库可以增长,而无需重新编码所有内容。 – dmon 2011-05-30 00:33:45