开发者

Deep access to the view in custom array adapter

    lv = getListView();
    lv.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView adapterview, View view,
                                           int p开发者_开发技巧osition, long id) {        
                        doSomething();                 
                }
        });

That is my code of my main activity. I made a custom listview and of course, customized ArrayAdapter as well. And in each row of listview I have a progress bar, I set progress bar view in ArrayAdapter at getView() method. Like this is my ArrayAdapter

@Override
public View getView(int position, View convertView, ViewGroup parent) {
ProgressBar bar = (ProgressBar)convertView.findViewById(R.id.progressbar);
}
private void updateProgressBar(){
//blah blah
}

So my question is: When I execute doSomething() method by clicking on a row of listview at main Activity, it triggers the updateProgressBar() method in ArrayAdapter class and update the progress bar view in that row. So imagine if I click three row then three progress bar of each row will start running. How I possibly achieve this?


Well, you can get the item using the adapter and activate the progressbar of the specific item or you can add a clicklistener to each row when created and update it inside the getview()

I hope this helps


For example, suppose lv is a ListView and suppose that your ProgressBar is invisible and you are just going to make it visible when someone clicks on the item

lv.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
         doSomething(view, yourListArray.get(position), position);
    }
});
private void doSomething(View view, YourItem item, int position){
    ((ProgressBar)view.findViewById(R.id.list_progress)).setVisibility(View.VISIBLE);
    //do whatever you want here, you have the item, the view and even the position in case you need something
    //else, in fact this maybe should be a thread so whenever you finish doing whatever you want, in the
    //handler of the thread you make the ProgressBar invisible again
}

I hope this helps

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜