listview asyncimage mismatch - android-asynctask

i used the Android-Universal-Image-Loader(https://github.com/nostra13/Android-Universal-Image-Loader) for my project,but i get a strange problem:
the image loaded from the website was dismatch with the listview item when i scroll fast or fling fast...
i mean the listview item will load the wrong image sometimes,here is the code:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
Map<String, Object> item = mDatasource.get(position);
View view = convertView;
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
view = mInflater.inflate(R.layout.block_list_item, null);
holder.account_name = (TextView) view.findViewById(R.id.author_name);
holder.account_avatar = (ImageView) view.findViewById(R.id.view_header);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
holder.account_name.setText(StringUtils.convertSafeString(item.get("account_name")));
String avatarUrl = UrlHelper.HOST + item.get("account_avatar");
if (!avatarUrl.endsWith(Constants.NO_AVATAR)) {
holder.account_avatar.setTag(avatarUrl);
imageLoader.displayImage(avatarUrl,holder.account_avatar, mOptions);
}
return view;
}

Well, I don't know this is going to help or not
I had a similar problem but I was using Parse, meaning I am getting my images from parse server
I managed to solve the problem when I discovered that the method I am using to get the image was meant to open a new thread rather than the UI thread and get the image in background (getParseFile().getDataInBackGround), when I changed it to another method which doesn't use another thread(getParseFile().getData()), it worked without problems.

Related

Xamarin.Android: Click event on Textview inside list view cell called multiple times

I have a list view in which inside each cell I have two textview, I need to handle the click event for one of the textview element. But when I put the click event inside the GetView() of the adapter, it is called multiple times.
public override View GetView(int position, View convertView, ViewGroup parent)
{
var item = tableItems[position];
ViewHolder holder;
View view = convertView;
if (view == null)
{
LayoutInflater layoutInflator = LayoutInflater.From(mContext);
view = layoutInflator.Inflate(Resource.Layout.myListViewCell, null);
holder = new ViewHolder();
holder.tvEmpName = view.FindViewById<TextView>(Resource.Id.tv_EmpName);
holder.tvEmpPhone = view.FindViewById<TextView>(Resource.Id.tv_EmpPhone);
view.Tag = holder;
}
else
{
holder = (ViewHolder)view.Tag;
}
holder.tvEmpName.Text = item.FullName;
holder.tvEmpPhone.Text = item.Phone;
holder.tvEmpPhone.Click += (sender, e) => {
// Click event to launch the Popup menu
// This event is being called multiple times, as Get view() being called multiple times.
};
return view;
}
I gone through this similar thread, but didn't find any solution.
The event is called multiple times because evertime when the metod getview is called you add a click event with +=.
In this case you can put the click event inside the if, like that:
if (view == null)
{
LayoutInflater layoutInflator = LayoutInflater.From(mContext);
view = layoutInflator.Inflate(Resource.Layout.myListViewCell, null);
holder = new ViewHolder();
holder.tvEmpName = view.FindViewById<TextView>(Resource.Id.tv_EmpName);
holder.tvEmpPhone = view.FindViewById<TextView>(Resource.Id.tv_EmpPhone);
view.Tag = holder;
holder.tvEmpPhone.Click += (sender, e) => {
// Code here
};
}
But the recyclerview is better than listview for this.

Checked checkboxes is unchecked when i scroll listview in xamarin android

Below is my GetView() method in adapter class, when I scroll the list view by selecting one checkbox. After scrolling back to initial position checked checkbox is getting unchecked.
public override View GetView (int position, View convertView, ViewGroup parent)
{
View view = convertView;
var item = mMyList [position];
//View holder
MyViewHolder holder = null;
if (view == null) {
holder = new MyViewHolder ();
view = mcontext.LayoutInflater.Inflate (Resource.Layout.listview_layout, null);
holder.mChecked = view.FindViewById<CheckBox> (Resource.Id.chkBox);
holder.Name = view.FindViewById<TextView> (Resource.Id.Name);
holder.StartDate = view.FindViewById<TextView> (Resource.Id.startDate);
holder.EndDate = view.FindViewById<TextView> (Resource.Id.endDate);
holder.Desc = view.FindViewById<TextView> (Resource.Id.Desc);
view.SetTag (holder);
} else {
holder = (MyViewHolder)view.Tag;
}
holder.Name.SetText (item.Name, TextView.BufferType.Normal);
holder.StartDate.SetText (item.StartDate, TextView.BufferType.Normal);
holder.EndDate.SetText (item.EndDate, TextView.BufferType.Normal);
holder.Desc.SetText (item.Description, TextView.BufferType.Normal);
return view;
}
It's simple!
You just need to store this "states" of checkboxes,because everytime when you scroll your listview, GetView method will be called(to draw hided items, Android reuses rows).
In your DataContext, for e.g. List<MyClass> , where MyClass represents:
public class MyClass
{
public string Name {get;set;}
public string SecondName {get;set;}
public bool IsChecked {get;set;}
}
try to add bool property(in this case IsChecked) for state of Checkbox.
And after this,in your GetView method write something likes this:
holder.mChecked.Checked = MyList [position].#YourBoolProperty#;
Btw i just write that on the fly.
Also,if something isn't clear for you, try to check this or this.
Hope that helps!

Create a Layout Item for ListView in Xamarin Android

I have a problem and It's 10 days that I am working and can't solve it.I made a layout for each row for ListView.This Layout Contains a linearLayout that there is a TextView and a WebView inside it.Now I Need a C# Project that I can add a new Row to the ListView with new text and url whenever I want.For Example: button.click { ListView.add(Resource.Layout.Items, "Text","Url")}..I know this command is wrong. Just I wanted to clear the problem for you.
I khnow it's custom Row layout and I read manny examples at this site other sites and Xamarin site about that,adapters,... but I can't do it. :(
Please answer me correctly.
It is very important for me.
Thanks a lot.
You need to create an adapter that can work with you custom objects as items. It could look like the following sample:
public class MyAdapter : BaseAdapter<MyItem>
{
readonly LayoutInflater inflater;
List<MyItem> myItemList;
public MyAdapter(Context context)
{
inflater = LayoutInflater.FromContext(context);
myItemList = YOUR_DATASOURCE.GetMyItems();
}
public override MyItem this [int index]
{
get { return myItemList[index]; }
}
public override int Count
{
get { return myItemList.Count; }
}
public override long GetItemId(int position)
{
return position;
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
View view = convertView ?? inflater.Inflate(Resource.Layout.MyItemLayout, parent, false);
var item = myItemList[position];
var viewHolder = view.Tag as MyViewHolder;
if (viewHolder == null)
{
viewHolder = new MyViewHolder();
viewHolder.Web = view.FindViewById<WebView>(Resource.Id.MyItemLayout_Icon);
viewHolder.Name = view.FindViewById<TextView>(Resource.Id.MyItemLayout_Title);
view.Tag = viewHolder;
}
viewHolder.Web.Url = item.Url; //You need to check how you have to set the url for a WebView
viewHolder.Name.Text = item.Text;
return view;
}
public override void NotifyDataSetChanged()
{
myItemList = YOUR_DATASOURCE.GetMyItems();
base.NotifyDataSetChanged();
}
}
class MyViewHolder : Java.Lang.Object
{
public WebView Web { get; set; }
public TextView Name { get; set; }
}
You apply the adapter to your ListView with ListView.Adapter = new MyAdapter(Activity);. Each time you change an item in you button click event, you tricker (ListView.Adapter as MyAdapter).NotifyDataSetChanged(); which will force the adapter to reload and refresh the data.
YOUR_DATASOURCE represents the point in your code where you store the informations like the url or text of all your items. This could typically be a database or something similar. While GetMyItems() is a method for example to query your database.
Hope this clears things up.

JavaFX change the image in an imageView

Basically I have a method to load an Image from database into an imageView and a second method to change the image I'm sucessfully running both methods without getting an exception but after the setImage in changeImage() method what do I need to update and how (scene,stage) is it possible at all. I know that there is no method like repaint() in swing in javafx, so how do I approach this ?
public class MainMenuController implements Initializable {
/**
* Initializes the controller class.
*/
#Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
private AnchorPane stck1;
#FXML
private AnchorPane openSecondWindow(ActionEvent event) throws Exception {
GUIController ctrl = new GUIController();
Stage stage = new Stage();
setStck1((AnchorPane) FXMLLoader.load(InteractiveFictionGame2.class.getResource("GUI.fxml")));
ImageView img_1 = new ImageView(ctrl.loadImg().getImage());
img_1.setPreserveRatio(true);
img_1.setSmooth(true);
img_1.setCache(true);
getStck1().getChildren().add(img_1);
Scene scene = new Scene(getStck1());
stage.setTitle("Interactive Fiction Game");
stage.setScene(scene);
stage.setFullScreen(true);
// stage.sizeToScene();
stage.show();
return getStck1();
}
public class GUIController implements Initializable {
#FXML
private TabPane tb1;
/**
* Initializes the controller class.
*
* #param url
*/
#Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
#FXML
private ImageView img_1;
public ImageView loadImg() {
try {
con = DriverManager.getConnection(host, unm, pswrd);
stmnt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
rs = stmnt.executeQuery(SQL);
rs.next();
fis = rs.getBinaryStream(4);
imgt = javax.imageio.ImageIO.read(fis);
Image newImg = SwingFXUtils.toFXImage(imgt, null);
img_1 = new ImageView();
img_1.setImage(newImg);
rs.close();
stmnt.close();
con.close();
} catch (Exception e) {
System.out.println("Not working");
}
return img_1;
}
public void changeImage() {
..
fis = rs.getBinaryStream(1);
imgt = javax.imageio.ImageIO.read(fis);
Image newImg = SwingFXUtils.toFXImage(imgt, null);
img_1.setImage(newImg);
...
} catch (Exception e) {
System.out.println("Not working");
}
return img_1;
}
Your Issue
If you have a member node in your controller which you inject using #FXML, you should never create a new object instance using a new constructor and assign that new object to your existing reference. Instead just use the object which FXML created for you.
You have:
#FXML
private ImageView img_1;
That's fine.
Then in loadImg, you have:
img_1 = new ImageView();
img_1.setImage(newImg);
That is bad.
You already have an ImageView which the FXMLLoader created for you when you loaded your FXML document. The FXML Loader then assigned that ImageView to your img_1 reference because you used an #FXML annotation.
How to Fix it
So all you need to do is to stop creating new ImageViews and only write:
img_1.setImage(newImg);
And you are done.
Why it works
The Image property of ImageView is an observable property. The JavaFX system observes the Image property for any changes and if it changes, automatically updates the image displayed on the screen for the ImageView. You don't need to perform any repaint call (there is no such repaint routine to call in any case).
Background Reading
If you want to understand the JavaFX scene graph architecture better, read the Oracle tutorial on it:
Working with the JavaFX Scene Graph.
Some Tips
You can create a JavaFX image directly from an InputStream, you don't need to use ImageIO and SwingFXUtils for this task.
You can use a Task to communicate with a database and your application may be more responsive.
It is probably simpler to read the image from a file or over http rather than from a database.
Disclaimer
Besides the issue pointed out here, there may be other errors in code you have not provided which may prevent you from getting your application to work as you wish.
Java I graduate here:
In my JavaFX term project I had to update an imageView object upon a setOnAction Event (clicking a button). This allowed the program user to click through a series of pictures.
The following worked great:
First create your Image and ImageView instances:
Image imageObject = new Image();
ImageView imageViewObject = new ImageView();
Then down in the code a button event causes the (next) image to be assigned and updated as follows:
btn.setOnAction(e -> {
imageIndex++;
imageFilename = imageNamesArray.get(imageIndex);
imageObject = new Image(imageFilename);
imageViewObject.setImage(imageObject);
}
Note: The filename(s) in my project are jpg file (names) saved as String elements in an ArrayList(). The button click also increments the array index to the next jpg filename (and path or URL) and the new image would appear.
So as in the aforementioned answer you only create one ImageViewObject but you do reassign a new image to the image object "imageObject" each time.

Multiple Custom Cell's in a ListView (Cross Platform)

Currently with ListView's I've only found that you can create a template for cells, which makes each cell look exactly the same. You can't have multiple custom cells in the listview. There are work-arounds like hiding the content in the cell depending on the content, but this seems pretty hacky.
The reason I want to use a listview over a tableview is because we plan on doing inserts, deletions, dynamically showing certain cells, and listview's can be binded to a data source.
Create your own ViewCell which overrides binding context change method. When the binding changes set the ViewCell's view to one that matches the type of view model and also set the height of the cell. Below is a quick sample that should give you an idea how to accomplish it.
public class DataTemplateCell1 : ViewCell
{
protected override void OnBindingContextChanged()
{
var vm1 = this.BindingContext as ViewModel1;
if (vm1 != null)
{
this.View = new View1() { HeightRequest = 40 };
this.Height = this.View.HeightRequest;
return;
}
var vm2 = this.BindingContext as ViewModel2;
if (vm2 != null)
{
this.View = new View2() { HeightRequest = 80 };
this.Height = this.View.HeightRequest;
return;
}
base.OnBindingContextChanged();
}
}

Resources