改写ListView 实现加载本地图片 非 id

需要两个额外组件:

一个需要写入内容的类(类型bean) 放你的内容

一个自己重写的adapter

1.主Activity

 

package com.su;

 

 

 

 

 

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

 

import android.app.Activity;

import android.content.Intent;

 

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.ListView;

import android.widget.SimpleAdapter;

import android.widget.SimpleAdapter.ViewBinder;

import android.widget.Toast;

 

public class TestListView extends Activity {

 

private String sightID;

private Bitmap bmp;

 

@Override

protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

setContentView(R.layout.comment);

 

 

bmp = getLoacalBitmap("/sdcard/image.jpg");

ListView listView = (ListView) findViewById(R.id.listView1);  

        MyListAdapter adapter = new MyListAdapter(TestListView.this, getData());

        listView.setAdapter(adapter);  

          

    }  

  public static Bitmap getLoacalBitmap(String url) {

    try {

         FileInputStream fis = new FileInputStream(url);

         return BitmapFactory.decodeStream(fis);

    } catch (FileNotFoundException e) {

         e.printStackTrace();

         return null;

    }

  }

  

 // Bitmap bmp = BitmapFactory.decodeFile("/sdcard/1.jpg");

  private List<Commentinfo> getData(){  

  ArrayList<Commentinfo> arrayList = new ArrayList<Commentinfo>();  

        for(int i=0;i<10;i++){  

 

            arrayList.add(new Commentinfo(bmp, "用户"+i, "内容"+i));  

        }  

        return arrayList;  

          

    }

 

 

}

2.Commentinfo.java

package com.su;

 

import android.graphics.Bitmap;

 

public class Commentinfo {

 

private Bitmap bitmap;  

    

private String comment;

    private String user;   

  

    public Commentinfo(Bitmap bitmap, String user ,String comment ) {  

        super();  

        this.bitmap = bitmap;

        this.user = user ;

        this.comment = comment;

        

    }  

    public Bitmap getBitmap() {

return bitmap;

}

 

public void setBitmap(Bitmap bitmap) {

this.bitmap = bitmap;

}

 

public String getComment() {

return comment;

}

 

public void setComment(String comment) {

this.comment = comment;

}

 

public String getUser() {

return user;

}

 

public void setUser(String user) {

this.user = user;

}

}

3.Mylistadapter.java

 

package com.su;

import java.util.List;

import android.app.Activity;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.BaseAdapter;

import android.widget.ImageView;

import android.widget.TextView;

 

public class MyListAdapter extends BaseAdapter {

private Activity context;

private List<Commentinfo> list;

 

public MyListAdapter(Activity context, List<Commentinfo> list) {

this.context = context;

this.list = list;

}

 

@Override

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

LayoutInflater inflater = context.getLayoutInflater();

View itemView = inflater.inflate(R.layout.commentchild, null);

Commentinfo info = list.get(position);

TextView textView = (TextView) itemView.findViewById(R.id.itemUser);

TextView textView1 = (TextView) itemView.findViewById(R.id.itemContent);

ImageView imageView = (ImageView) itemView

.findViewById(R.id.profileImage);

textView.setText(info.getUser());

textView1.setText(info.getComment());

imageView.setImageBitmap(info.getBitmap());

return itemView;

}

 

@Override

public int getCount() {

return list.size();

}

 

@Override

public Object getItem(int position) {

return list.get(position);

}

 

@Override

public long getItemId(int position) {

return position;

}

 

 

}

 

源码下载http://dl.dbank.com/c0w7tekit4

你可能感兴趣的:(ListView)