我正在使用列表视图,它使用自定义布局,布局包含两个textview和两个按钮。 这是代码。 。
<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="wrap_content"
android:background="@drawable/border_week_flash_card" >
<TextView
android:id="@+id/txt_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_margin="12dp"
android:textSize="20sp" />
<Button
android:id="@+id/bPractice_deck"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@id/txt_title"
android:layout_centerHorizontal="true"
android:background="@drawable/week_button"
android:onClick="startSwipeViewActiviy"
android:text="Practice this deck ->" />
<TextView
android:id="@+id/txt_progress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/bPractice_deck"
android:layout_margin="12dp"
android:text="See Your progress by test . . ."
android:textSize="20sp" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@id/txt_progress"
android:background="@drawable/week_button"
android:text="Take test to see progress->" />
</RelativeLayout>
和我的主要java代码是。 。 。
public class Course_Week extends ListActivity {
Button bPractice;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
final Context context = this;
// bPractice=(Button)findViewById(R.id.bPractice_deck);
final String[] course = { "Week 1", "Week 2", "Week 3", "Week 4", "Week 5",
"Week 6", "Week 7", "Week 8", "Week 9", "Week 10", "Week 11",
"Week 12" };
ArrayAdapter<String> adopter = new ArrayAdapter<String>(this,
R.layout.week_flash_card, R.id.txt_title, course) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
super.getView(position, convertView, parent);
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.week_flash_card, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.txt_title);
//ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
textView.setText(course[position]);
// Change the icon for Windows and iPhone
switch(postion){
case 0:
//when button on first week click
break;
case 1:
//when button on 2nd week click
break;
}
return rowView;
}
};
setListAdapter(adopter);
}
}
问题是我不知道如何实现onclick操作,因为我想在点击每个不同的列表按钮时执行不同的操作。 即我想点击第1周的“练习这个牌”按钮时执行不同的操作,而按第2周“练习这个牌组按钮”则不同。 。
06-28 22:21:49.003: E/AndroidRuntime(14739): FATAL EXCEPTION: main
06-28 22:21:49.003: E/AndroidRuntime(14739): Process: com.saru.chaudhary.wpmeasy, PID: 14739
06-28 22:21:49.003: E/AndroidRuntime(14739): java.lang.NullPointerException
06-28 22:21:49.003: E/AndroidRuntime(14739): at com.saru.chaudhary.wpmeasy.Course_Week$1.getView(Course_Week.java:37)
06-28 22:21:49.003: E/AndroidRuntime(14739): at android.widget.AbsListView.obtainView(AbsListView.java:2255)
06-28 22:21:49.003: E/AndroidRuntime(14739): at android.widget.ListView.makeAndAddView(ListView.java:1790)
06-28 22:21:49.003: E/AndroidRuntime(14739): at android.widget.ListView.fillDown(ListView.java:691)
06-28 22:21:49.003: E/AndroidRuntime(14739): at android.widget.ListView.fillFromTop(ListView.java:752)
06-28 22:21:49.003: E/AndroidRuntime(14739): at android.widget.ListView.layoutChildren(ListView.java:1630)
06-28 22:21:49.003: E/AndroidRuntime(14739): at android.widget.AbsListView.onLayout(AbsListView.java:2087)
06-28 22:21:49.003: E/AndroidRuntime(14739): at android.view.View.layout(View.java:14860)
06-28 22:21:49.003: E/AndroidRuntime(14739): at android.view.ViewGroup.layout(ViewGroup.java:4643)
06-28 22:21:49.003: E/AndroidRuntime(14739): at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
06-28 22:21:49.003: E/AndroidRuntime(14739): at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
06-28 22:21:49.003: E/AndroidRuntime(14739): at android.view.View.layout(View.java:14860)
06-28 22:21:49.003: E/AndroidRuntime(14739): at android.view.ViewGroup.layout(ViewGroup.java:4643)
06-28 22:21:49.003: E/AndroidRuntime(14739): at com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:374)
06-28 22:21:49.003: E/AndroidRuntime(14739): at android.view.View.layout(View.java:14860)
06-28 22:21:49.003: E/AndroidRuntime(14739): at android.view.ViewGroup.layout(ViewGroup.java:4643)
06-28 22:21:49.003: E/AndroidRuntime(14739): at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
06-28 22:21:49.003: E/AndroidRuntime(14739): at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
06-28 22:21:49.003: E/AndroidRuntime(14739): at android.view.View.layout(View.java:14860)
06-28 22:21:49.003: E/AndroidRuntime(14739): at android.view.ViewGroup.layout(ViewGroup.java:4643)
06-28 22:21:49.003: E/AndroidRuntime(14739): at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2013)
06-28 22:21:49.003: E/AndroidRuntime(14739): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1770)
06-28 22:21:49.003: E/AndroidRuntime(14739): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1019)
06-28 22:21:49.003: E/AndroidRuntime(14739): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5725)
06-28 22:21:49.003: E/AndroidRuntime(14739): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
06-28 22:21:49.003: E/AndroidRuntime(14739): at android.view.Choreographer.doCallbacks(Choreographer.java:574)
06-28 22:21:49.003: E/AndroidRuntime(14739): at android.view.Choreographer.doFrame(Choreographer.java:544)
06-28 22:21:49.003: E/AndroidRuntime(14739): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
06-28 22:21:49.003: E/AndroidRuntime(14739): at android.os.Handler.handleCallback(Handler.java:733)
06-28 22:21:49.003: E/AndroidRuntime(14739): at android.os.Handler.dispatchMessage(Handler.java:95)
06-28 22:21:49.003: E/AndroidRuntime(14739): at android.os.Looper.loop(Looper.java:136)
06-28 22:21:49.003: E/AndroidRuntime(14739): at android.app.ActivityThread.main(ActivityThread.java:5086)
06-28 22:21:49.003: E/AndroidRuntime(14739): at java.lang.reflect.Method.invokeNative(Native Method)
06-28 22:21:49.003: E/AndroidRuntime(14739): at java.lang.reflect.Method.invoke(Method.java:515)
06-28 22:21:49.003: E/AndroidRuntime(14739): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
06-28 22:21:49.003: E/AndroidRuntime(14739): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
06-28 22:21:49.003: E/AndroidRuntime(14739): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
试试这个。未经测试,但应该有效:
final Context context = this;
ArrayAdapter<String> adopter = new ArrayAdapter<String>(this,
R.layout.week_flash_card, R.id.txt_title, course)
{
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
super.getView(position, convertView, parent);
//convertView = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE).inflate(R.layout.week_flash_card, null);
Button button = convertView.findViewById(R.id.bPractice_deck);
button.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0)
{
switch(position)
{
case 0:
{
// your code goes here
break;
}
case 1:
{
// your code goes here
break;
}
// etc...
}
}
});
return convertView;
}
};
setListAdapter(adopter);
答案 1 :(得分:0)
// bPractice=(Button)findViewById(R.id.bPractice_deck);
会导致错误(如果当然没有注释),因为您还没有为您的活动设置布局。
- 要解决:
TextView textView = (TextView) rowView.findViewById(R.id.txt_title);
下方
bPractice=(Button) rowView.findViewById(R.id.bPractice_deck);
然后在下一行中,执行bPractice.setTag(Integer.valueOf(position))
。此标记可让您利用点击方法中的位置。由于您使用android:onClick="startSwipeViewActiviy"
在xml中声明了click方法,因此您现在需要创建方法(而不是以编程方式设置侦听器并使用onClick)
public void startSwipeViewActivity(View v){
switch((Integer) v.getTag()){
case 0:
// Do stuff for case 0
break;
case 1:
// Do stuff for case 1
break;
// Continue for all other cases
}
}
我无法对此进行测试,但至少应该让你朝着正确的方向前进。如果它不起作用,让我知道为什么,当我有机会测试它时,我可以找出我出错的地方。