改写ListView 实现加载本地图片 非 id
博客专区 > SuShine 的博客 > 博客详情
改写ListView 实现加载本地图片 非 id
SuShine 发表于2年前
改写ListView 实现加载本地图片 非 id
  • 发表于 2年前
  • 阅读 5
  • 收藏 0
  • 点赞 0
  • 评论 0

腾讯云 十分钟定制你的第一个小程序>>>   

//主Activity
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



共有 人打赏支持
粉丝 118
博文 303
码字总数 78170
×
SuShine
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: