2012-04-12 2 views
0

Я создаю пользовательский интерфейс на основе объектов JSON, отправленных в методе. При каждом запуске метода в пользовательский интерфейс добавляется новая строка. Мне нужны мои диаграммыButtonListener для доступа к 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) для связывания любого произвольного объекта с представлением.

chartsButton.setTag (вопрос);

И в вашем onClick вы можете сделать: Вопрос q = (Вопрос) v.getTag();

Смежные вопросы