Why can't I click a Button in a ListView using SimpleAdapter in android?

本秂侑毒 提交于 2019-12-29 09:47:33

问题


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>

回答1:


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




回答2:


Write this attribute android:descendantFocusability="blocksDescendants" in your mytask.xml relative layout.

And in main activity put a,c,b in place of "TaskId","heading","status".



来源:https://stackoverflow.com/questions/22905041/why-cant-i-click-a-button-in-a-listview-using-simpleadapter-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!