如何从android中的视图中打印动态删除的值

时间:2014-07-16 10:37:57

标签: android dynamic-programming layout-inflater

我需要点击TextView打印已移除的Button文字。

这是我到目前为止的代码:

public void function() {

    LayoutInflater layoutInflater = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    final View addView = layoutInflater.inflate(R.layout.row, null);

    TextView textOut = (TextView) addView.findViewById(R.id.textout);
    textOut.setText(test.get(i).toString());

    Button buttonRemove = (Button) addView.findViewById(R.id.remove);
    buttonRemove.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            ((LinearLayout) addView.getParent()).removeView(addView);
            //need to print the value of the removed textview 
        }
    });

    container.addView(addView);
}

1 个答案:

答案 0 :(得分:0)

public void function() {

LayoutInflater layoutInflater = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View addView = layoutInflater.inflate(R.layout.row, null);

TextView textOut = (TextView) addView.findViewById(R.id.textout);
textOut.setText(test.get(i).toString());
final String textToPrint = test.get(i).toString();
Button buttonRemove = (Button) addView.findViewById(R.id.remove);
buttonRemove.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        Log.d("myLog",textToPrint );
        ((LinearLayout) addView.getParent()).removeView(addView);
        //need to print the value of the removed textview 
    }
});

container.addView(addView);

}

试试这种方式