为列表视图调用并实例化适配器

发布于 2024-12-24 21:13:53 字数 3987 浏览 3 评论 0原文

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 来提供 listview 行数据图像和文本动态。

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

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

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

}

In this example, the listview of the list_item.java is initially empty, and it will call the ImageAndTextListAdapter which will call the web url to supply the listview row of data of image and Text dynamically.

The question I have is how to call the Adapter, lv.setAdapter((ListAdapter) new ImageAndTextListAdapter(this, xxx)); What should be xxx be? can I just do a xxx=List imageAndTexts which list of the ImageAndText class but isn't that duplicate what's inside the ImaheAndTextListAdapter constructor?

Secondly, what should I supply inside the click routine, public void onClick(View v) {
}
// code here
}
} inside the list_item.
The goal of mine is hit the button and that will initiate the action of adpater supplying all the necessary data.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柠栀 2024-12-31 21:13:53

从 ImageAndTextListAdapter 适配器中,xxx 应该是一个列表。

您的适配器采用“活动”和“列表”两个参数。

因此,您应该创建一个列表,并创建 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));

From the ImageAndTextListAdapter adapter the xxx should be a List.

Your adapter takes two parameters of a Activity, and List.

So you should create a List, and create objects of ImageAndText class to add to the list like this..

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));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文