繁体   English   中英

android在列表视图中的按钮上设置onclicklistener

[英]android setting onclicklistener on button in listview

更新!

我创建了一个自定义列表视图,到目前为止,我已经尝试在按钮上实现侦听器,但是它不起作用

这是我的主要活动

public class homepage extends Activity {

ListView list;
  String[] Name = {
    "Lovelle Ong",
      "Ryan Lopez",
      "Melissa Gan"
  } ;
  String[] Location = {
            "Botanical Gardens",
              "Cape Town",
              "Gardens By the Bay"
          } ;
  Integer[] imageId = {
      R.drawable.lovelle,
      R.drawable.ryanlopez,
      R.drawable.melissa
  };

  Integer[] mainId = {
          R.drawable.likedpage1,
          R.drawable.commentpage,
          R.drawable.melissap
      };

@Override
protected void onCreate(Bundle savedInstanceState) {


    super.onCreate(savedInstanceState);
    setContentView(R.layout.homepage);



     CustomList adapter = new
                CustomList(homepage.this, Name, Location, imageId, mainId);
            list=(ListView)findViewById(R.id.list);
                list.setAdapter(adapter);
                list.setClickable(true);
                list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view,
                                                int position, long id) {
                            Toast.makeText(getApplicationContext(),
                                      "Click ListItem Number " + Name [position], Toast.LENGTH_LONG)
                                      .show();  //this doesn't work too

                        }
                    });

        ImageButton back = (ImageButton) findViewById(R.id.imageButton1);
        back.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(v.getContext(),  MainPage.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
                finish(); 
            }

        });
 }
 }

我的适配器类

 public class CustomList extends ArrayAdapter<String>{
private final Activity context;
private final String[] Name, Location;
private final Integer[] imageId, mainId;
public CustomList(Activity context,
String[] Name, String[] Location, Integer[] imageId, Integer[] mainId) {
super(context, R.layout.list_single, Name);
this.context = context;
this.Name = Name;
this.imageId = imageId;
this.Location = Location;
this.mainId = mainId;


}
@Override
public View getView(int position, View view, ViewGroup parent) {

    LayoutInflater inflater = context.getLayoutInflater();
    View rowView= inflater.inflate(R.layout.list_single, null, true);

    TextView name = (TextView) rowView.findViewById(R.id.textView1);
    TextView location = (TextView) rowView.findViewById(R.id.textView2);
    ImageView profileView = (ImageView) rowView.findViewById(R.id.imageView1);
    Button mainImage = (Button) rowView.findViewById(R.id.profilepagelist1);
    Button comment = (Button) rowView.findViewById(R.id.buttonComment);
    final Button emptyheart = (Button) rowView.findViewById(R.id.imageView3);
    final Button filledheart = (Button)   rowView.findViewById(R.id.ImageViewRight);

    emptyheart.setOnClickListener(new View.OnClickListener()    //thisworks
    {
        @Override
        public void onClick(View v) {
            emptyheart.setVisibility(View.INVISIBLE);
            filledheart.setVisibility(View.VISIBLE);

        }

    });

    filledheart.setOnClickListener(new View.OnClickListener()   //thisworks
    {
        @Override
        public void onClick(View v) {
            emptyheart.setVisibility(View.VISIBLE);
            filledheart.setVisibility(View.INVISIBLE);
        }

    });

    comment.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v) {
            Log.d("fail", null, null);      
            // this.startActivity(new Intent(getBaseContext().this, commentpage.class));   <---- errors here

        }

    });

    name.setText(Name[position]);
    location.setText(Location[position]);
    profileView.setImageResource(imageId[position]);
    mainImage.setBackgroundResource(mainId[position]);



    return rowView;
}

}

我的list_single.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.muc2.MainActivity"
    tools:ignore="MergeRootFrame" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:id="@+id/mega1">
    <LinearLayout
        android:layout_width="match_parent"
        android:paddingLeft="2dp"
        android:paddingTop="2dp"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:paddingBottom="5dp"
    >
        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="50dp"
            android:layout_height="50dp"
             />

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/nameholders"
    >

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:paddingLeft="5dp"
                android:text="Lovelle Ong"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <Button
                android:id="@+id/buttonComment"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:layout_toLeftOf="@+id/imageView3"
                android:background="@drawable/comment" />

            <ImageView
                android:id="@+id/imageView3"
                android:layout_alignParentRight="true"
                android:layout_width="40dp"
                android:layout_height="40dp"
            android:src="@drawable/like" />

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/textView1"
                 android:layout_alignParentLeft="true"
                android:orientation="horizontal" >

                <ImageView
                    android:id="@+id/imageView2"
                    android:layout_width="23dp"
                    android:layout_height="23dp"
                    android:src="@drawable/geoicon" />

                <TextView
                    android:id="@+id/textView2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:paddingTop="2dp"
                    android:text=""
                    android:textAppearance="?android:attr/textAppearanceSmall" />
            </LinearLayout>

        </RelativeLayout>


    </LinearLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:paddingBottom="5dp"
        android:paddingLeft="2dp"
        android:paddingTop="2dp" >

        <Button
            android:id="@+id/profilepagelist1"
            android:layout_width="fill_parent"
            android:layout_height="310dp"
            android:layout_centerInParent="true"
            android:paddingBottom="5dp"
            android:paddingLeft="2dp"
            android:paddingRight="2dp" />
    </RelativeLayout>

</LinearLayout>

最后是包含列表视图的xml文件

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
tools:context="com.example.muc2.MainActivity$PlaceholderFragment"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:orientation="vertical"
 >

<RelativeLayout
    android:background="#000000"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <ImageButton
        android:id="@+id/imageButton1"
        android:layout_width="35dp"
        android:layout_height="35dp"
        android:background="@drawable/exit"
        android:maxHeight="35dp"
        android:maxWidth="35dp"
        android:paddingBottom="2dp"
        android:paddingLeft="2dp"
        android:paddingRight="2dp"
        android:paddingTop="2dp" 
        android:layout_alignParentRight="false"
         />

    <ImageButton
        android:id="@+id/imageButton2"
        android:layout_width="35dp"
        android:layout_height="35dp"
        android:background="@drawable/momentlogowhite"
        android:maxHeight="35dp"
        android:maxWidth="35dp"
        android:paddingBottom="2dp"
        android:paddingLeft="2dp"
        android:paddingRight="2dp"
        android:paddingTop="2dp" 
        android:layout_alignParentRight="true"
         />


    </RelativeLayout>

    <ListView
        android:id="@+id/list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
    </ListView>
</LinearLayout>

有没有一种方法可以在按钮上实现侦听器,并在单击按钮时将其引导至下一个活动?

感谢和对不起,很长的帖子!

我能够在单击时看到它,但是当我打算将其从一种活动切换到另一种活动时,就会出现错误。 我只用log.d来找出按钮是否工作

startActivity是Activity类的一种方法。 因此,您需要活动上下文。 你已经有了

this.context = context; 

所以用

context.startActivity(new Intent(context, commentpage.class)); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM