我需要在listview中插入图像

时间:2015-03-05 15:37:12

标签: java android

我是Android开发的新手,我需要修改我的代码,使其显示带有图像功能的文本:

print_view xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#BFAF80">


<ListView
    style="14dp"
    android:layout_width="fill_parent"
    android:layout_height="150dp"
    android:id="@+id/operations"
    android:layout_below="@+id/search_view"
    android:layout_centerHorizontal="true" />

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageView"
    android:layout_below="@+id/operations"
    android:layout_centerHorizontal="true"
    android:src="@drawable/printer" />


list_row_item xml:

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

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:padding="20dp"
    android:layout_height="wrap_content">

    <ImageView
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:src="@drawable/wifi"
        android:layout_height="wrap_content"
        android:id="@+id/imageForList" />

    <TextView
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:layout_marginTop="35dp"
        android:layout_height="wrap_content"
        android:text="Nearby Wifi"
        android:id="@+id/textForList" />

    <ImageView
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:src="@drawable/bluetooth"
        android:layout_height="wrap_content"
        android:id="@+id/imageForList2" />

    <TextView
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:layout_marginTop="35dp"
        android:layout_height="wrap_content"
        android:text="Nearby Blutooth"
        android:id="@+id/textForList2" />

    <ImageView
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:src="@drawable/usb"
        android:layout_height="wrap_content"
        android:id="@+id/imageForList3" />

    <TextView
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:layout_marginTop="35dp"
        android:layout_height="wrap_content"
        android:text="Direct USB Connected"
        android:id="@+id/textForList3" />


    <ImageView
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:src="@drawable/cloud"
        android:layout_height="wrap_content"
        android:id="@+id/imageForList4" />

    <TextView
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:layout_marginTop="35dp"
        android:layout_height="wrap_content"
        android:text="Google Cloud Print"
        android:id="@+id/textForList4" />
</LinearLayout>


SettingsList java class:

public class SettingsList {

private int[] images;
private String[] items;

public SettingsList(int[] images, String[] items) {

    this.images = images;
    this.items = items;
}

public int[] getImages() {
    return images;
}

public void setImages(int[] images) {
    this.images = images;
}

public String[] getItems() {
    return items;
}

public void setItems(String[] items) {
    this.items = items;
}
}

SettingsAdapter java class:

public class SettingsAdapter extends ArrayAdapter<SettingsList> {


public SettingsAdapter(Context context, int resource, List<SettingsList> objects) {
    super(context, resource, objects);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolderItems holder;
    if(convertView == null){
        LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = vi.inflate(R.layout.list_row_item, parent, false);
        holder = new ViewHolderItems();
        holder.holderImage = (ImageView)convertView.findViewById(R.id.imageForList);
        holder.holderImage = (ImageView)convertView.findViewById(R.id.imageForList2);
        holder.holderImage = (ImageView)convertView.findViewById(R.id.imageForList3);
        holder.holderImage = (ImageView)convertView.findViewById(R.id.imageForList4);
        holder.holderText = (TextView)convertView.findViewById(R.id.textForList);
        holder.holderText = (TextView)convertView.findViewById(R.id.textForList2);
        holder.holderText = (TextView)convertView.findViewById(R.id.textForList3);
        holder.holderText = (TextView)convertView.findViewById(R.id.textForList4);
        convertView.setTag(holder);
    }else{
        holder = (ViewHolderItems)convertView.getTag();
    }
    SettingsList current = getItem(position);
    holder.holderImage.setImageResource(current.getImages()[position]);
    holder.holderText.setText(current.getItems()[position]);

    return convertView;
}

static class ViewHolderItems{
    ImageView holderImage;
    TextView holderText;
}
}

PrintView mainactivity:

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class PrintView  extends ActionBarActivity {    private ListView lv;
private SettingsAdapter adapter;
private List<SettingsList> itemList;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.print_view);
    int[] imgs = new int[]
            {
                    R.drawable.wifi,
                    R.drawable.bluetooth,
                    R.drawable.usb,
                    R.drawable.cloud,

            };
    String[] values = new String[]
            {"Nearby Wifi",
                    "Nearby Blutooth",
                    "Direct USB Connected",
                    "Google Cloud Print"

            };
    lv = (ListView)findViewById(R.id.operations);
    itemList = new ArrayList<>();
    itemList.add(new SettingsList(imgs, values));

    adapter = new SettingsAdapter(PrintView.this, R.layout.list_row_item, itemList);
    lv.setAdapter(adapter);

}

}

1 个答案:

答案 0 :(得分:1)

试试这个:

1)创建res / layout / list_row_item.xml

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

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:padding="20dp"
        android:layout_height="wrap_content">

        <ImageView
            android:layout_width="match_parent"
            android:layout_weight="1"
            android:src="@mipmap/ic_launcher"
            android:layout_height="wrap_content"
            android:id="@+id/imageForList" />

        <TextView
            android:layout_width="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:layout_marginTop="35dp"
            android:layout_height="wrap_content"
            android:text="New Text"
            android:id="@+id/textForList" />
    </LinearLayout>
</LinearLayout>

2)创建自定义列表项:

public class SettingsList {

    private int image;
    private String item;

    public SettingsList(int image, String item) {

        this.image = image;
        this.item = item;
    }

    public int getImage() {
        return image;
    }

    public void setImage(int image) {
        this.image = image;
    }

    public String getItem() {
        return item;
    }

    public void setItem(String item) {
        this.item = item;
    }
}

3)创建自定义适配器

public class SettingsAdapter extends ArrayAdapter<SettingsList> {


    public SettingsAdapter(Context context, int resource, List<SettingsList> objects) {
        super(context, resource, objects);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolderItems holder;
        if(convertView == null){
            LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = vi.inflate(R.layout.list_row_item, parent, false);
            holder = new ViewHolderItems();
            holder.holderImage = (ImageView)convertView.findViewById(R.id.imageForList);
            holder.holderText = (TextView)convertView.findViewById(R.id.textForList);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolderItems)convertView.getTag();
        }
            SettingsList current = getItem(position);
            holder.holderImage.setImageResource(current.getImage());
            holder.holderText.setText(current.getItem());

        return convertView;
    }

    static class ViewHolderItems{
        ImageView holderImage;
        TextView holderText;
    }
}

4)在您希望显示列表视图的活动中执行以下操作:

public class YourActivity extends ActionBarActivity {

    private ListView lv;
    private SettingsAdapter adapter;
    private List<SettingsList> itemList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        lv = (ListView)findViewById(R.id.operations);
        itemList = new ArrayList<>();
        itemList.add(new SettingsList(R.drawable.wifi, Nearby Wifi));
        itemList.add(new SettingsList(R.drawable.bluetooth, Nearby Blutooth));
        itemList.add(new SettingsList(R.drawable.usb, Direct USB Connected));
        itemList.add(new SettingsList(R.drawable.cloud, Google Cloud Print));
        adapter = new SettingsAdapter(YourActivity.this, R.layout.list_row_item, itemList);
        lv.setAdapter(adapter);

    }

}

5)print_view.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#BFAF80">


    <ListView
        style="14dp"
        android:layout_width="fill_parent"
        android:layout_height="150dp"
        android:id="@+id/operations"
        android:layout_below="@+id/search_view"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

希望它有所帮助!!!