带有可点击按钮的Android Listview

时间:2013-02-21 06:11:39

标签: android listview custom-adapter

我有一个ListView,其中有3个按钮。所有这些都可以点击。这是一个自定义的ListView,其中包含一个单独的CustomAdapter类。

我想点击一个按钮,将其带到another activity,第二个按钮将remove the row从列表中

我该怎么办?请帮忙。

3 个答案:

答案 0 :(得分:0)

通过使用此方法,您可以获得Listview项目位置,然后获取您需要设置的任何内容。

public void onListItemClick(ListView parent, View v, int position,long id) { selection.setText(items[position]); }

我希望这对你有帮助:))

答案 1 :(得分:0)

For this we have to customize the layout instead of listview. it is not possible with listview because listview takes items so individual onclick methods developing is not possible.see the following code. it will help you alot.

Activity:

package com.example.customlistexample;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TableLayout.LayoutParams;
import android.widget.TextView;

public class SearchResults extends Activity {
    /** Called when the activity is first created. */



    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        /** create a new layout to display the view */
        final LinearLayout mainLinearLayout = (LinearLayout) findViewById(R.id.mainLinearLayout);

        String listData[] = new String[]{"A","B","C","D","E","F","G"};





            for (int i = 0; i < listData.length; i++) {

                TextView tv1 = new TextView(this);
                tv1.setText(listData[i]);
                tv1.setLayoutParams(new LayoutParams(50,50,Gravity.LEFT));
                tv1.setTextColor(Color.BLACK);
                tv1.setTypeface(null, Typeface.BOLD);
                tv1.setTextSize(TypedValue.COMPLEX_UNIT_PX, 22);


                    ImageView iv1 = new ImageView(this);
                    iv1.setLayoutParams(new LayoutParams(50, 50,Gravity.LEFT));
                    iv1.setImageResource(R.drawable.ic_launcher);




                    ImageView iv2 = new ImageView(this);
                    iv2.setImageResource(R.drawable.ic_launcher);
                    iv2.setLayoutParams(new LayoutParams(50,50,Gravity.LEFT));




                    final LinearLayout ll3 = new LinearLayout(this);
                    ll3.setOrientation(LinearLayout.HORIZONTAL);
                    ll3.setLayoutParams(new LayoutParams(
                            LayoutParams.WRAP_CONTENT,
                            LayoutParams.WRAP_CONTENT,Gravity.CENTER_HORIZONTAL));
                    ll3.addView(tv1);
                    ll3.addView(iv1);
                    ll3.addView(iv2);
                    ll3.setPadding(0, 20, 0, 25);


                    mainLinearLayout.addView(ll3);

                    final LinearLayout ll5 = new LinearLayout(this);
                    ll5.setLayoutParams(new LayoutParams(1, 1));
                    ll5.setBackgroundColor(Color.parseColor("#CCCCCC"));
                    mainLinearLayout.addView(ll5);
                    mainLinearLayout.setPadding(10, 10, 10, 30);

                    iv1.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            // TODO Auto-generated method stub
                            startActivity(new Intent(SearchResults.this,SecondActivity.class));

                        }
                    });
                    iv2.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            // TODO Auto-generated method stub
                            mainLinearLayout.removeView(ll3);
                            mainLinearLayout.removeView(ll5);
                        }
                    });
                }
            }

}

Layout:


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".SearchResults" >

 <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
         android:orientation="vertical">


        <LinearLayout
            android:id="@+id/mainLinearLayout"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:layout_gravity="left"
            android:orientation="vertical">


            <TextView
                android:id="@+id/tv_results"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Results"
                android:layout_marginTop="10dp"
                android:layout_marginBottom="10dp"
                android:layout_marginLeft="10dp"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:textColor="#800000"
                android:textSize="22px"
                android:textStyle="bold" >
            </TextView>
        </LinearLayout>
    </ScrollView>

</RelativeLayout>

try this definately helps you. Let me know your doubt is clarified or not.

答案 2 :(得分:0)

谢谢大家的答案,我已经想到了这个:

要使其中一个按钮转到另一个活动 -

我在主活动中创建了一个私有适配器类,当我单击该按钮时,它的onClickListener会调用Activity中的另一个公共函数,该函数会触发另一个Activity的意图。

但是我仍然不确定如何在单击其中的按钮时删除一行。