0

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

我想单击一个按钮,它将把它带到another activity第二个按钮将remove the row来自列表

我该怎么做?请帮忙。

4

3 回答 3

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.
于 2013-02-21T09:09:55.573 回答
0

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

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

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

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

于 2013-02-22T07:01:18.477 回答
0

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

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

我希望这对你有帮助:)

于 2013-02-21T06:23:26.570 回答