Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show Toast in a class extended by BaseAdapter Get View Method

I am having problem of showing Toast Message when i click a button within a list View. The problem is that i a custom class extended by BaseAdapter i have a method named GetView I wanna show a toast Message from that particular message

Following is my Listview Row Xml

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/tools"
    android:id="@+id/scrollView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    app:context=".HomeActivity" >

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <TableRow
            android:id="@+id/tableRow_laugh"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:ignore="UselessParent" >

                <TableLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentTop="true" >

                    <TableRow
                        android:id="@+id/tableRow_Header_laugh"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:background="#818085" >

                        <RelativeLayout
                            android:id="@+id/relative_header"
                            android:layout_width="wrap_content"
                            android:layout_height="fill_parent"
                            android:background="#818085" >

                            <ImageView
                                android:id="@+id/UserIconPic_laugh"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_alignParentLeft="true"
                                android:layout_alignParentTop="true"
                                android:layout_weight="0.4"
                                android:src="@drawable/ic_launcher"
                                app:ignore="ObsoleteLayoutParam,ContentDescription" />

                            <Button
                                android:id="@+id/btn_Vote_laugh"
                                android:layout_width="wrap_content"
                                android:layout_height="match_parent"
                                android:layout_alignParentRight="true"
                                android:layout_alignParentTop="true"
                                android:layout_weight=".4"
                                android:text="VOTE"
                                app:ignore="ObsoleteLayoutParam,HardcodedText" />

                            <TextView
                                android:id="@+id/txt_number_of_vote_laugh"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_alignBaseline="@+id/btn_Vote_laugh"
                                android:layout_alignBottom="@+id/btn_Vote_laugh"
                                android:layout_toRightOf="@+id/UserIconPic_laugh"
                                android:layout_weight=".4"
                                android:textAppearance="?android:attr/textAppearanceMedium"
                                app:ignore="ObsoleteLayoutParam,HardcodedText" />
                        </RelativeLayout>
                    </TableRow>

                    <TableRow
                        android:id="@+id/tableRow_MainText_laugh"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" >

                        <TextView
                            android:id="@+id/txt_MainText_laugh"
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            android:text="@string/whatweare_about_main"
                            android:textAppearance="?android:attr/textAppearanceMedium"
                            app:ignore="HardcodedText" />
                    </TableRow>

                    <TableRow
                        android:id="@+id/tableRow_Footer_laugh"
                        android:layout_width="wrap_content"
                        android:layout_height="fill_parent" >

                        <RelativeLayout
                            android:id="@+id/relative_footer"
                            android:layout_width="wrap_content"
                            android:layout_height="fill_parent"
                            android:background="#bababa" >

                            <Button
                                android:id="@+id/btn_Share_laugh"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:text="Share"
                                app:ignore="HardcodedText" />

                            <ImageButton
                                android:id="@+id/btn_facebook_laugh"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_centerVertical="true"
                                android:layout_toLeftOf="@+id/btn_twitter_laugh"
                                android:src="@drawable/pic2"
                                app:ignore="ContentDescription" />

                            <ImageButton
                                android:id="@+id/btn_twitter_laugh"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_alignParentRight="true"
                                android:layout_alignTop="@+id/btn_facebook_laugh"
                                android:src="@drawable/pic3"
                                app:ignore="ContentDescription" />
                        </RelativeLayout>
                    </TableRow>
                </TableLayout>
            </RelativeLayout>
        </TableRow>

        <TableRow
            android:id="@+id/tableRow_light"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:ignore="UselessParent" >

                <TableLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentTop="true" >

                    <TableRow
                        android:id="@+id/tableRow_header_light"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content" >

                        <RelativeLayout
                            android:id="@+id/RelativeLayout02"
                            android:layout_width="wrap_content"
                            android:layout_height="fill_parent"
                            android:background="#818085" >

                            <ImageView
                                android:id="@+id/UserIconPic_light"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_alignParentLeft="true"
                                android:layout_alignParentTop="true"
                                android:layout_weight="0.4"
                                android:src="@drawable/ic_launcher"
                                app:ignore="ObsoleteLayoutParam,ContentDescription" />

                            <Button
                                android:id="@+id/btn_Vote_light"
                                android:layout_width="wrap_content"
                                android:layout_height="match_parent"
                                android:layout_alignParentRight="true"
                                android:layout_alignParentTop="true"
                                android:layout_weight=".4"
                                android:text="VOTE"
                                app:ignore="ObsoleteLayoutParam,HardcodedText" />

                            <TextView
                                android:id="@+id/txt_vote_light"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_alignBaseline="@+id/btn_Vote_light"
                                android:layout_alignBottom="@+id/btn_Vote_light"
                                android:layout_toRightOf="@+id/UserIconPic_light"
                                android:layout_weight=".4"
                                android:textAppearance="?android:attr/textAppearanceMedium"
                                app:ignore="ObsoleteLayoutParam,HardcodedText" />
                        </RelativeLayout>
                    </TableRow>

                    <TableRow
                        android:id="@+id/tableRow_content_light"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" >

                        <FrameLayout
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content" >

                            <TextView
                                android:id="@+id/textView1"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_gravity="bottom|center"
                                android:maxLength="300"
                                android:text="@string/Main_laugh"
                                android:textAppearance="?android:attr/textAppearanceSmall"
                                app:ignore="HardcodedText" />

                            <ImageView
                                android:id="@+id/light_image"
                                android:layout_width="fill_parent"
                                android:layout_height="wrap_content"
                                app:ignore="ContentDescription" />
                        </FrameLayout>
                    </TableRow>

                    <TableRow
                        android:id="@+id/tableRow_Footer_light"
                        android:layout_width="wrap_content"
                        android:layout_height="fill_parent" >

                        <RelativeLayout
                            android:id="@+id/RelativeLayout01"
                            android:layout_width="wrap_content"
                            android:layout_height="fill_parent"
                            android:background="#bababa" >

                            <Button
                                android:id="@+id/Btn_Share_light"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:text="Share"
                                app:ignore="HardcodedText" />

                            <ImageButton
                                android:id="@+id/btn_facebook_light"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_centerVertical="true"
                                android:layout_toLeftOf="@+id/btn_twitter_light"
                                android:src="@drawable/pic2"
                                app:ignore="ContentDescription" />

                            <ImageButton
                                android:id="@+id/btn_twitter_light"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_alignParentRight="true"
                                android:layout_alignTop="@+id/btn_facebook_light"
                                android:src="@drawable/pic3"
                                app:ignore="ContentDescription" />
                        </RelativeLayout>
                    </TableRow>
                </TableLayout>
            </RelativeLayout>
        </TableRow>

        <TableRow
            android:id="@+id/tableRow_fun"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:ignore="UselessParent" >

                <TableLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentTop="true" >

                    <TableRow
                        android:id="@+id/tableRow_header_fun"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content" >

                        <RelativeLayout
                            android:id="@+id/RelativeLayout04"
                            android:layout_width="wrap_content"
                            android:layout_height="fill_parent"
                            android:background="#818085" >

                            <ImageView
                                android:id="@+id/UserIconPic_fun"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_alignParentLeft="true"
                                android:layout_alignParentTop="true"
                                android:layout_weight="0.4"
                                android:src="@drawable/ic_launcher"
                                app:ignore="ObsoleteLayoutParam,ContentDescription" />

                            <Button
                                android:id="@+id/btn_vote_fun"
                                android:layout_width="wrap_content"
                                android:layout_height="match_parent"
                                android:layout_alignParentRight="true"
                                android:layout_alignParentTop="true"
                                android:layout_weight=".4"
                                android:text="VOTE"
                                app:ignore="ObsoleteLayoutParam,HardcodedText" />

                            <TextView
                                android:id="@+id/txt_vote_number_fun"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_alignBaseline="@+id/btn_vote_fun"
                                android:layout_alignBottom="@+id/btn_vote_fun"
                                android:layout_toRightOf="@+id/UserIconPic_fun"
                                android:layout_weight=".4"
                                android:textAppearance="? android:attr/textAppearanceMedium"
                                app:ignore="ObsoleteLayoutParam,HardcodedText" />
                        </RelativeLayout>
                    </TableRow>

                    <TableRow
                        android:id="@+id/tableRow_Content_fun"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" >

                        <RelativeLayout
                            android:id="@+id/relativelayout"
                            android:layout_width="fill_parent"
                            android:layout_height="fill_parent" >

                            <ImageView
                                android:id="@+id/image_fun"
                                android:layout_width="fill_parent"
                                android:layout_height="fill_parent"
                                android:adjustViewBounds="true"
                                android:baselineAlignBottom="true"
                                android:cropToPadding="true"
                                app:ignore="ContentDescription" />

                            <TextView
                                android:id="@+id/myImageViewText"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_alignBottom="@+id/image_fun"
                                android:layout_alignLeft="@+id/image_fun"
                                android:layout_alignRight="@+id/image_fun"
                                android:layout_alignTop="@+id/image_fun"
                                android:layout_margin="1dp"
                                android:gravity="center"
                                android:text="@string/main_light"
                                android:textColor="#000000" />
                        </RelativeLayout>
                    </TableRow>

                    <TableRow
                        android:id="@+id/tableRow_Footer_fun"
                        android:layout_width="wrap_content"
                        android:layout_height="fill_parent" >

                        <RelativeLayout
                            android:id="@+id/RelativeLayout03"
                            android:layout_width="wrap_content"
                            android:layout_height="fill_parent"
                            android:background="#bababa" >

                            <Button
                                android:id="@+id/Btn_share_fun"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:text="Share"
                                app:ignore="HardcodedText" />

                            <ImageButton
                                android:id="@+id/btn_facebook_fun"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_centerVertical="true"
                                android:layout_toLeftOf="@+id/btn_twitter_fun"
                                android:src="@drawable/pic2"
                                app:ignore="ContentDescription" />

                            <ImageButton
                                android:id="@+id/btn_twitter_fun"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_alignParentRight="true"
                                android:layout_alignTop="@+id/btn_facebook_fun"
                                android:src="@drawable/pic3"
                                app:ignore="ContentDescription" />
                        </RelativeLayout>
                    </TableRow>
                </TableLayout>
            </RelativeLayout>
        </TableRow>
    </TableLayout>

</ScrollView>

My CustomeAdapter Class code

 package com.example.laysapp.ContentListView;
 import java.util.ArrayList;
 import java.util.List;

 import com.example.laysapp.R;
 import com.example.laysapp.RegisteredUser.LighterShare;

 import android.app.Application;
 import android.content.Context;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.View.OnClickListener;
 import android.view.ViewGroup;
 import android.webkit.WebView.FindListener;
 import android.widget.BaseAdapter;
 import android.widget.Button;
 import android.widget.ImageView;
 import android.widget.TextView;
 import android.widget.Toast;

 public class ContentItemAdapter extends BaseAdapter {

private final List<Content_Items> items;

public ContentItemAdapter(final Context context, final int itemResId,
        final ArrayList<Content_Items> items2) {
    this.items = items2;
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return items.size();
}

@Override
public Object getItem(int arg0) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
}

@SuppressWarnings("null")
@Override
public View getView(int position, View convertView, ViewGroup parent) {

    final Content_Items item = this.items.get(position);
    View itemView = null;

    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) parent.getContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        itemView = inflater.inflate(R.layout.contentlistrow, null);
    } else {
        itemView = convertView;
    }

    // Set the text of the Laugh Row Start
    TextView txtLaugh_Vote = (TextView) itemView
            .findViewById(R.id.txt_number_of_vote_laugh);
    txtLaugh_Vote.setText(item.getLaughNo_Vote());

    TextView txtlaugh_Content = (TextView) itemView
            .findViewById(R.id.txt_MainText_laugh);
    txtlaugh_Content.setText(item.getLaughContent());

    ImageView imgView_Laugh_UserPic = (ImageView) itemView
            .findViewById(R.id.UserIconPic_laugh);

    imgView_Laugh_UserPic.setImageBitmap(item.getLaughtUserPic());

    // Set the text of the Laugh Row End

    // Setting of Light Row Start

    TextView txtLight_Vote = (TextView) itemView
            .findViewById(R.id.txt_vote_light);
    txtLight_Vote.setText(item.getLaughNo_Vote());

    TextView txtlight_Content = (TextView) itemView
            .findViewById(R.id.textView1);
    txtlight_Content.setText(item.getLaughContent());

    ImageView imgView_Light_UserPic = (ImageView) itemView
            .findViewById(R.id.UserIconPic_light);

    imgView_Light_UserPic.setImageBitmap((item.getLightStoriesUserPic()));

    ImageView imgView_LightContent_Image = null;
    // Check weather ContentImage is Available For Light

    String Check = item.getIsLightContentAvaiable();
    if (Check == "yes") {
        imgView_LightContent_Image = (ImageView) imgView_LightContent_Image
                .findViewById(R.id.light_image);
        imgView_LightContent_Image.setImageBitmap(item
                .getLightStoriesContentImagePic());
    }

    // Setting of Light Row End

    // Set the text of the Funto Row Start

    TextView txtfunto_Vote = (TextView) itemView
            .findViewById(R.id.txt_vote_number_fun);
    txtfunto_Vote.setText(item.getLaughNo_Vote());

    TextView txtfunto_Content = (TextView) itemView
            .findViewById(R.id.myImageViewText);
    txtfunto_Content.setText(item.getLaughContent());

    // Set the text of the Funto Row End

    ImageView imgView_funto_UserPic = (ImageView) itemView
            .findViewById(R.id.UserIconPic_fun);

    imgView_funto_UserPic.setImageBitmap(item.getfuntoUserPic());

    ImageView imgView_funto_ContentPic = (ImageView) itemView
            .findViewById(R.id.image_fun);

    imgView_funto_ContentPic.setImageBitmap(item.getfuntoContentImagePic());

    Button MainVote_Laugh = null;
    MainVote_Laugh = (Button) MainVote_Laugh
            .findViewById(R.id.btn_Vote_laugh);
    MainVote_Laugh.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            Toast.makeText(getApplication(), "Clicked Laugh Vote", Toast.LENGTH_SHORT).Show();
        }
    });

    return itemView;
}
}
like image 383
Aftab Ali Avatar asked Dec 18 '12 06:12

Aftab Ali


3 Answers

As you switch from your MainActivity context changes, you have to use your AdapterClass > ViewHolderClass > ViewHolder's constructor argument to get context for adapter.

Here is an example.

    public MyViewHolder(final View itemView) {
        super(itemView);

        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Toast.makeText(itemView.getContext(),"Hi",Toast.LENGTH_LONG);
            }
        });

    }

otherwise you can also pass context to a constructor argument for any other class not having activity.

example.

public class MyClass {
Context c;
public MyClass(Context context) {
     c = context;
 }}
like image 196
Prashant Abdare Avatar answered Nov 01 '22 03:11

Prashant Abdare


Replace this line:

Toast.makeText(getApplication(), "Clicked Laugh Vote", Toast.LENGTH_SHORT).show();

with this line:

 Toast.makeText(v.getContext(), "Clicked Laugh Vote", Toast.LENGTH_SHORT).show();
like image 26
Andro Selva Avatar answered Nov 01 '22 05:11

Andro Selva


You can also use parent.getContext(), full code of toast is like below

Toast.makeText(parent.getContext(), "view clicked", Toast.LENGTH_SHORT).show();
like image 41
shweta c Avatar answered Nov 01 '22 03:11

shweta c