我是Android的新手。我也提前发布了这个问题,但没有找到合适的答案。我的要求是使用Button
生成的Listview
中SimpleAdapter
可点击。我不想使用CustomAdapter
,BaseAdapter
或任何其他适配器。我不想将我的Activty扩展为SimpleAdapter
。我的代码和错误日志如下。如果有人有任何解决方案,请一步一步向我解释。谢谢。
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);
}
错误日志:
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>
答案 0 :(得分:5)
activity_main.xml
没有Button
,标识为mytask
。因此,您无法在MainActivtiy中初始化Button。
Button
属于mytask.xml
覆盖getView并在那里有按钮监听器。
http://developer.android.com/reference/android/widget/SimpleAdapter.html
编辑:
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;
}
};
编辑:因为你不相信这里的快照
答案 1 :(得分:0)
将此属性android:descendantFocusability="blocksDescendants"
写入mytask.xml
相对布局。
在主要活动中,a,c,b
代替"TaskId","heading","status"
。