开发者

Getting source information from a programatically created OnClickListener

I'm writing an android application that programatically populates a LinearLayout with several Views that contain TextViews. Additionally, I'm adding OnClickListeners to each of the above views such that I can retreive the Strings those TextViews contained within when clicked. The ClickListeners are working, as I'm hitting that method when running through the debugger, but Im not sure how to access the data for the specific view it is being called from. Here's a code snippet to clarfy:

LinearLayout list = (LinearLayout)findViewById(R.id.whatever);

for(int i = 0; i < sourceArrayList.size(); i++)
{
    View customTextView = View.inflate(this, R.layout.custom_text_view.xml, null);
    TextView tv1 = (TextView)customTextView.findViewById(R.id.tv1);
    tv1.setText("This is the value I would like to retrieve");
    TextView tv2 = (TextView)customTextView.findViewById(R.id.tv2);    
    tv2.setText("I would also like to get this value");

    customTextView.setOnClickListener(new OnClickListener()
    {
        public void onClick(View arg0) 
        {
            Toast.makeText(getBaseContext(), "You clicked: x", Toast.LENGTH_SHORT).show();
        }

 开发者_StackOverflow社区   });

    list.addView(customTextView);
}

So, any guidance on how to grab the values of those textViews within that onClick() method would be much appreciated.


In an anonymousness inner class you have access to final variables that are available within the outer scope. Thus, to be able to access tv1 or tv2 within your OnClickListener you just need to declare them final.

final TextView tv1 = (TextView)customTextView.findViewById(R.id.tv1);
tv1.setText("This is the value I would like to retrieve");
final TextView tv2 = (TextView)customTextView.findViewById(R.id.tv2);    
tv2.setText("I would also like to get this value");

customTextView.setOnClickListener(new OnClickListener()
{
    public void onClick(View arg0) 
    {
        Toast.makeText(getBaseContext(), "You clicked: " + tv1.getText(), Toast.LENGTH_SHORT).show();
    }

});

Not sure what exactly you are trying to accomplish, but you might want to look into using a ListView instead.


If I understand both what you're saying and the code you included, it sounds like any click inside the parent view (regardless of where) needs to access the state of the child views. Is that right?

If so, there are at least two basic ways (with multiple variations) to do this.

First, if there's a reasonable number of views to work with, you can set a variable in the outer class to hold each one, and you can declare the variable as final. Then you can use those variables directly, as Mayra outlined.

Alternately, the parameter to the onClick() method (in your code, named arg0) always contains the view that generated the event. With your code, that's the parent (customTextView). You can use that view to get references to any child views, whether you do that through findViewById(), something like a ViewHolder, or any other way. For example, you could set this as the onClick():

public void onClick(View arg0) 
    {
        TextView textView1 = (TextView) arg0.findViewById(R.id.tv1);
        String text1 = textView1.getText().toString;
        Toast.makeText(getBaseContext(), text1, Toast.LENGTH_SHORT).show();
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜