将资产中的可绘制对象添加到 ListView

Adding Drawables from assets into ListView

我在 assets 文件夹中添加了很多图像,想从那里获取这些图像并将其添加到 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;
}
}

列表视图适配器:

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

为什么要为图像使用资产文件夹?如果没有必要,那么您可以使用 drawable for same 将所有图像放在 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;
}
}

这样做

class MainActivity extends AppCompatActivity {
private ListView listEvent;


String arras[]={
  "Logo",
    "name",
    "of",
    "events",
    "are",
    "present",
    "here"
};
Drawable[] arr=new Drawable[5];
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
            EventList adapter = new EventList(MainActivity.this, arras);
            //ListView lv = (ListView)rootView.
            listEvent=(ListView)findViewById(R.id.listEvent);
            listEvent.setAdapter(adapter);



        }
    }

而适配器class是这样的

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

}

如果您得到 FileNotFound Exception

  1. 检查图像是否在资产的子文件夹内并添加适当的路径

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

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

使用 ViewHolder

ViewHolder 通过缓存视图使列表视图加载速度更快。如果每次都调用findViewById()查找view,会很慢。

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