ListFragment中的onClickListener没有响应

时间:2013-01-16 20:32:04

标签: android android-listview android-listfragment

对onClickListener没有响应的原因感到困惑。是因为按钮设置在一个布局中,但绑定到另一个布局?任何帮助,将不胜感激。

public class ActivityList extends ListFragment

{
private ActivityDbAdapter mDbHelper;
private Long mRowId=Long.valueOf(1);
private Activity mContext; //changed to private
AlertDialog activity_relationship;



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) 
{
super.onCreate(savedInstanceState);
mContext=this.getActivity();
mDbHelper=new ActivityDbAdapter(mContext);
mDbHelper.open();
Cursor activity = mDbHelper.fetchAll(mRowId);
View view = (View) inflater.inflate(R.layout.activity_activity_row, container,false);
Button relationship = (Button) view.findViewById(R.id.show_relationship_activities);




String[] from = new String[]{ActivityDbAdapter.COLUMN_NAME_VALUE1_RELATIONSHIP , //from and too.
        ActivityDbAdapter.COLUMN_NAME_VALUE1_EDUCATION,
        ActivityDbAdapter.COLUMN_NAME_VALUE1_RECREATION,
        ActivityDbAdapter.COLUMN_NAME_VALUE1_MIND, ActivityDbAdapter.COLUMN_NAME_VALUE1_DAILY};


int[] to = new int[]{R.id.relationship_value,R.id.education_value, R.id.recreation_value,
        R.id.mind_value, R.id.daily_value};

SimpleCursorAdapter contacts = 
    new SimpleCursorAdapter(mContext, R.layout.activity_activity_row, activity, from, to); //my cursor adapter 


relationship.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg) {

        Log.e("Why","Are you not working?");

        Cursor activity = mDbHelper.fetchAll(mRowId);

     String mactivity_relationship=activity.getString(                                              //define all of the Strings. Gahh!!
            activity.getColumnIndexOrThrow(ActivityDbAdapter.COLUMN_NAME_ACTIVITY1_RELATIONSHIP));

        AlertDialog activity_relationship=new AlertDialog.Builder(mContext).create();
        activity_relationship.setTitle("Relationship Activities");
        String activities=(mactivity_relationship+"\n"+mactivity_relationship2+"\n"+mactivity_relationship3+
                "\n"+mactivity_relationship4+"\n"+mactivity_relationship5);
        activity_relationship.setMessage(activities);
        activity_relationship.show();



    }

});

setListAdapter(contacts);




return inflater.inflate(R.layout.activity_activity_list, container, false);

}

}

正如我现在所说,日志甚至没有显示出来。

1 个答案:

答案 0 :(得分:3)

您可能希望在Button中返回您实际创建的视图(并用于获取onCreateView):

//...
return view;

而不是返回新增加的布局(并丢失之前的Button):

return inflater.inflate(R.layout.activity_activity_list, container, false);

或者将view添加到您膨胀并返回的R.layout.activity_activity_list文件中的视图。