how to set different actions to dynamiclly created

2019-09-06 09:57发布

问题:

i created a layout containing multiple text views. i saved the text view's ids in an ArrayList which is a class variable called _resultId.

now i want to create buttons which suppose to scroll to the correct text view (the first button to the first text view etc)

the question is: how to pass the correct id to each of the buttons on press method?

i tried using a global variable _counter but when i run the program all the buttons scroll to the last text view

the code of the method:

private void addNavigationView(ViewGroup navigationLayout, ArrayList<Perek> mishnayot) 
{
    for (int i=0;i<mishnayot.size();i++)
    {
        _counter=i;
        String currentOt=mishnayot.get(i).getOt();
        Button button = new Button(getBaseContext());
        button.setText(currentOt);
        if (_resultId==null)
            throw new IllegalAccessError("missing result id link cannot be created");
        button.setOnClickListener(new OnClickListener() 
        {

            @Override
            public void onClick(View arg0) //make it scroll to correct textview
            {
                 new Handler().post(new Runnable() 
                 {
                     @Override
                     public void run() 
                     {
                         View currentView=findViewById(_resultId.get(_counter));
                         ScrollView scrollView=(ScrollView) findViewById(R.id.resultScroll);
                         scrollView.scrollTo(0, currentView.getTop());
                     }
                 });
            }
        });
         navigationLayout.addView(button);//add the button to panel
    }
    navigationLayout.setVisibility(View.VISIBLE);
}

回答1:

after learning more on Runnable interface i Found an answer so for those who will struggle with something similar: you need to create a new class altogether that implements both runnable and the OnClickListener.

this class will contain extra data and a constructor to match your needs. when you set the onClick method of the button you create a new object of that class. for instance in my case the new method looked like:

public class ButtonHandler implements OnClickListener,Runnable 
{
private String _data;//data to be passed for the button
private ArrayList<Integer> _resultId;//the id's of the text views
private Activity _activity;

public ButtonHandler(String data,ArrayList<Integer> resultId,Activity activity)
{
    _data=data;
    _resultId=resultId;
    _activity=activity;
}

@Override
public void run() 
{
     View currentView=_activity.findViewById(_resultId.get(Integer.valueOf(_data)));
     ScrollView scrollView=(ScrollView) _activity.findViewById(R.id.resultScroll);
     scrollView.scrollTo(0, currentView.getTop());

}

@Override
public void onClick(View v) 
{
    new Handler().post(this);

}

}

now to call it from the main activity i use:

    private void addNavigationView(ViewGroup navigationLayout, ArrayList<Perek> mishnayot) 
{
    for (int i=0;i<mishnayot.size();i++)
    {
        _counter=i;
        String currentOt=mishnayot.get(i).getOt();
        Button button = new Button(getBaseContext());
        button.setText(currentOt);
        if (_resultId==null)
            throw new IllegalAccessError("missing result id link cannot be created");
        button.setOnClickListener(new ButtonHandler(i+"",_resultId,this));

        navigationLayout.addView(button);//add the button to panel
    }
    navigationLayout.setVisibility(View.VISIBLE);
}