Android: Cannot cast from View to Gallery - android

I'd like to make a Gallery in android. In order to set the adapter in need to get the gallery which I defined in my xml file. Im doing that as:
Gallery g = (Gallery) findViewById(R.id.gallery1);
But with this code I can't compile my project, since I get the Error "Cannot cast from View to Gallery". My xml-file looks like this:
"
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Gallery android:id="#+id/gallery1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="bottom"
/>
</LinearLayout>
Anybody knows how to solve my problem? Thanks a lot

Your code looks fine.
Check if you have the correct import for Gallery
import android.widget.Gallery;

I think you just created the Object instance for Gallery. Gallery is a AdapterView. So you must set Adapter for the Content. please check this sample

Related

Loading the correct Image in List View

I'm making an app that displays images from urls in a listview. I use a asynctask to get the image and place it into the imageviews. The way I currently have it is that an asynctask is called in the getview method. The do in background part gets the image from the url. The problem I'm having is when I scroll to an element in the listview, initially the wrong image is there but the correct image is quickly loaded. Why is this happening?
xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitStart"
android:src="#drawable/ic_launcher" />
</LinearLayout>
Make sure that you are setting your ImageView to null as soon as you declare it. It's hard to tell without knowing what the image is, but it's possible that the ListView is reusing old images. Reference below:
wrong images are displaying first and then correct images are displaying during scrolling of listview

QuiltView Gallery with Pull to Refresh in Android?

I am working on an social networking app in android where I was using QuiltViewLibrary for making a QuiltView Gallery of images.
In the same application I have used the HANDMARKS Pull to Refresh library for the GridView.
Both are working fine. Now I have to implement both the libraries for the combined task, that is PULL TO REFRESH in QuiltView GALLERY.
The problem which I am getting is to combine the XML of both the LIBRARIES which are totally different.
XML for PullToRefresh with simple GRID VIEW:
<!-- The PullToRefreshGridView replaces a standard GridView widget. -->
<com.handmark.pulltorefresh.library.PullToRefreshGridView
xmlns:ptr="http://schemas.android.com/apk/res-auto"
android:id="#+id/pull_refresh_grid"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:numColumns="auto_fit"
android:verticalSpacing="1dp"
android:horizontalSpacing="1dp"
android:columnWidth="100dp"
android:stretchMode="columnWidth"
android:gravity="fill"
ptr:ptrMode="both"
ptr:ptrDrawable="#drawable/ic_launcher" />
</LinearLayout>
XML for QuiltView Gallery:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:quilt="http://schemas.android.com/apk/res/com.tv.photos"
android:id="#+id/FrameLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white" >
<com.tv.photos.QuiltView
android:id="#+id/quilt"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dip" >
</com.tv.photos.QuiltView>
</FrameLayout>
Please let me know if anyone can suggest me something.
This is quite simple:
Chris Banes' pulltorfresh library supports ScrollView
The QuiltView library is just an extension of a GridLayout (not GridView or any other adapter class)
Put an instance of QuiltView within an instance of PullToRefreshScrollView. Example java, xml.
Shouldn't be any more complicated than this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<com.handmark.pulltorefresh.library.PullToRefreshScrollView
xmlns:ptr="http://schemas.android.com/apk/res-auto"
android:id="#+id/pull_refresh_scrollview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<com.tv.photos.QuiltView
android:id="#+id/quilt"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dip" />
</com.handmark.pulltorefresh.library.PullToRefreshScrollView>
</LinearLayout>
You can continue to use handmark's pulltorefresh library and create a pulltorefresh view that works with quiltview inside it.
Since QuiltView extends FrameLayout, you cannot use it directly in handmark's pull to refresh, but you have 3 options in front of you:
1. Create new class of PullToRefreshQuiltView by extending PullToRefreshBase class.
Just create a class extending PullToRefreshBase
You can check PullToRefreshWebView class to get an idea of how to do that.
2. Use the scrollview in QuiltView's setup function
See line 50 of this link
You can either change the scrollview in setup function to a pullToRefreshScrollView, or add this scrollView to a ptr view.
3. Use RecyclerView with GridLayoutManager inside Handmark's PullToRefresh or android SwipeRefreshLayout
The other option is to use a RecyclerView with a GridLayoutManager ( Something that looks like GridLayoutManager Span Size RecycleView ) inside handmark's pulltorefresh or android's SwipeRefreshLayout.
For more information about SwipeRefreshLayout check the android docs.

accessing objects of xml layout android

I have an activity (MainActivity.java) in which content view is like this
this.setContentView(R.layout.standalone_example);
my standalone_example.xml is like this
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<com.abc.view.PageCurlView
android:id="#+id/dcgpagecurlPageCurlView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#drawable/page1" />
</RelativeLayout>
I am done some work in PageCurlView.java now I want to access objects and variables of PageCurlView.java in my MainActivity.java, I am searching for hours but couldnt find any answer, any suggestions thanks in advance.
You can get a reference to the inflated PageCurlView using findViewById() in your activity. In a method in your activity:
PageCurlView mPageCurlView = (PageCurlView) findViewById(R.id.dcgpagecurlPageCurlView1);
You can then call methods or access public variables on mPageCurlView. Please note that setContentView() must be called first in your activity before you can use findViewById().

Dispalying image in android

I am trying to display an image in android.The process is i am downloading 1 image from net and trying to display it in the screen,the problem i am facing is in the activity screen i cant able to set the content view and the image.Following is the code:
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
</LinearLayout>
Activity Screen:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.);---->Here i cant set as main
Bitmap bitmap = DownloadImage(
"http://www.allindiaflorist.com/imgs/arrangemen4.jpg");
ImageView img = (ImageView) findViewById(R.id.); //here i cant set the image id
img.setImageBitmap(bitmap);
}
Use Android Smart Image View. That is the easiest way to display an image using a URL. Here is the link.
Clean your project.sometimes it don't take R.java file.
Just clean the project using Project -> Clean. If it still exists, run the project, and they will usually disappear.

ListView with checkbox - why the checkbox doesn't show?

I'm using the ListView to provide a list for the user to choose from.
Here is the main code:
SimpleAdapter adapter = new SimpleAdapter(this,contacts, R.layout.list_contact,
from_contacts, to_contacts);
listview_selected_contact.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listview_selected_contact.setAdapter(adapter);
I wonder why the checkbox doesn't show?
The program run properly just without the visible checkbox.
Can anyone help?
Here is the xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView" android:textAppearance="?android:attr/textAppearanceMedium" android:id="#+id/username"></TextView>
</LinearLayout>
It looks like that your R.layout.list_contact isn't CheckedTextView.
See source of android.R.layout.simple_list_item_multiple_choice
I can recommend you to build custom view which is child of any ViewGroup classes and implementator of interface Checkable.
id of checkBox and listView in R.layout.list_contact should be specific .
so follow ApiDemoes multiple List example for both layout and java code .

Categories

Resources