android - 如何在 BaseAdapter Get View 方法扩展的类中显示 Toast

标签 android android-layout listview baseadapter listview-adapter

当我单击 ListView 中的按钮时,我遇到了显示 Toast 消息的问题。 问题是我有一个由 BaseAdapter 扩展的自定义类,我有一个名为 GetView 的方法 我想显示来自该特定消息的 toast 消息

以下是我的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>

我的 CustomeAdapter 类代码

 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;
}
}

最佳答案

替换这一行:

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

用这一行:

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

关于android - 如何在 BaseAdapter Get View 方法扩展的类中显示 Toast,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13927601/

相关文章:

使用 SimpleAdapter 的 Android ListActivity - 突出显示所选项目

安卓 ListView : Detect if ListView data fits on screen without scrolling

java - 在 Dagger 2 中使用 AndroidInjections 贡献模块(抽象类)作为子组件

java - 使用 Cyber​​Garage Cyber​​Link for Java 的 Android 简单 UPnP/DLNA 控制点

android - 在 LinearLayout 中更改 LayoutParams 的动画

android - 使用 'setAlpha' 不改变按钮的透明度

android - 水平 facebook 样式 slider

android - 单击 ListView 项时如何更改按钮背景

java - 是否可以将个人资料图片与注册一起上传到 Fire Base 服务器?

java - 在 Google map 上使用 JSON 表示经纬度