0

对 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);

}

}

正如我现在拥有的那样,日志甚至没有出现。

4

1 回答 1

3

您可能希望在以下位置返回您实际创建的视图(并用于获取ButtononCreateView

//...
return view;

而不是返回一个新膨胀的布局(并丢失前一个Button):

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

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

于 2013-01-16T20:36:22.373 回答