聚焦或点击按钮不刷新来自 Json 解析器 URL 的播放列表

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

如何实现这个家伙? 我的应用程序有两个按钮。

  1. Button1 fetch/retrieve 来自 Json 文件的所有视频播放列表 URL 名为 VideoAll.js

  2. Button2 fetch/retrieve URL 上的 Json 文件中名为 VideoUK.js

  3. 的英国视频播放列表

我的代码工作正常,当我点击全部 (VideoAll.js) 按钮时它会获取播放列表,但是当我点击英国按钮时它不会刷新,我从 VideoAll.js 得到相同的列表我有关闭应用程序并打开以首先单击 UK 按钮以从 VideoUK.js 获取正确的播放列表,但我再次无法从 All 获取列表。基本上它不会刷新或更新 onclick 甚至 onfocus。每次我点击它都会调用 Json 所以我确定这不是完成的方式,我做错了。

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();

    }

}

这是我在服务器上的第二个 Json 文件 (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":

}
]

这里是 (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":
}
]

最后是布局中的按钮

 <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>

有人可以至少展示一个示例,说明每次我们单击刷新按钮时如何调用 json 并获取列表吗?。只有首先单击的按钮有效,但之后单击时没有任何变化。抱歉重复,但它让我发疯。

Screenshot of my ap

点击按钮首先清除视频列表

     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();
                }
            }

        });

并在 web 服务中设置适配器后通知数据集 chenge:

mDrawerList.notifyDataSetChanged();

请在您的代码中的这一行之后使用:

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

notifyDataSetChanged();

用于刷新适配器

也可以在 vutton click 上清除列表