Android: ArrayAdapter isnt setting images properly - android

Inside a ListFragment I have a ListView which contains an image and some text (See Previous Solved Question)
I briefly managed to get the solution to that question to work but its stopped working now.
I'm loading image Bitmaps in an AsyncTask which update the Adapter via adapter.notifyDataSetChanged();
when they finish downloading I store the Bitmap in an class member: Bitmap array named ICONS[] which I access directly from the ArrayAdapter to save memory.
There's various checks in place to make sure i'm not downloading images multiple times all of which are working but ill add them here anyway:
note i here is each entry in the ListView
ICONS[i] = null for all i until they are downloaded and ICONS[i] becomes a Bitmap image.
iconSet[i] = false for all i until image is downloaded and set (updated within adapter), this stops the resetting of the ImageView Bitmap if its already been set.
iconDownloading[i] = false for all i until the AsyncTask downloader starts
iconDownloaded[i] = false for all i until the AsyncTask downloader finishes
private class ArticleAdapter extends ArrayAdapter<String>{
private final Context context;
private final String[] values;
public ArticleAdapter(Context context, String[] values) {
super(context, R.layout.list_entry, values);
this.context=context;
this.values=values;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.list_entry,parent,false);
TextView tv = (TextView) rowView.findViewById(R.id.label);
tv.setText(values[position]);
if(ICONS[position]!=null&&!iconSet[position]){//not null and not set
iconSet[position]=true;
System.out.println("ArticleListFragment: Page " + pageIndex + " Setting icon " + position);
ImageView iv = (ImageView) rowView.findViewById(R.id.logo);
iv.setImageBitmap(ICONS[position]);
}else if(!iconDownloading[position]&&!iconDownloaded[position]){
iconDownloading[position]=true;//Starting download
String iconUrl = ARTICLE_LIST.getArticle(position).getThumbnail();
if(iconUrl!=null&&!iconUrl.equals("")){
System.out.println("ArticleListFragment: Page " + pageIndex + " Downloading icon " + position);
new LoadThumbnail(position, iconUrl).execute();
}else{
System.out.println("ArticleFragment: Article " + position + " has no icon");
}
}
//iv.setImageResource(resId);//Change Image - All set to sport at the moment
return rowView;
}
}
I've added a few System.out calls aswell to check everything is been done in the correct order, as intended the:
System.out.println("ArticleListFragment: Page " + pageIndex + " Downloading icon " + position);
Comes first then the:
System.out.println("ArticleListFragment: Page " + pageIndex + " Setting icon " + position);
Which is right before the setting of the Bitmaps, so its getting to the right point, with the right Bitmap (i assume as i've used ICONS[position].size() which outputs about 4000 bytes) so i'm stuck wondering what's going wrong?
EDIT
Ive narrowed the problem down to the check if(!iconSet[position]) if i remove this the bitmaps are loaded correctly, but they are reloaded every time another bitmap is added to the ICONS array which for some reason triggers that if statement 4 times for 2 icons.
Why is it doing it 4 times? (twice per row entry) and why does that check make a difference?

Related

removeAllViews does not work on ViewGroup

This function is my function
private void previewCapturedImage(){
Log.d("ChildCount", "content child count before:" + mSelectedImagesContainer.getChildCount());
mSelectedImagesContainer.removeAllViews();
Log.d("ChildCount", "content child count after:" + mSelectedImagesContainer.getChildCount());
Iterator<Uri> iterator = mMedia.iterator();
ImageInternalFetcher imageFetcher = new ImageInternalFetcher(this, 500);
while (iterator.hasNext()) {
Uri uri = iterator.next();
// showImage(uri);
Log.i(TAG, " uri: " + uri);
if (mMedia.size() >= 1) {
mSelectedImagesContainer.setVisibility(View.VISIBLE);
}
View imageHolder = LayoutInflater.from(this).inflate(R.layout.media_layout, null);
// View removeBtn = imageHolder.findViewById(R.id.remove_media);
// initRemoveBtn(removeBtn, imageHolder, uri);
ImageView thumbnail = (ImageView) imageHolder.findViewById(R.id.media_image);
if (!uri.toString().contains("content://")) {
// probably a relative uri
uri = Uri.fromFile(new File(uri.toString()));
}
imageFetcher.loadImage(uri, thumbnail);
mSelectedImagesContainer.addView(imageHolder);
// set the dimension to correctly
// show the image thumbnail.
int wdpx = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 150, getResources().getDisplayMetrics());
int htpx = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 100, getResources().getDisplayMetrics());
thumbnail.setLayoutParams(new FrameLayout.LayoutParams(wdpx, htpx));
}
}
In the second line mSelectedImagesContainer.removeAllViews(), I am trying to clear the view of all existing children views. but it is not working as expected.
So basically this parent container mSelectedImagesContainer has a horizonal list of images that was passed from one more activity.
Also the output of the logging is accurate, it clears all the childviews but for some reason main activity still shows the old images that were previously selected in the viewgroup.
If anything else is needed please let me know. I have not posted the full code since it's quite large. Anyways, if needed I can post it here with the XML's.
Just an info, I am working on a poly-clicker library, for the image picker and a custom scroll view for images.
Screenshots
1) Here I selected 2 images
2) And these images displayed perfectly in the scroll view
3) Now i selected a different set of images i.stack.imgur.com/sw5hz.jpg
4) However, the first selection is still not cleared from the view i.stack.imgur.com/lKovI.jpg
Answer posted in comments by #Shaishav, where the iterator was passed with a hashset which needed to be cleared, so reinitializing the variable "mMedia" did the job.

Strange Glitch For Loop Inflating Views

I am trying to take a string (which is formatted HTML) and split it at every double enter. I know that before and after a double enter will be a p block tag, and I'm using a for loop to split it in this way. Here is my code
for(String s : rawHTML.split("\\n\\n")){
View newView = LayoutInflater.from(c).inflate(R.layout.commentandunder, baseView, true);
LinearLayout underneath = (LinearLayout) newView.findViewById(R.id.inside);
TextView comm = (TextView) newView.findViewById(R.id.commentLine);
Log.v("Slide", "Should be " + s );
Log.v("Slide", "Currently is "+ comm.getText());
comm.setText(Html.fromHtml(s) );
Expected behavior from this is every "Currently is" in the log should be blank. If there are two or more p blocks, though, when I setText, it overwrites the previous text.
For example, I have a string
<p>Test</p>\n\n<p>Hello</p>
I should be seeing two TextViews like so
[Test]
[Hello]
Instead, I see this
[Hello]
[]
I am really stumped and can't figure out this strange issue.
Thank you!
The following code snippet shoud work for you.
View newView = LayoutInflater.from(c).inflate(R.layout.commentandunder, baseView, true);
LinearLayout underneath = (LinearLayout) newView.findViewById(R.id.inside);
TextView comm = (TextView) newView.findViewById(R.id.commentLine);
Log.v("Slide", "Should be " + s );
Log.v("Slide", "Currently is "+ comm.getText());
for(String s : rawHTML.split("\\n\\n")){
comm.append(Html.fromHtml(s) );
}

Update "Log.i" in TextView with ScrollBar

I have in my layout.xml a TextView with "id = txtLog".
Where do the test results from my application using:
Log.i("Result:", "Value of x = " + x);
for show result in LogCat.
It is possible to show these results "Log.i" within the TextView?
Note: I left a space at the bottom of my application to show the TextView.
Like a console.
I would like to display these messages on TextView.
If possible create a scroll bar and display every time I use Log.i
I am a beginner, do not know if it is possible. Yet thanks.
I would think
myTextView.setText(myTextView.getText() + "Value of x = " + x + "\n");
would work.
EDIT:
Also, to make the TextView scrollable, you need to set a movement method like so:
myTextView.setMovementMethod(new ScrollingMovementMethod());
EDIT 2:
If you want the information to go to both Log.i and a TextView, then you need a method that holds a reference to the TextView you want to update.
public static void LogToView(TextView myTextView, String title, String message) {
Log.i(title, message);
myTextView.setText(myTextView.getText() + title + ": Value of x = " + x + "\n");
}
Put that in whatever class or in your Activity class. Use it instead of Log.i and the message will be passed to both.

Inline images with Text in android

I'm attempting to download webpages for a user to view later. So far, I've downloaded the html, and the images. I can get the HTML to show up nicely formatted, but I cannot get the images to inline.
The code I'm using so far:
This is my method to get the article.
MyImageGetter mig = new MyImageGetter(this, urlId);
Spanned span = Html.fromHtml(contents[1], mig, null);
contents[1] = span.toString();
titleView.setText(contents[0]);
content.setText(contents[1]);
contents[] is an array that contains two strings. contents[0] is a simple string, contents[1] is a string with HTML markup.
MyImageGetter:
public class MyImageGetter implements Html.ImageGetter{
String urlId = null;
Context c = null;
public MyImageGetter(ArticleViewer articleViewer, String urlId2) {
c = articleViewer;
urlId = urlId2;
}
public Drawable getDrawable(String source) {
String[] brokenUrl = source.split("/");
String imgName = brokenUrl[brokenUrl.length-1];
File image = new File("/data/data/com.theHoloDev.Reader/Offline/" + urlId + "/" + imgName);
Log.w("MyApp", image.getAbsolutePath());
Bitmap bm = BitmapFactory.decodeFile(image.getAbsolutePath());
Drawable d = new BitmapDrawable(c.getResources(), bm);
return d;
}
}
When I have it log image.getAbsolutePath() it comes up with a file that exists in ddms. The Text content is there perfectly, but there are still little black boxes that say obj in each image. I had thought a textview would still be able to display images in that fashion, but either I'm doing something wrong, or there is another way to do this.
What is your desired end result? It is confusing to see that you are displaying html but you want the user to see the page... without html or did you do that for debugging? I assume the users will not see the html but rather the the page itself. In this case, I would go for a webview.
http://developer.android.com/reference/android/webkit/WebView.html
I don't think the text view is meant to be used like you are using it.
Good luck.
I figured it out. Instead of:
Spanned span = Html.fromHtml(contents[1], mig, null);
contents[1] = span.toString();
content.setText(contents[1]);
I need:
Spanned span = Html.fromHtml(contents[1], mig, null);
content.setText(span);
I was running into problems setting span to string.

How to check whether the jigsaw puzzle is completed or not?

i am preparing one small game like jigsaw , for that i am using 9 imageview's with 9 different images in the layout. set the images to imageview at the time of starting those are actual images, after shuffle user will do sliding the images to complete puzzle, i want to check the modified image with actual image's, weather it's equal or not if those are equal popup a message, like gameover.
i tried like this
1.by using AND operator between the images(Drawables) but unlucky.
2.Using setLevel() for the images, compare those setLevel values with getLevel values for images after sliding still Unlucky.. here the problem is if i click on imageview one time getLevel() gives correct value, if i click more than once it gives zero value. Why it happens like this..
Please help me if you find any mistake in my code, otherwise guide me with good technique..
Xml code like this
<RelativeLayout
//9 imageview's
in jave code like this
if user will click on imageview it will swap the image's
img_View11.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(img_View21.getDrawable() == default_img ||
img_View12.getDrawable() == default_img) {
System.out.println("yes");
++click;
Noof_moves.setText("Moves: " +click);
ImageView iv = (ImageView)v;
d11 = iv.getDrawable();
prev_img = prev_imgView.getDrawable();
la11 = d11.getLevel();
System.out.println("d11 value is " +la11);
prev_imgView.setImageDrawable(d11);
img_View11.setImageDrawable(prev_img);
prev_imgView = img_View11;
check();
}
else { System.out.println("no"); }
}
});
void check(){
System.out.println("in checking condition");
if((lb11==la11) && (lb12==la12) && (lb13==la13) && (lb21==la21) && (lb22==la22)
&& (lb23==la23) && (lb31==la31) && (lb32==la32) && (lb33==la33)) {
Context c = getBaseContext();
System.out.println("gameover");
Toast.makeText(c, " GameOver ", Toast.LENGTH_SHORT).show();
}else{ System.out.println(" codition not checked "); }
}
//here lb=level before sliding
// la=level after sliding..
Ok, here is what you do for the solution.
maintain a new set of drawables marking them as original drawables that you had initially.
may be like
drawable og11 = imageView11.getDrawable();
do this part before shuffling. now you have original drawables, stored in the form of drawables.
After every click , check if og11 == imageView11.getDrawable(),... and so on for all the images in the jigsaw, if they match , it matches, else, they don't.
HTH.
I have a simple solution. Set serial number Integer tags to ImageViews using View.setTag(index) before jumbling(before preparing the puzzle.) Then everytime the user makes a move, loop through all the imageviews and check if they are in order. If out of order then puzzle is not completed yet.
class PuzzleItem {
Drawable puzzlepartImage;
int correctPosition;
public PuzzleItem(Drawable d, int index) {
puzzlepartImage = d;
correctPosition = index;
}
}
ArrayList<PuzzleItem> list = new ArrayList<PuzzleItem>();
for (int i = 0; i < 9; i++) {
list.add(new PuzzleItem(drawables[i], i));
}
Collections.shuffle(list);
//Create the views from this list and add to the layout serially.
// set the last view as emptyview.
On every move:
void onClick(View v) {
/* swap drawables */
Drawable clickedDrawable = v.getDrawable();
v.setDrawable(null);
mEmptyView.setDrawable(clickedDrawable);
mEmptyView = v;
/* swap tag integers */
Integer temp = (Integer)mEmptyView.getTag();
mEmptyView.setTag(v.getTag());
v.setTag(temp);
}
After every move check for completion:
for (int i = 0; i < 9; i++) {
if (imgView[i].getTag() != i) break;
}
if (i == 9)// puzzle completed.
why don't you use a gridView? and after each change, just check the items to see that images are in desired order
When you use == operator they are checking if both the objects are referencing the same object. But in your case they are 2 different objects.
You should set a tag to both the actual image and the other image. And check if they are both the same. That should work in your if condition.
For the drawable class i did notice a method setLevel and getLevel. You might be able to use this for your requirement.

Categories

Resources