【问题标题】:Proper usage of twoLineListItem AndroidtwoLineListItem Android的正确使用
【发布时间】:2013-02-13 18:20:13
【问题描述】:

我是 android 开发新手,正在尝试创建一个带有粗体标题的列表
以及每个项目的较小描述。 就像这里显示的一样(抱歉还不能发布图片):
http://i.stack.imgur.com/UVqzq.png
这是我必须开始的 XML:

<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TwoLineListItem android:layout_width="fill_parent"
android:layout_height="wrap_content" android:mode="twoLine">
<TextView android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@android:id/text1" android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView android:layout_below="@android:id/text1"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_alignLeft="@android:id/text1" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:id="@android:id/text2" />
</TwoLineListItem>
</LinearLayout> 

有没有办法从另一个 xml 文件中的数组填充列表?如果不是,我将如何从 Java 代码中填充这样的列表?就像我说的那样,我是开发新手,所以我可能还很遥远。感谢您的任何意见!

【问题讨论】:

    标签: android listview


    【解决方案1】:

    【讨论】:

    • 我在我的应用程序的不同部分有一个列表视图,但我的印象是它只能显示列表中每个项目的一种类型的文本。感谢您的意见,我会进一步研究。
    • 这取决于你如何实例化它。您可以对 BaseAdapter 类进行子类化,以将您想要的任何视图提供给列表视图。网上有数百个自定义列表视图示例。
    【解决方案2】:

    在对象数组的情况下,您只需要实现自己的适配器。 它适用于 TwoLineListItem 或任何其他自定义布局。

    例如,如果我有以下类型的项目列表:

    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;
        }
    }
    

    然后我创建以下适配器:

    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;
    }
    

    }

    然后一切就绪,在我的活动(或片段)中,我只需要在onCreate 方法中设置这个适配器:

    setListAdapter(new ProfileItemAdapter(getActivity(),
                android.R.layout.two_line_list_item, // or my own custom layout 
                ProfileListContent.ITEMS));
    

    【讨论】:

      【解决方案3】:

      我有一个类似的实现,但不是 2 行列表视图,而是 3 行。

      这是 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>
      

      这是用从数据库返回的数据填充行的 JAVA 代码。这段代码在 onCreate() 方法中:

      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);
      

      【讨论】:

      • 你没有定义 c, setListAdapter 方法。此解决方案不完整。
      猜你喜欢
      • 2015-03-15
      • 2013-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-12
      相关资源
      最近更新 更多