【问题标题】:How to change my ListView to LinearLayout in Android?如何在 Android 中将我的 ListView 更改为 LinearLayout?
【发布时间】:2015-08-18 08:30:18
【问题描述】:

我正在构建一个 Android 应用程序,我从本地数据库中获取数据并将其显示在自定义 ListView 中。

现在的问题是我想使用LinearLayout 代替ListView 作为滚动问题。

在我用于listView 的代码下方。

  1. 活动中的声明 -

    lv = (ListView) findViewById(R.id.listView1);
    
    adapter = new CustomAdapter(this, dbh.fetchAllRec(),
            CursorAdapter.NO_SELECTION);
    lv.setAdapter(adapter);
    
  2. 自定义列表视图 -

    public class CustomAdapter extends CursorAdapter {
    
    ///---  cunstructor ---///
    
    @Override
    public void bindView(View v, Context context, Cursor c) {
    // TODO Auto-generated method stub
    //TextView tvId=(TextView)v.findViewById(R.id.tvId);
    
    //tvId.setText(c.getString(c.getColumnIndex(c.getColumnName(0))));
    //tvId.setText(c.getString(arg0));
    
    TextView tvEmail = (TextView)v.findViewById(R.id.tvEmail);
    tvEmail.setText(c.getString(c.getColumnIndex(c.getColumnName(4))));
    
    TextView tvPword = (TextView)v.findViewById(R.id.tvPword);
    tvPword.setText(c.getString(c.getColumnIndex(c.getColumnName(5))));
    
    TextView tvMobile =(TextView)v.findViewById(R.id.tvMobile);
    String FlatNumber = c.getString(c.getColumnIndex(c.getColumnName(1)));
    String Landmark = c.getString(c.getColumnIndex(c.getColumnName(2)));
    String Address = c.getString(c.getColumnIndex(c.getColumnName(3)));
    tvMobile.setText(FlatNumber+","+Landmark+","+Address);
    
      }
    
    @Override
      public View newView(Context context, Cursor c, ViewGroup parent ) {
        // TODO Auto-generated method stub
      LayoutInflater inflater = LayoutInflater.from(parent.getContext());
      View v = inflater.inflate(R.layout.cust_list, parent, false);
    
      return v;
     }
    
    }
    

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/locationT1"
        android:textSize="14sp" />
    
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:text="@string/locationT2"
        android:textSize="10sp" />
    
    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:layout_marginTop="@dimen/margin_10"
        android:background="@drawable/cornerround">
    
        <ImageView
            android:id="@+id/mapLogo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:contentDescription="@null"
            android:src="@drawable/locationicon" />
    
        <TextView
            android:id="@+id/pickerUserAddress"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginRight="@dimen/margin_50"
            android:layout_toRightOf="@+id/mapLogo"
            android:lines="1"
            android:textSize="@dimen/text_size_14" />
    
        <ImageView
            android:id="@+id/editLocation"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:contentDescription="@null"
            android:src="@drawable/editiconn" />
    </RelativeLayout>
    
    <EditText
        android:id="@+id/pickerUserName"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_marginTop="@dimen/margin_10"
        android:background="@drawable/cornerround"
        android:hint="@string/hintEnterName"
        android:textSize="@dimen/text_size_14" />
    
    <EditText
        android:id="@+id/pickerUserPhoneNumber"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_marginTop="@dimen/margin_10"
        android:background="@drawable/cornerround"
        android:hint="@string/hintEnterPhoneNumber"
        android:inputType="phone"
        android:maxLength="12"
        android:textSize="@dimen/text_size_14" />
    
    <EditText
        android:id="@+id/pickerUserFlaNumber"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_marginTop="@dimen/margin_10"
        android:background="@drawable/cornerround"
        android:hint="@string/hintFlatNumber"
        android:textSize="@dimen/text_size_14" />
    
    <EditText
        android:id="@+id/pickerLandMark"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_marginTop="@dimen/margin_10"
        android:background="@drawable/cornerround"
        android:hint="@string/hintLandmark"
        android:textSize="@dimen/text_size_14" />
    
    <RelativeLayout
        android:id="@+id/pickerCollectCash"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/margin_10">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_centerVertical="true"
            android:text="@string/cashToCollect" />
    
        <EditText
            android:id="@+id/pickerCashToCollect"
            android:layout_width="@dimen/width_100"
            android:layout_height="40dp"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_gravity="end"
            android:layout_marginTop="@dimen/margin_5"
            android:background="@drawable/cornerround"
            android:hint="@string/hintRs"
            android:inputType="phone"
            android:maxLength="5"
            android:textSize="@dimen/text_size_14" />
    
    </RelativeLayout>
    
    
    <RelativeLayout
        android:id="@+id/PickerDoneBtn"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:layout_gravity="right"
        android:layout_marginTop="10dp"
        android:background="@color/secondary_color">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:paddingLeft="20dp"
            android:paddingRight="20dp"
            android:text="PROCEED" />
    
    </RelativeLayout>
    
    
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:background="@color/newDesignGrey">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:layout_marginLeft="5dp"
            android:layout_marginTop="5dp"
            android:text="Saved Addresses"
            android:textSize="@dimen/text_size_14"
            android:textStyle="bold" />
    </RelativeLayout>
    
    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:dividerHeight="0dp"
        android:scrollbars="none"
        android:background="@color/newDesignGrey" />
    

【问题讨论】:

  • 解释你的问题。我认为您不应该摆脱列表视图。
  • @ThomasR。实际上,我在 listview 上方使用了很多工具,当我滚动 dowen listView 时,我需要根据我的页面。整页应该滚动而不是 listView
  • 可以显示你的xml文件
  • 线性布局为什么需要adater来设置数据...
  • 不要放弃你的列表视图来支持线性布局 - 这样做会非常具有挑战性。

标签: android android-layout android-listview android-linearlayout


【解决方案1】:

请描述所有问题范围。

正如我从 question 和 cmets 中看到的,您有一个 ListView,它可以在屏幕的某些部分滚动,但您需要滚动整个屏幕。并且您想将 ListView 转换为 LinearLayout。那是对的吗?如果是,这不是一个好主意,特别是如果您的 ListView 包含很多项目。最好将标题添加到您的 ListView 或使用自定义 NonScrollableListView 类。但就我而言,第一个选项更可取。

等你的cmets,等你回复我就清楚一点了。

【讨论】:

  • 是的,你是对的。将所有工具更改为标题将花费很多时间您能提出其他建议吗
  • 那么您还可以在滚动视图中添加列表视图和所有其他视图。
  • 在 ScrollView 中添加 ListView 不是很好的做法。基本上,在这种情况下,您将在屏幕上有两个滚动处理程序,而 android 将不知道该使用哪个。当我尝试执行这种技巧时,我刚刚收到了我的 ListView 的第一项,仅此而已。实际上,我认为将您的视图放在某个容器中、获取该容器的 id 并从中制作标题没有问题,但如果可能存在这样的问题,本主题可能会对您有所帮助:stackoverflow.com/questions/4338185/… 请参阅 cmets,那里有很多解决方案。
  • 我不这么认为,这可以支持我的观点..stackoverflow.com/questions/18813296/…
  • 也检查此链接的第二个答案stackoverflow.com/questions/6210895/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-08-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多