Proper usage of twoLineListItem Android

前端 未结 3 947
误落风尘
误落风尘 2021-01-07 02:44

I am new to development on android and trying to create a list that has a bold header
and a smaller description for each item. Like the one shown here (sorry can\'t post

相关标签:
3条回答
  • 2021-01-07 03:08

    In the case of an Array of Objects, you just have to implement your own adapter. It can work for a TwoLineListItem or any other custom layout.

    For example, if I have a list of items of the following type :

    public class ProfileItem {
        public String host, name, tag;
    
        public ProfileItem(String host, String name, String tag) {
            this.host = host;
            this.name = name;
            this.tag = tag;
        }
    
        @Override
        public String toString() {
            return name+"#"+tag;
        }
    }
    

    Then I create the following adapter :

    public class ProfileItemAdapter extends ArrayAdapter<ProfileItem> {
    
    private Context context;
    private int layoutResourceId;   
    private List<ProfileItem> objects = null;
    
    public ProfileItemAdapter(Context context, int layoutResourceId, List<ProfileItem> objects) {
        super(context, layoutResourceId, objects);
        this.context = context;
        this.layoutResourceId = layoutResourceId;
        this.objects = objects;
    }
    
    public View getView(int position, View convertView, ViewGroup parent)  {
        View v = convertView;
        if(v == null)
        {
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            v = inflater.inflate(layoutResourceId, parent, false);
        }
    
        ProfileItem item = objects.get(position);
        TextView titletext = (TextView)v.findViewById(android.R.id.text1);
        titletext.setText(item.toString());
        TextView mainsmalltext = (TextView)v.findViewById(android.R.id.text2);
        mainsmalltext.setText(item.host);
        return v;
    }
    

    }

    Then everything is in place, in my activity (or fragment), I just have to set this adapter in the onCreate method :

    setListAdapter(new ProfileItemAdapter(getActivity(),
                android.R.layout.two_line_list_item, // or my own custom layout 
                ProfileListContent.ITEMS));
    
    0 讨论(0)
  • 2021-01-07 03:17

    You use a listview http://developer.android.com/reference/android/widget/ListView.html

    0 讨论(0)
  • 2021-01-07 03:25

    I have a similar implementation, but instead of a 2 line listview, I have 3 lines.

    This is the XML:

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical"  >
        <TextView
            android:id="@+id/text1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textColor="#111111"
            android:textSize="17sp"
            android:textStyle="bold"
            android:text="PROJECT NAME"
            android:typeface="monospace"
            android:paddingBottom="2dp"
            android:paddingTop="2dp"
            android:paddingLeft="5dp"
            android:paddingRight="5dp"
            android:gravity="left|center"   >
        </TextView>
        <TextView
            android:id="@+id/text2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textColor="#111111"
            android:text="Selected Type"
            android:textSize="16sp"
            android:paddingBottom="1dp"
            android:paddingTop="1dp"
            android:paddingLeft="5dp"
            android:paddingRight="5dp"  >
        </TextView>
        <TextView
            android:id="@+id/text3"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textColor="#111111"
            android:textSize="16sp"
            android:paddingTop="1dp"
            android:paddingBottom="1dp"
            android:paddingLeft="5dp"
            android:paddingRight="5dp"
            android:text="Project Description"  >
        </TextView>
    </LinearLayout>
    

    And this is the JAVA code for filling the lines with data returned from a DB. This code is in the onCreate() method:

    String[] fields = new String[]  {   db.TABLE_PRJ_NAME, db.TABLE_PRJ_TYPE, db.TABLE_PRJ_DESC };
        int[] views = new int[] {   R.id.text1, R.id.text2, R.id.text3  };
    
        c = db.getAllProjects();
        startManagingCursor(c);
    
        // Set the ListView
        SimpleCursorAdapter prjName = new SimpleCursorAdapter(
                this,
                R.layout.project_list_rows,
                //android.R.layout.simple_list_item_1,
                c, fields, views);
        setListAdapter(prjName);
    
    0 讨论(0)
提交回复
热议问题