将资产中的Drawables添加到ListView中

时间:2017-01-06 08:04:11

标签: java android listview android-assets

我在assets文件夹中添加了许多图像,并希望从那里获取这些图像并将其添加到ListView中。 当我执行应用程序时,一切都很完美,但listview的图像容器显示为空白(透明)。 Output

Java文件:

package com.basil.victor;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import java.io.IOException;
import java.io.InputStream;

public class Events extends Fragment {

private ListView listEvent;

String eventname[]={
        "Name",
        "of",
        "the",
        "events",
        "are",
        "present",
        "here"
};

String eventlogoname[]={
        "Logo",
        "name",
        "of",
        "events",
        "are",
        "present",
        "here"
};

Drawable[] arr=new Drawable[7];

String eventsubtitle []={
        "Subtitles",
        "of",
        "the",
        "events",
        "are",
        "present",
        "here"
};

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_events, null);



    for(int i=0;i<7;i++) {
        try {
            InputStream stream = getContext().getAssets().open(eventlogoname[i] + ".jpg");
            Drawable el = Drawable.createFromStream(stream, null);
            arr[i] = el;
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    EventList adapter = new
            EventList(getActivity(), eventname, arr, eventsubtitle);
    //ListView lv = (ListView)rootView.
    listEvent=(ListView)view.findViewById(R.id.listEvent);
    listEvent.setAdapter(adapter);


    return view;
}
}

ListView适配器:

package com.basil.victor;
import android.app.Activity;
import android.graphics.drawable.Drawable;
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 EventList extends ArrayAdapter<String>{

private final Activity context;
private final String[] title;
private final Drawable[] banner;
private final String[] subtitle;
public EventList(Activity context,
                  String[] title, Drawable[] banner, String[] subtitle) {
    super(context, R.layout.list_single, title);
    this.context = context;
    this.title = title;
    this.banner = banner;
    this.subtitle = subtitle;

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

    TextView txtTitle = (TextView) rowView.findViewById(R.id.event_title);
    ImageView imageView = (ImageView) rowView.findViewById(R.id.event_banner);
    TextView subTitle = (TextView) rowView.findViewById(R.id.event_subtitle);


    txtTitle.setText(title[position]);
    imageView.setImageDrawable(banner[position]);
    subTitle.setText(subtitle[position]);


    return rowView;
}
}

4 个答案:

答案 0 :(得分:1)

如果您收到FileNotFound Exception

  1. 检查图片是否在资源中的子文件夹内,并添加相应的路径

  2. 检查扩展的情况(在我意识到我的图像有.JPG(大写)扩展名之前我已经遇到这个问题了很长时间)

  3. 正在搜索的最后一个字符串,检查随机空格

答案 1 :(得分:0)

为什么要为图像使用资产文件夹?如果没有必要那么你可以使用drawable将所有图像放在drawable文件夹和

package com.basil.victor;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import java.io.IOException;
import java.io.InputStream;

public class Events extends Fragment {

private ListView listEvent;

String eventname[]={
        "Name",
        "of",
        "the",
        "events",
        "are",
        "present",
        "here"
};



int[] arr=new int[7];

String eventsubtitle []={
        "Subtitles",
        "of",
        "the",
        "events",
        "are",
        "present",
        "here"
};

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_events, null);

 arr[]={
            R.drawable.Logo,
            R.drawable.name,
            R.drawable.of,
            R.drawable.events,
            R.drawable.are,
            R.drawable.present,
            R.drawable.here
    };



    EventList adapter = new
            EventList(getActivity(), eventname, arr, eventsubtitle);
    //ListView lv = (ListView)rootView.
    listEvent=(ListView)view.findViewById(R.id.listEvent);
    listEvent.setAdapter(adapter);


    return view;
}
}

适配器:

package com.basil.victor;
import android.app.Activity;
import android.graphics.drawable.Drawable;
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 EventList extends ArrayAdapter<String>{

private final Activity context;
private final String[] title;
private final int[] banner;
private final String[] subtitle;
public EventList(Activity context,
                  String[] title, int[] banner, String[] subtitle) {
    super(context, R.layout.list_single, title);
    this.context = context;
    this.title = title;
    this.banner = banner;
    this.subtitle = subtitle;

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

    TextView txtTitle = (TextView) rowView.findViewById(R.id.event_title);
    ImageView imageView = (ImageView) rowView.findViewById(R.id.event_banner);
    TextView subTitle = (TextView) rowView.findViewById(R.id.event_subtitle);


    txtTitle.setText(title[position]);
imageView.setImageResource((banner[position]);
    subTitle.setText(subtitle[position]);


    return rowView;
}
}

答案 2 :(得分:0)

这样做

 public class EventList extends BaseAdapter {
LayoutInflater inflater;
private final Activity context;
String arras[];
public EventList(Activity context,String[] arras) {
    super();
    this.context = context;
    this.arras = arras;
    inflater = (LayoutInflater) this.context
            .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

}
@Override
public int getCount() {
    return arras.length;
}

@Override
public Object getItem(int position) {
    return arras.length;
}

@Override
public long getItemId(int position) {
    return position;
}
@Override
public View getView(int position, View myView, ViewGroup parent) {
    if (myView == null) {
        myView = inflater.inflate(R.layout.event_row, null, true);

    }

    ImageView imageView = (ImageView) myView.findViewById(R.id.event_banner);
    try {
        InputStream stream = context.getAssets().open(arras[position] + ".jpg");
        Drawable el = Drawable.createFromStream(stream, null);
        imageView.setImageDrawable(el);
    } catch (IOException e) {
        e.printStackTrace();
    }


    return myView;
}

适配器类就像这样

{{1}}

}

答案 3 :(得分:0)

使用ViewHolder

ViewHolder通过缓存视图使列表视图加载更快。如果每次通过调用findViewById()来查看视图,它将会非常慢。

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        ViewHolder holder; // to reference the child views for later actions

        if (v == null) {
            LayoutInflater vi =
                    (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.mainrow, null);
            // cache view fields into the holder
            holder = new ViewHolder();
            holder.ImageView = (ImageView) v.findViewById(R.id.event_banner);
            // associate the holder with the view for later lookup
            v.setTag(holder);
        } else {
            // view already exists, get the holder instance from the view
            holder = (ViewHolder) v.getTag();
        }
        // no local variables with findViewById here

        // use holder.ImageView where you were
        // using the local variable nameText before
        try {
            InputStream stream = context.getAssets().open(arras[position] + ".jpg");
            Drawable el = Drawable.createFromStream(stream, null);
            holder.ImageView.setImageDrawable(el);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return v;
    }

// somewhere else in your class definition
static class ViewHolder {
    ImageView imageView;
}