Android 通过单击按钮创建活动
我基于方法中发送的JSON对象构建了一个UI。每次运行该方法时,都会向UI添加一个新行。我需要我的chartsButtonListener能够访问JSONObjectAndroid 通过单击按钮创建活动,android,json,android-intent,Android,Json,Android Intent,我基于方法中发送的JSON对象构建了一个UI。每次运行该方法时,都会向UI添加一个新行。我需要我的chartsButtonListener能够访问JSONObject问题 public void buildQuestions(JSONObject question) throws JSONException { LayoutInflater inflater = (LayoutInflater) getActivity(). getSystemService(Context.LA
问题
public void buildQuestions(JSONObject question) throws JSONException {
LayoutInflater inflater = (LayoutInflater) getActivity().
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
questionContainer = (TableLayout) mainLayout.findViewById(R.id.questionContainer);
View questionBox = inflater.inflate(R.layout.question, null);
questionBox.setId(Integer.parseInt(question.getString("id")));
TextView title = (TextView) questionBox.findViewById(R.id.questionTextView);
title.setText(question.getString("title"));
//omitted code for verbosity
Button chartsButton = (Button) questionBox.findViewById(R.id.chartsButton);
Button submitButton = (Button) questionBox.findViewById(R.id.submitButton);
chartsButton.setOnClickListener(chartsListener);
submitButton.setOnClickListener(submitListener);
TableRow tr = (TableRow) questionBox;
TableLayout.LayoutParams trParams = new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.MATCH_PARENT);
trParams.setMargins(leftMargin, topMargin, rightMargin, bottomMargin);
tr.setLayoutParams(trParams);
LinearLayout progressRow = (LinearLayout) mainLayout.findViewById(R.id.progressRow);
progressRow.setVisibility(View.GONE);
questionContainer.addView(tr);
}
public OnClickListener chartsListener = new OnClickListener() {
public void onClick(View v) {
Intent chart = new Intent();
chart.setClass(getActivity(), Chart.class);
Log.v("", v.getParent().getClass().toString());//returns the TableRow
startActivity(chart);
}
};
我熟悉put extra,但最好是直接将疑问对象传递给听者,这样我就可以将其转发给意图。有什么建议吗?您可以使用View.setTag(Object)将任意对象与视图相关联
chartsButton.setTag(问题)
在onClick中,您可以执行以下操作:
问题q=(问题)v.getTag()