Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/209.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android twoLineListItem的正确使用_Android_Listview - Fatal编程技术网

Android twoLineListItem的正确使用

Android twoLineListItem的正确使用,android,listview,Android,Listview,我是android开发新手,正在尝试创建一个带有粗体标题的列表 以及每个项目的较小说明。 如图所示(抱歉,现在还不能发布图片): 这是我必须首先使用的XML: <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent"> <TwoLineListItem android:layout_width="fill_parent" android:layout_height=

我是android开发新手,正在尝试创建一个带有粗体标题的列表
以及每个项目的较小说明。 如图所示(抱歉,现在还不能发布图片):

这是我必须首先使用的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代码中填充这样的列表?就像我说的,我对开发还不熟悉,所以我可能离开发还有很长的路要走。谢谢你的意见

您使用的是listview

我有一个类似的实现,但是我没有2行listview,而是有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>

对于对象数组,您只需实现自己的适配器。 它可以用于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;
}

我在我的应用程序的不同部分有一个listview,但我的印象是,它只能显示列表中每个项目的一种文本类型。感谢您的输入,我将进一步研究。这取决于您如何实例化它。您可以对BaseAdapter类进行子cass,以便将任何视图提供给listview。在线上有数百个自定义ListView示例。您没有定义c、setListAdapter方法。这个解决方案是不完整的。
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;
}
setListAdapter(new ProfileItemAdapter(getActivity(),
            android.R.layout.two_line_list_item, // or my own custom layout 
            ProfileListContent.ITEMS));