我将数据解析为ListView,现在如何在每行的左侧添加图像? (来自drawable文件夹)

时间:2013-10-18 15:14:18

标签: android json parsing listview imageview

listview从ArrayList收集数据,该数组从JSON,URL收集并解析它。

现在我想在每行的左侧添加图标,但它们是本地的,所以在我的drawable文件夹中。它们不需要被解析。我在ViewHolder中放了一个链接到ImageView的链接,如果我转到我的xml布局,我可以安装android:src = @ drawable / ic_launcher它会起作用。我想要10个不同的图像。

请参阅下面的ListView:

class FancyAdapter extends ArrayAdapter<idData> {

    FancyAdapter() {
        super(getApplicationContext(), android.R.layout.simple_list_item_1, arrayOfBooks);
    }

    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder;

        if (convertView == null) {
            LayoutInflater inflater = getLayoutInflater();
            convertView = inflater.inflate(R.layout.row_full, null);

            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }
        else {
            holder = (ViewHolder)convertView.getTag();
        }
        holder.populateFrom(arrayOfBooks.get(position));
        return(convertView);

    }

    class ViewHolder {
        public TextView id = null;
        public TextView title = null;
        public TextView link = null;
        public ImageView image = null;

        ViewHolder(View row) {
            id = (TextView) row.findViewById(R.id.id);
            title = (TextView) row.findViewById(R.id.title);
            link = (TextView) row.findViewById(R.id.link); 
            image = (ImageView) row.findViewById(R.id.imageView);
        }

        void populateFrom(idData r) {
            id.setText("ID: " + r.id);
            title.setText(r.title);
            link.setText("URL: " + r.link);
            image.setImageResource(R.drawable.ic_launcher);
        }
    }
}

我尝试添加此内容以查看是否可以将其链接但不起作用:

public static final Integer[] images = { R.drawable.ic_action_overflow,
    R.drawable.ic_launcher, R.drawable.bg_main, R.drawable.ic_launcher};

有什么想法吗?服务器端JSON侧面的本地映像

任何帮助都表示赞赏。

1 个答案:

答案 0 :(得分:2)

在适配器构造函数中初始化LayoutInfalter。您可以将数组传递给构造函数,并在getView

中使用它

在你的构造函数

LayoutInflater inflater;
Integer[] images = { R.drawable.ic_action_overflow,
R.drawable.ic_launcher, R.drawable.bg_main, R.drawable.ic_launcher}
FancyAdapter() {
    super(getApplicationContext(), android.R.layout.simple_list_item_1, arrayOfBooks);
    inflater = LayoutInflater.from(ActivityContext); 
 }

您需要更改您的getview

public View getView(int position, View convertView, ViewGroup parent) {

    ViewHolder holder;

    if (convertView == null) {

        convertView = inflater.inflate(R.layout.row_full, null);
        holder = new ViewHolder();
        holder.id = (TextView)convertView.findViewById(R.id.id);
        holder.title = (TextView)convertView.findViewById(R.id.title);
        holder.link = (TextView)convertView.findViewById(R.id.link);
        holder.image = (ImageView)convertView.findViewById(R.id.imageVIew);
        convertView.setTag(holder);
    }
    else {
        holder = (ViewHolder)convertView.getTag();
    }
     holder.id.setText("id");
     holder.title.setText("title");
     holder.link.setText("link");
     // set image to imageview 
     holder.image.setImageResource(images[position]);
     // considering you have array of drawables in images 
    return convertView ;

}
static class ViewHolder
{
 TextView id,title,link;
 ImageView image;
}

编辑:

你的getView实现看起来很好。我第一眼看上去很困惑。但是没有必要在getView中初始化LayoutInflater。在适配器的构造函数中初始化相同的内容。

正如prosperk建议使用此image.setImageResource(images[position])