Safe, standard way to load images in ListView on a different thread?
Date : March 29 2020, 07:55 AM
hop of those help? I solve this by maintaining a WeakHashMap that remembers the last Bitmap I want to set for that view.
|
Android Loading ListView from another Thread
Date : March 29 2020, 07:55 AM
this one helps. Please explain me why this code does not work?(I need to call the lv.setAdapter (adapterA) in the onClick(). It is necessary to change the adapters). It works if you run code on the main thread, but I need to load data in another Thread. I would be grateful for any ideas. Thank you. , SOLUTION: package com.example.examplelist;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.concurrent.TimeUnit;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
public class ActivityElemsExample extends Activity implements OnClickListener {
ArrayList<Elem> myList;
Button btn;
ListView lv;
AdapterA adapterA;
AdapterB adapterB;
boolean running = false;
Thread thread;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn = (Button)findViewById(R.id.button_loading);
btn.setOnClickListener(this);
lv = (ListView)findViewById(R.id.lv);
myList = new ArrayList<Elem>();
adapterA = new AdapterA(this, myList);
adapterB = new AdapterB(this, myList);
}
public void onClick(View button){
if(!running){
running = true;
Log.d("MyLogs", "LOADING");
thread = new Thread(new Runnable(){
public void run(){
myList.clear();
for(int i=0; i < 3000; i++){
myList.add(new Elem(i, System.currentTimeMillis(), "Elem "+i));
}
handler.sendEmptyMessage(1);
}
});
thread.start();
}
}
public void onDestroy(){
super.onDestroy();
if(thread != null){
while(true){
try{
thread.join();
break;
}
catch (InterruptedException e){}
}
}
}
public Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
lv.setAdapter(adapterA);
running = false;
}
};
}
|
How to highlight android listview first item after load items to listview?
Date : March 29 2020, 07:55 AM
this one helps. 1.This is my listview FromTableAdapter.setSelectedPosition(0);
FromTableAdapter.notifyDataSetChanged();
<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_selected="true"
android:drawable="@color/pressed_color"/>
<item
android:drawable="@color/default_color" />
</selector>
lv_FromTable.setItemChecked(0,true);
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:listSelector="@drawable/list_selector_background" />
|
JavaFX listview load a wrong image in the custom cell with a thread
Tag : java , By : user143038
Date : March 29 2020, 07:55 AM
will be helpful for those in need I work on my first JavaFX project and have problem with listview custom cell factory. this is my code private static Map<String, Image> images = new HashMap<>();
...
Image image;
if ((image = images.get(item.getOwnerId())) == null) {
img.setImage(null);
new Thread(() -> {
Image image1 = new Image(item.getOwnerProfilePicUrl());
images.put(item.getOwnerId(), image1);
Platform.runLater(() -> img.setImage(image1));
}).start();
} else {
img.setImage(image);
}
...
import java.awt.Desktop; // importing more classes from awt than neccessary could result in problems
...
public class DischargeComment extends ListCell<Comment> {
...
/**
* Constructor to pass external cache
* @param cache
*/
public DischargeComment(Map<String, SoftReference<Image>> cache) {
if (cache == null) {
throw new IllegalArgumentException();
}
this.cache = cache;
}
/**
* constructor using the default cache
*/
public DischargeComment() {
this(getDefaultCache());
}
private final Map<String, SoftReference<Image>> cache;
private static Map<String, SoftReference<Image>> defaultCache;
private static final URL FXML_URL = DischargeComment.class.getResource("cell/discharge_comment.fxml");
public static Map<String, SoftReference<Image>> getDefaultCache() {
if (defaultCache == null) {
defaultCache = new HashMap<>();
}
return defaultCache;
}
public static String search = "";
private boolean loaded = false; // no need for a reference to fxmlloader here
@Override
protected void updateItem(Comment item, boolean empty) {
super.updateItem(item, empty);
if (empty || item == null) {
setText(null);
setGraphic(null);
} else {
if (!loaded) {
FXMLLoader mLLoader = new FXMLLoader(FXML_URL);
mLLoader.setController(this);
try {
mLLoader.load();
img.setClip(new Circle(25, 25, 25));
loaded = true;
} catch (IOException e) {
e.printStackTrace();
}
}
...
// use single access here
// also use url as key
cache.compute(item.getOwnerProfilePicUrl(), (key, value) -> {
Image image = null;
if (value != null) {
image = value.get();
}
if (image == null) {
image = new Image(key, true); // load image in background
value = new SoftReference<>(image);
}
img.setImage(image);
return value;
});
lblUsername.setOnMouseClicked(e->{
try {
Desktop.getDesktop().browse(new URI("https://www.instagram.com/" + item.getOwnerUsername()));
} catch (IOException | URISyntaxException exception) {
exception.printStackTrace();
}
});
setText(null);
setGraphic(pane);
setPrefHeight(Region.USE_COMPUTED_SIZE); // don't set height to -1
}
}
}
|
Android listview not load one by one item is load completely when complete data download?
Date : March 29 2020, 07:55 AM
|