我确信这在某个地方得到了解答,但找不到它。
我有一个用户目标的ListView ...我在顶部有一个按钮,用户可以在其中添加目标:
<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="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
style="@style/secondaryBackground"
tools:context="android.bignerdranch.com.mobilemidwife.GoalsList">
<ImageButton
android:id="@+id/addgoal"
style="@style/FloatingActionButtonOrange"
android:src="@drawable/ic_action_new"
android:layout_alignParentRight="true"
android:contentDescription="add goal"
android:onClick="btnRemoveClick" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:layout_below="@+id/addgoal"
android:orientation="vertical"
android:id="@+id/goalsection">
<TextView
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="Goal Name"
android:id="@+id/goalname"
android:padding="10dp"
style="@style/orangetextlarger"
android:layout_gravity="left" />
</LinearLayout>
</RelativeLayout>
AddGoal按钮实际上不是ListView的一部分...它不是被检索数据的一部分..我试图让按钮单击在适配器中启动一个新活动,但我还没有能够至;我在GetItemView函数中添加了一些代码,但似乎无法解析findViewById
@Override
public View getItemView(ParseObject object, View view, ViewGroup parent) {
if (view == null) {
view = View.inflate(getContext(), R.layout.activity_goals_list, null);
}
// use midwifefirm as item view/list
super.getItemView(object, view, parent);
// find in layout the practice name
TextView titleTextView = (TextView) view.findViewById(R.id.goalname);
//in the midwifefirm data model, call getPracticename
titleTextView.setText(object.getString("goalname"));
mAddGoal = (ImageButton) findViewById(R.id.addgoal);
mAddGoal.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(GoalListAdapter.this, AddGoal.class);
startActivity(intent);
}
});
return view;
}
我也在布局中的ImageButton中思考,我可以在那里设置onClick函数吗?
抱歉,我知道这可能在某个地方得到了回答,但无法弄明白。答案 0 :(得分:1)
使用findViewById查找视图时,您应该使用膨胀的视图。你应该:
mAddGoal = (ImageButton) view.findViewById(R.id.addgoal);
而不是:
mAddGoal = (ImageButton) findViewById(R.id.addgoal);
如果您要沿着设置onClickListener的路线走下去,那么您应该在XML中删除ImageView上的onClick属性。
从元素@+id/addgoal
中删除以下内容:
android:onClick="btnRemoveClick"
要从您的课程开始活动,您需要传递上下文。示例如下所示:
public class ParseAdapter extends ParseQueryAdapter<ParseObject> {
private Context mContext;
public ParseAdapter(Context context) {
this.mContext = context;
}
...
@Override
public View getItemView(ParseObject object, View view, ViewGroup parent) {
...
mAddGoal = (ImageButton) findViewById(R.id.addgoal);
mAddGoal.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(GoalListAdapter.this, AddGoal.class);
mContext.startActivity(intent);
}
});
}
}
答案 1 :(得分:0)
我不太确定你要做什么,但删除了代码
android:onClick="btnRemoveClick"
从你的xml 。这可能有用。
答案 2 :(得分:0)
这最终为我工作;经过一些研究,看看上面的答案,我明白我需要使用getContext():
@Override
public View getItemView(ParseObject object, View view, final ViewGroup parent) {
if (view == null) {
view = View.inflate(getContext(), R.layout.activity_goals_list, null);
}
//use midwifefirm as item view/list
super.getItemView(object, view, parent);
// find in layout the practice name
TextView titleTextView = (TextView) view.findViewById(R.id.goalname);
//in the midwifefirm data model, call getPracticename
titleTextView.setText(object.getString("goalname"));
mAddGoal = (ImageButton) view.findViewById(R.id.addgoal);
mAddGoal.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(parent.getContext(), AddGoal.class);
v.getContext().startActivity(intent);
}
});
return view;
}