2012-04-12 42 views
0

我構建基於方法中發送的JSON對象的用戶界面。每次該方法運行時,都會向用戶界面添加一個新行。我需要我的chartsButtonListener能夠訪問JSONObject question從按鈕單擊構建活動

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

我所熟悉的投入額外,但它是最好的,如果我可以簡單直接的問題對象傳遞給聽衆,所以我可以再往前是一起意圖。有什麼建議麼?

回答

1

您可以使用View.setTag(Object)將任意對象關聯到View。

chartsButton.setTag(question);

而在你的onClick中,你可以這樣做: 問題q =(問題)v.getTag();