【问题标题】:Android Listview with clickable buttons带有可点击按钮的 Android Listview
【发布时间】:2013-02-21 06:11:38
【问题描述】:

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

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

我该怎么做?请帮忙。

【问题讨论】:

  • 使用自定义列表视图我认为它会帮助你
  • 我使用了自定义列表视图,将我的按钮可聚焦属性设置为 false,单击按钮时可以在日志中打印一条消息,但它在公共适配器类中,我不能将活动称为它是一个单独的类,因此也无法生成将其发送到其他活动的意图。
  • 你能告诉我如何在单击列表中的按钮时从列表中删除一行吗?

标签: android listview custom-adapter


【解决方案1】:

通过使用这个方法你可以得到Listview项目的位置。然后你需要设置什么。

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

希望对你有帮助:)

【讨论】:

    【解决方案2】:
    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.
    

    【讨论】:

      【解决方案3】:

      谢谢大家的回答,我是这样想的:

      要让一行中的一个按钮带我去另一个活动-

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-12-15
        • 1970-01-01
        • 2017-02-12
        • 2021-02-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多