开发者

about ListView code

I am new to android and right now I am learning about ListView.

I was reading tutorials from bogotobogo.com when I saw this code:

ListVie开发者_开发技巧w lv = getListView();
            lv.setTextFilterEnabled(true);
            *** lv.setOnItemClickListener(new OnItemClickListener() {  
                public void onItemClick(AdapterView parent, View v,
                        int position, long id) {
                      // When clicked, show a toast with the TextView text
                    Toast.makeText(getApplicationContext(), ((TextView) v).getText(),
                    Toast.LENGTH_SHORT).show();
                }
            }); ***

i was not able to understand code from lv.setOnItemClickListener(new OnItemClickListener(); is it an argument?

Can some one help me to understand it?


What that code is doing is implementing a new OnItemClickListener inline. The OnItemClickListener interface is basically a contract that says that an object will provide an implementation of the function onItemClick(....). Later on, when an item in your list gets clicked, the onItemClick function will be called and the AdapterView (thing that's instantiating and managing the list rows, the view - (the render code for a particular row), the position (the position in the list) and an id property which I never use so you can look up what that's for are being passed in.

Inline code like this always looks strange to me. There are a couple other ways to write this that I think make more intuitive sense. Just keep in mind that what you're doing is writing some code to get executed when a row in your list gets clicked.

1 - You can have your Activity implement OnItemClickListener

public class SomeActivity extends Activity implements OnItemClickListener{
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.yourview);

        ListView lv = (ListView)findViewById(R.id.listView);
        lv.setOnItemClickListener(this);
    } 

    @Override
    public void onItemClick(AdapterView<?> arg0, View view, int position, long id) {
        //Your toast code goes in here
    }
}

In this code you're having your main class implement the OnItemClickListener interface so setOnItemClickListener sees the main class (this) as an instance of OnItemClickListener. When a row in your list gets clicked the onItemClick function will get called.

You can also 2 - have your click listener come from an internal class.

public class SomeActivity extends Activity implements OnItemClickListener{
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.yourview);

        ListView lv = (ListView)findViewById(R.id.listView);
        lv.setOnItemClickListener(new YourInternalClass());
    } 


    class YourInternalClass implements View.OnItemClickListener{
        @Override
        public void onItemClick(AdapterView<?> arg0, View view, int position, long id) {
            //Your toast code goes in here
        }
    }
}

And really all three methods are doing the same thing: Providing the setOnItemClickListener with an instance of a View.OnItemClickListener class that will have it's onItemClick function called when a row in the list gets clicked.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜