0

If there is no event like list.onLoadListener(list is done with its data filling) how to access list's first row?

As list reuses its row for performance this listener is not there that is understandable. But I need to access list's first item when at least there is one item(first item , position 0).

After setting adapter to list

list.getChildAt(0)   

returns me null. So Do i need to put delay for accessing first item?

We can access list items(views in that item) on using list item click listener. I want to use item when I can be sure that list's first item has filled.

I am playing videos in list item using TextureView. So once list is filled with its item I want to play first item's video automatically.(without any click or user interaction).

Here is my code :-

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.home);

    list = (ListView) v.findViewById(R.id.list);
            videoListDatas = new ArrayList<VideoListData>();
            adapter = new MyVideoListAdapterNew(getActivity(), videoListDatas);

            list.setAdapter(adapter);

            getVideoList(); //Method which get data from server

}

Here is getVideoList() method implementation

private void getVideoList() {
    final MyProgressDialog progressDialog = new MyProgressDialog(context);
    new Thread(new Runnable() {

        @Override
        public void run() {
            try {

                // Implementation goes here fill array with data

            } catch (Exception e) {
                e.printStackTrace();
            }
            runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    adapter.notifyDataSetChanged();
                    progressDialog.dismiss();

                }

            });
        }

    }).start();
}

And here is my adapter

   public class MyVideoListAdapterNew extends BaseAdapter {

Context context;
private LayoutInflater inflater;

ArrayList<VideoListData> videoListDatas;


public MyVideoListAdapterNew(FragmentActivity fragmentActivity,
        ArrayList<VideoListData> videoListDatas) {
    context = fragmentActivity;
    this.videoListDatas = videoListDatas;
    inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return videoListDatas.size();
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return videoListDatas.get(position);
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;

    if (convertView == null) {
        convertView = inflater.inflate(R.layout.myvideo_row, null);

        holder = new ViewHolder();


        holder.flVideo = (FrameLayout) convertView
                .findViewById(R.id.flVideo);

        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();



    }

    try {

        //Filling views by getting values from array

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

if(position == 0 ){
    //Code to play first video automatically
            }else{
                     }

    return convertView;
}

private static class ViewHolder {

    FrameLayout flVideo;

}

}

I know code will not help much but I am just posting it on some people's suggestion.

Thanks.

Roshan Jha
  • 2,091
  • 1
  • 21
  • 30

4 Answers4

0

You can use Custom Adapter for List View and and then you can intercept the view on any specific position in public View getView(int position, View convertView, ViewGroup parent) method. Here you can check that

if(position==0){
 //do your stuff
}

Look this answer for Custom Adapter

Community
  • 1
  • 1
Mukesh Kumar Singh
  • 4,512
  • 2
  • 22
  • 30
  • I have given this try, it works also but problem with this is MyAdapter is independent class which I use to fill my adapter. So I just pass require arguments like context and my data. As I am showing one video at one time only I have some global variables declared like my textureView and its listener. Also I need to manage few flags (say list is again scrolled and position is again 0) well all this things can be done but will take some(state manage will increase) more management. So will be good if there is any method which can fulfill this requirement. – Roshan Jha Oct 30 '13 at 10:42
  • @R.j. I dont think that there is any specific method where you can manage your variables at all. As you are using custom adapter then you have to manage these by own. – Mukesh Kumar Singh Oct 30 '13 at 11:06
0

When are you populating the list? When do you want access to the first element? If you are using fragments you have onViewCreated, onCreateView methods and you can populate the listview in the onCreateView method and access to it in the onViewCreated methods. With activitys it is different, you could populate the list in onCreate() and later onStart() or onResume() access to the first element.

Jon Zangitu
  • 957
  • 1
  • 15
  • 30
0

Don't know if this is the best approach but you can give it a try.

Create a listener that looks something like this

public interface ViewLoadedInterface {
    public void onFirstViewLoaded(/**add params if you like*/);
}

Pass that Interface to your Adapter. You only invoke the onFirstViewLoaded() method when your first View is returned and then never invoke it again for that instance, otherwise you will have some problems when recycling starts.

When it is invoked you then do the list.getChildAt(0) in the implementation of the interface.

So in the end the implementation will look something like this

public class YourActivityClass extends Activity implements ViewLoadedInterface {
    ...

    @Override
    public void onFirstViewLoaded() {
        ...

        something = list.getChildAt(0);

        ...
    }
}
the-ginger-geek
  • 7,041
  • 4
  • 27
  • 45
  • see Mukesh Kumar answer. Isn,t same? yes I got some problems because as list resuse its view. That is why I want to avoid my implementation(Actually I am looking for method/implementation which can easily fuifill my requirement comparing to this one.) – Roshan Jha Oct 30 '13 at 10:50
  • Yes it is similar because I also used a custom adapter. But the listener part is different. A custom adapter would be the route to take. – the-ginger-geek Oct 30 '13 at 10:53
0

I found the solution for this situation from this thread :- https://stackoverflow.com/a/6944587/647014

My requirement was to accessing list's first item as soon as list completes its data loading. This way list waits till its data is refreshed/loaded.

Thanks.

Community
  • 1
  • 1
Roshan Jha
  • 2,091
  • 1
  • 21
  • 30