altin bana altin bana - 2 months ago 12
Android Question

On focus or onclick button not refreshing playlist from Json parser URL

How to achieve this guys?
I have two buttons om my app.


  1. Button1 fetch/retrieve All Videos playlist from Json file on URL called VideoAll.js

  2. Button2 fetch/retrieve UK Videos playlist from Json file on URL called VideoUK.js



My code is working and its fetching the playlist when I click All (VideoAll.js) button, but when I click UK button its not refreshing and I get the same list from VideoAll.js I have to close the app and open to click UK button first to get the correct playlist from VideoUK.js, but again I cant get the list from All. Basically its not refreshing or updating onclick or even onfocus. its calling Json every time I click so I am sure this is not the way is done and I am doing it wrong.

MyActivity

public class VideoPlayerActivity extends Activity implements MediaPlayer.OnPreparedListener {
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mDrawerToggle;
//private Toolbar toolbar;
private ListView mDrawerList;
private CharSequence mDrawerTitle;
private RelativeLayout mDrawerRelativeLayout;

public static final String EXTRA_INDEX = "EXTRA_INDEX";
public static final int PLAYLIST_ID = 6; //Arbitrary, for the example (different from audio)
protected EMVideoView emVideoView;
protected PlaylistManager playlistManager;

protected int selectedIndex = 2;//Auto play first video
private final String VIDEO_URL1 = "http://satdoc.dyndns.info/all.js";
private final String VIDEO_URL2 = "http://satdoc.dyndns.info/uk.js";


private String TAG = "VideoPlayerActivity";
List<VideoItem> videoList = new ArrayList<>();
private FullScreenListener fullScreenListener;
//private Toolbar toolbar;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.video_player_activity);
setVolumeControlStream(3);

mDrawerTitle = getTitle();


mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.selection_activity_list);
mDrawerRelativeLayout = (RelativeLayout) findViewById(R.id.left_drawer);

emVideoView = (EMVideoView) findViewById(R.id.video_play_activity_video_view);
emVideoView.setOnPreparedListener(this);


if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
fullScreenListener = new FullScreenListener();
}

goFullscreen();
emVideoView.setVideoViewControlsCallback(new DefaultControlsCallback());

initDrawer();

Button btn = (Button) findViewById(R.id.button);
Button btn2 = (Button) findViewById(R.id.button2);



btn.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
if (hasFocus) {
fetchVideos1();
}
}

});


btn2.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
if (hasFocus) {
fetchVideos2();
}
}

});

}


/**
* Fetch for videos online.
*/
public void fetchVideos1() {
if (NetworkConnectionStatus.isOnline(this)) {//If there is internet connection, load reviews through a http request.
final ProgressDialog progressDialog = new ProgressDialog(this);

AsyncHttpClient client = new AsyncHttpClient();
client.setTimeout(20_000);
client.get(VIDEO_URL1, new AsyncHttpResponseHandler() {

@Override
public void onStart() {
// called before request is started

}

@Override

public void onSuccess(int statusCode, Header[] headers, byte[] response) {// called when response HTTP status is "200 OK"
String jsonResponse = new String(response);



try {


JSONArray jsonArray = new JSONArray(jsonResponse);
JSONObject jsonObject;
String videoTitle, videoUrl;
int videoNum;
int length = jsonArray.length();


for (int i = 0; i < length; ++i) {


jsonObject = (JSONObject) jsonArray.get(i);
videoTitle = jsonObject.getString("chname");
videoUrl = jsonObject.getString("chlink");
videoNum = jsonObject.getInt("chid");

VideoItem videoItem = new VideoItem(videoTitle, videoUrl, videoNum);
videoList.add(videoItem);
}


setTitle(mDrawerTitle);
init();
mDrawerList.setAdapter(new VideoSelectionListAdapter(VideoPlayerActivity.this, videoList));

} catch (Exception e) {
Log.e(TAG, e.getMessage(), e);
}

progressDialog.dismiss();

}

@Override
public void onFailure(int statusCode, Header[] headers, byte[] errorResponse, Throwable e) {

Toast.makeText(VideoPlayerActivity.this, "A server error occured, Try again later", Toast.LENGTH_LONG).show();
}

@Override
public void onRetry(int retryNo) {
// called when request is retried
}
});

} else {
Toast.makeText(VideoPlayerActivity.this, "No internet connection", Toast.LENGTH_LONG).show();

}

}




public void fetchVideos2() {
if (NetworkConnectionStatus.isOnline(this)) {//If there is internet connection, load reviews through a http request.
final ProgressDialog progressDialog = new ProgressDialog(this);



AsyncHttpClient client = new AsyncHttpClient();
client.setTimeout(20_000);
client.get(VIDEO_URL2, new AsyncHttpResponseHandler() {

@Override
public void onStart() {
// called before request is started

}

@Override

public void onSuccess(int statusCode, Header[] headers, byte[] response) {// called when response HTTP status is "200 OK"
String jsonResponse = new String(response);



try {


JSONArray jsonArray = new JSONArray(jsonResponse);
JSONObject jsonObject;
String videoTitle, videoUrl;
int videoNum;
int length = jsonArray.length();


for (int i = 0; i < length; ++i) {


jsonObject = (JSONObject) jsonArray.get(i);
videoTitle = jsonObject.getString("chname");
videoUrl = jsonObject.getString("chlink");
videoNum = jsonObject.getInt("chid");

VideoItem videoItem = new VideoItem(videoTitle, videoUrl, videoNum);
videoList.add(videoItem);
}


setTitle(mDrawerTitle);
init();
mDrawerList.setAdapter(new VideoSelectionListAdapter(VideoPlayerActivity.this, videoList));

} catch (Exception e) {
Log.e(TAG, e.getMessage(), e);
}

progressDialog.dismiss();

}

@Override
public void onFailure(int statusCode, Header[] headers, byte[] errorResponse, Throwable e) {

Toast.makeText(VideoPlayerActivity.this, "A server error occured, Try again later", Toast.LENGTH_LONG).show();
}

@Override
public void onRetry(int retryNo) {
// called when request is retried
}
});

} else {
Toast.makeText(VideoPlayerActivity.this, "No internet connection", Toast.LENGTH_LONG).show();

}

}


And here is my 2nd Json file on the server (VideoUK.js)

[
{
"chid": 1,
"chname": "UK1",
"chlogo": "",
"chlink": "http://samplevideoUK1.mp4",
"chenable": "",
"chnote": ""
},
{
"chid": 2,
"chname": "UK 2",
"chlogo": "",
"chlink": "http://samplevideoUK2.mp4",
"chenable": "",
"chnote": ""
},
{
"chid": 3,
"chname": "UK 3",
"chlogo": "",
"chlink": "http://samplevideoUK3.mp4",
"chenable": "",
"chnote":

}
]


and here is (VideoAll.js)

[
{
"chid": 1,
"chname": "Video 1",
"chlogo": "",
"chlink": "http://samplevideo1.mp4",
"chenable": "",
"chnote": ""
},
{
"chid": 2,
"chname": "Video 2",
"chlogo": "",
"chlink": "http://samplevideo2.mp4",
"chenable": "",
"chnote": ""
},
{
"chid": 3,
"chname": "RO 3",
"chlogo": "",
"chlink": "http://samplevideo3.mp4",
"chenable": "",
"chnote":
}
]


and finally my buttons in my layout

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:baselineAligned="false">

<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="All"
android:id="@+id/button1"
android:layout_weight="1" />


</LinearLayout>

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:baselineAligned="false" >

<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/uk"
android:id="@+id/button2"
android:choiceMode="none"
android:focusable="true"
android:fadeScrollbars="false"
android:layout_weight="1" />

</LinearLayout>


Can someone please at least show an example of how to call json and get the list every time we click the button refreshed?. Only the button that is clicked first works but after that nothing changes when I click. sorry for repeating but its driving me mad.

Screenshot of my ap

Answer

firstly clear video list on button click

     btn.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                // TODO Auto-generated method stub
                if (hasFocus) {
videoList.clear();
                    fetchVideos1();
                }
            }

        });


        btn2.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                // TODO Auto-generated method stub
                if (hasFocus) {
videoList.clear();
                    fetchVideos2();
                }
            }

        });

and notify data set chenge after setting adapter in webservices like:

mDrawerList.notifyDataSetChanged();

Please Use this After This lines in your code :

setTitle(mDrawerTitle);
                init();
                mDrawerList.setAdapter(new VideoSelectionListAdapter(VideoPlayerActivity.this, videoList));

mDrawerList.notifyDataSetChanged();

also clear list on vutton click