如何在android studio中使用xml布局填充列表视图项?

时间:2016-05-07 08:37:34

标签: android android-layout android-fragments android-studio

我正在开发一个Android项目,我想用xml布局填充listview项目。布局由图像文本框和文本视图组成。请告诉我我该怎么做?

2 个答案:

答案 0 :(得分:1)

您需要为对象创建模型,然后创建类型为model的自定义ArrayAdapter,如下所示

public class MyAdapter extends ArrayAdapter<MyModel> {

public MyAdapter(Context context, ArrayList<MyModel> objects) {
    super(context, 0, objects);
}

@Override
public View getView(int position, View view, ViewGroup viewGroup) {
    ViewHolder viewHolder;

    if (view == null) {
        view = LayoutInflater.from(getContext()).inflate(R.layout.Your_xml_layout, viewGroup, false);

        viewHolder = new ViewHolder();

        viewHolder.textView = (TextView) view.findViewById(R.id.Your_TextView_id);
        viewHolder.imageView = (ImageView) view.findViewById(R.id.Your_ImageView_id);
        view.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) view.getTag();
    }

    MyModel myModel = getItem(position);

    viewHolder.textView.setText(myModel.getter_for_textView);    
    viewHolder.imageView // Load your image to the imageView

   return view;
}

static class ViewHolder {
    TextView textView;
    ImageView imageView;
}

}

在您的主要活动中,获取对listView的引用,创建适配器的对象并在listview上设置适配器。

答案 1 :(得分:-1)

尝试这种方式,您需要创建自定义适配器

In [77]: df_quad
Out[77]:
   C1_C1  C1_C2  C1_C3  C1_C4  C2_C2  C2_C3  C2_C4  C3_C3  C3_C4  C4_C4
0     64      0     40     48      0      0      0     25     30     36
1     16     20     12     20     25     15     25      9     15     25

In [156]: df_quad.columns = pd.MultiIndex.from_tuples(combs)

In [157]: df_quad
Out[157]:
   C1              C2          C3      C4
   C1  C2  C3  C4  C2  C3  C4  C3  C4  C4
0  64   0  40  48   0   0   0  25  30  36
1  16  20  12  20  25  15  25   9  15  25

In [78]: combs
Out[78]:
[('C1', 'C1'),
 ('C1', 'C2'),
 ('C1', 'C3'),
 ('C1', 'C4'),
 ('C2', 'C2'),
 ('C2', 'C3'),
 ('C2', 'C4'),
 ('C3', 'C3'),
 ('C3', 'C4'),
 ('C4', 'C4')]

列表项

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <ListView
       android:id="@+id/list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

    </ListView>

</RelativeLayout>

适配器

  <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

        <ImageView
            android:id="@+id/img"
            android:layout_width="50dp"
            android:layout_height="50dp"/>

        <TextView
            android:id="@+id/txt"
            android:layout_width="wrap_content"
            android:layout_height="50dp" />


</LinearLayout >

MainActivity

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class CustomList extends ArrayAdapter<String>{

private final Activity context;
private final String[] web;
private final Integer[] imageId;
public CustomList(Activity context,
String[] web, Integer[] imageId) {
super(context, R.layout.list_single, web);
this.context = context;
this.web = web;
this.imageId = imageId;

}
@Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.list_single, null, true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.txt);

ImageView imageView = (ImageView) rowView.findViewById(R.id.img);
txtTitle.setText(web[position]);

imageView.setImageResource(imageId[position]);
return rowView;
}
}