为Listview调用和实例化适配器

时间:2012-01-04 20:06:06

标签: android listview adapter

list_item.java

  public class List_Items extends ListActivity{

 public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
       setContentView(R.layout.list_item);


       ListView lv = (ListView) this.findViewById(android.R.id.list);

       lv.setAdapter((ListAdapter) new ImageAndTextListAdapter(this, xxx));


      Button btn=(Button) findViewById(R.id.button_sync);
      btn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
                    }
                    // code here
                }
              }

ImageAndTextListAdapter.java

    public class ImageAndTextListAdapter extends ArrayAdapter<ImageAndText> {

//   new method
    private ListView listView;
    private AsyncImageLoader asyncImageLoader;

//constructor
public ImageAndTextListAdapter(Activity activity, List<ImageAndText> imageAndTexts) {
    super(activity, 0, imageAndTexts);

    //new method
    this.listView = listView;
    asyncImageLoader = new AsyncImageLoader();
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    Activity activity = (Activity) getContext();
  //  ......

}

}

ImageAndText.java

   public class ImageAndText {
    private String imageUrl;
   private String text;

public ImageAndText(String imageUrl, String text) {
    this.imageUrl = imageUrl;
    this.text = text;
}
public String getImageUrl() {
    return imageUrl;
}
public String getText() {
    return text;
}

}

AsyncImageLoader.java

  public class AsyncImageLoader {
  private HashMap<String, SoftReference<Drawable>> imageCache;
  HashMap<String, SoftReference<Drawable>> drawableMap = new HashMap<String,          SoftReference<Drawable>>();


public AsyncImageLoader() {
    //HashMap<String, SoftReference<Drawable>> drawableMap = new HashMap<String, SoftReference<Drawable>>();
}

public Drawable loadDrawable(final String imageUrl, final ImageCallback imageCallback) {

    if (drawableMap.containsKey(imageUrl)) {
        SoftReference<Drawable> softReference = imageCache.get(imageUrl);
        Drawable drawable = softReference.get();
        if (drawable != null) {
            return drawable;
        }
    }
    final Handler handler = new Handler() {
        @Override
        public void handleMessage(Message message) {
            imageCallback.imageLoaded((Drawable) message.obj, imageUrl);
        }
    };

    //this is the new thread that download the image from url
    new Thread() {
        @Override
        public void run() {
            Drawable drawable = loadImageFromUrl(imageUrl);
            imageCache.put(imageUrl, new SoftReference<Drawable>(drawable));
            Message message = handler.obtainMessage(0, drawable);
            handler.sendMessage(message);
        }
    }.start();
    return null;
}

public static Drawable loadImageFromUrl(String url) {
      InputStream inputStream;
      try {
          inputStream = new URL(url).openStream();
      } catch (IOException e) {
          throw new RuntimeException(e);
      }
      return Drawable.createFromStream(inputStream, "src");
}

public interface ImageCallback {
    public void imageLoaded(Drawable imageDrawable, String imageUrl);
}

}

在这个例子中,list_item.java的listview最初是空的,它将调用ImageAndTextListAdapter,它将调用web url动态地提供图像和Text的listview行数据。

我的问题是如何调用Adapter,lv.setAdapter((ListAdapter)new ImageAndTextListAdapter(this,xxx));什么应该是xxx?我可以只做一个xxx = List imageAndTexts ImageAndText类的哪个列表,但是它与ImaheAndTextListAdapter构造函数中的内容不重复?

其次,我应该在点击例程中提供什么,public void onClick(View v){                     }                     //代码在这里                 }               在list_item里面 我的目标是按下按钮,这将启动adpater提供所有必要数据的操作。

1 个答案:

答案 0 :(得分:1)

从ImageAndTextListAdapter适配器中,xxx应该是List。

您的适配器采用Activity和List的两个参数。

所以你应该创建一个List,并创建ImageAndText类的对象,以便像这样添加到列表中。

ImageAndText image = new ImageAndText("url","Test");
 List<ImageAndText>text;
 text.add(image); //Add the Object of ImageAndText
 ListView lv = (ListView) findViewById(android.R.id.list);

 //Here i supply the adapter with the text list created.
 lv.setAdapter((ListAdapter)new ImageAndTextListAdapter(Main.this, text));