且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为什么我不能点击在Android中使用SimpleAdapter一个ListView一个按钮?

更新时间:2023-02-05 22:19:48

activity_main.xml中没有按钮 ID为 mytask 。所以你不能在MainActivtiy初始化按钮。

按钮属于 mytask.xml 覆盖getView并有按钮听者那里。

http://developer.android.com/reference/android/widget/ SimpleAdapter.html

编辑:

  SimpleAdapter K =新SimpleAdapter(这一点,VAL1,R.layout.mytask,新的String [] {A,C,B},新的INT [] {R.id.View1,R.id.View2,R.id.ViewStatus})
    {
        @覆盖
        公共查看getView(INT位置,查看convertView,父母的ViewGroup)
        {
            视图V = super.getView(位置,convertView,父母);             按钮B =(按钮)v.findViewById(R.id.mytask);
             b.setOnClickListener(新OnClickListener(){                @覆盖
                公共无效的onClick(查看为arg0){
                    // TODO自动生成方法存根
                    Toast.makeText(MainActivity.this,拯救,Toast.LENGTH_SHORT).show();
                }
            });
            返回伏;
        }
    };

编辑:既然你不相信这里的单元

为什么我不能点击在Android中使用SimpleAdapter一个ListView一个按钮?

I am a newbie to Android. I have posted this question earlier too but didn't find the appropriate answer. My requirement is to make a Button clickable in a Listviewwhich is generated using a SimpleAdapter.I don't want to use CustomAdapter,BaseAdapter or any other Adapter. I don't want to extend my Activty as SimpleAdapter.My code and error logs are as follows. If anyone has any solutions please explain to me step by step. Thank you.

MainActivity.java

 public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ArrayList<HashMap<String, String>>val1=new ArrayList<HashMap<String,String>>(); 
    HashMap<String, String>val=new HashMap<String,String>();
    val.put("a","a");
    val.put("c","c");
    val.put("b","b");
    val1.add(val);

     final ListView l=(ListView)findViewById(R.id.listView1);
     ListAdapter k=new SimpleAdapter(this,val1,R.layout.mytask,new String[]{"TaskId","heading","status"},new int[]{R.id.View1,R.id.View2,R.id.ViewStatus});

     Button b=(Button)findViewById(R.id.mytask);
     b.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Toast.makeText(MainActivity.this,"save",Toast.LENGTH_SHORT).show();
        }
    });
        l.setAdapter(k);




}

Error log:

  04-07 01:43:07.075: E/AndroidRuntime(2113): FATAL EXCEPTION: main
  04-07 01:43:07.075: E/AndroidRuntime(2113): Process: com.example.s, PID: 2113
  04-07 01:43:07.075: E/AndroidRuntime(2113): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.s/com.example.s.MainActivity}: java.lang.NullPointerException
  04-07 01:43:07.075: E/AndroidRuntime(2113):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
  04-07 01:43:07.075: E/AndroidRuntime(2113):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
  04-07 01:43:07.075: E/AndroidRuntime(2113):   at android.app.ActivityThread.access$800(ActivityThread.java:135)
  04-07 01:43:07.075: E/AndroidRuntime(2113):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
  04-07 01:43:07.075: E/AndroidRuntime(2113):   at android.os.Handler.dispatchMessage(Handler.java:102)
  04-07 01:43:07.075: E/AndroidRuntime(2113):   at android.os.Looper.loop(Looper.java:136)
  04-07 01:43:07.075: E/AndroidRuntime(2113):   at android.app.ActivityThread.main(ActivityThread.java:5017)
  04-07 01:43:07.075: E/AndroidRuntime(2113):   at java.lang.reflect.Method.invokeNative(Native Method)
  04-07 01:43:07.075: E/AndroidRuntime(2113):   at java.lang.reflect.Method.invoke(Method.java:515)
  04-07 01:43:07.075: E/AndroidRuntime(2113):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
  04-07 01:43:07.075: E/AndroidRuntime(2113):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
  04-07 01:43:07.075: E/AndroidRuntime(2113):   at dalvik.system.NativeStart.main(Native Method)
 04-07 01:43:07.075: E/AndroidRuntime(2113): Caused by: java.lang.NullPointerException
 04-07 01:43:07.075: E/AndroidRuntime(2113):    at com.example.s.MainActivity.onCreate(MainActivity.java:34)
 04-07 01:43:07.075: E/AndroidRuntime(2113):    at android.app.Activity.performCreate(Activity.java:5231)
 04-07 01:43:07.075: E/AndroidRuntime(2113):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
 04-07 01:43:07.075: E/AndroidRuntime(2113):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
 04-07 01:43:07.075: E/AndroidRuntime(2113):    ... 11 more

mytask.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical" 

>

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="18dp"
    android:text="Task Name:"

    android:textSize="15dp"
    android:textStyle="bold" />

<TextView
    android:id="@+id/View1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:text="TextView"

    android:textSize="15dp"
    android:textStyle="bold"
    android:visibility="invisible" />

<TextView
    android:id="@+id/View2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/textView1"
    android:layout_alignBottom="@+id/textView1"
    android:layout_marginLeft="31dp"
    android:layout_toRightOf="@+id/textView1"
    android:text="TextView"

    android:textSize="15dp"
    android:textStyle="bold"
    android:visibility="visible" />

<TextView
    android:id="@+id/ViewStatus"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/View2"
    android:layout_alignBottom="@+id/View2"
    android:layout_alignParentRight="true"
    android:text="Pending"

    android:textSize="15dp"
    android:textStyle="bold" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/ViewStatus"
    android:layout_alignBottom="@+id/ViewStatus"
    android:layout_marginRight="14dp"
    android:layout_toLeftOf="@+id/ViewStatus"
    android:text="Status:"

    android:textSize="15dp"
    android:textStyle="bold" />

<Button
    android:id="@+id/mytask"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/textView1"
    android:text="Button" />

 </RelativeLayout>

activity_main.xml

 <ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="106dp"
    android:layout_marginTop="124dp" >
</ListView>

activity_main.xml does not have Button with id mytask. So you cannot initialize Button in MainActivtiy.

The Button belongs to mytask.xml override getView and have the button listener there.

http://developer.android.com/reference/android/widget/SimpleAdapter.html

Edit:

   SimpleAdapter k=new SimpleAdapter(this,val1,R.layout.mytask,new String[]{"a","c","b"},new int[]{R.id.View1,R.id.View2,R.id.ViewStatus})
    {
        @Override
        public View getView (int position, View convertView, ViewGroup parent)
        {
            View v = super.getView(position, convertView, parent);

             Button b=(Button)v.findViewById(R.id.mytask);
             b.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    Toast.makeText(MainActivity.this,"save",Toast.LENGTH_SHORT).show();
                }
            });
            return v;
        }


    };

Edit: Since you are not convinced here's the snap