How to change my ListView to LinearLayout in Android?

我只是一个虾纸丫 提交于 2019-12-25 06:58:53

问题


I am building an Android application where I am fetching data from a local DB and displaying it in a custom ListView.

Now the problem is I want to use LinearLayout in place of ListView as a scroll problem.

Below my code that I am using for listView.

  1. Declaration in activity -

    lv = (ListView) findViewById(R.id.listView1);
    
    adapter = new CustomAdapter(this, dbh.fetchAllRec(),
            CursorAdapter.NO_SELECTION);
    lv.setAdapter(adapter);
    
  2. CustomListView -

    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" />
    


回答1:


Please, describe all scope of issue.

As I see from question and comments, you've got a ListView, which scrolls on some part of the screen, but you need to scroll all the screen. And you want to cast ListView to LinearLayout. Is that correct? If yes, it's not good idea, specially if your ListView contains a lot of items. It's better to add header to your ListView or use custom NonScrollableListView class. But as for me first option is preferable.

Waiting comments from you, it'll be a bit more clear for me after your response.



来源:https://stackoverflow.com/questions/32067358/how-to-change-my-listview-to-linearlayout-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!