我遵循了一个教程,该教程使我可以在回收的android系统中从mysql数据库加载数据,并且一切都做得很好。在加载的数据中,有指向视频的链接,我希望当用户单击recyclerview元素时,他可以播放相应的视频。我该怎么办?
这是从数据库加载视频的代码
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
// CONNECTION_TIMEOUT and READ_TIMEOUT are in milliseconds
public static final int CONNECTION_TIMEOUT = 10000;
public static final int READ_TIMEOUT = 15000;
private RecyclerView recyclerView;
private Adapter mAdapter;
private DatabaseReference mDatabase;
List<Data> data = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Make call to AsyncTask
new AsyncLogin().execute();
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
RecycleClick.addTo(recyclerView).setOnItemClickListener(new RecycleClick.OnItemClickListener() {
@Override
public void onItemClicked(RecyclerView recyclerView, int position, View v) {
String url = data.get(position).sizeName;
String name = data.get(position).fishName;
String titre = data.get(position).catName;
for(int i = 0;i<data.size();i++) {
Intent intent = new Intent(MainActivity.this, PlayVideo.class);
intent.putExtra("url", url);
intent.putExtra("name", name);
intent.putExtra("title", titre);
startActivity(intent);
}
overridePendingTransition(R.anim.activity_back_in, R.anim.activity_back_out);
finish();
Toast.makeText(getApplicationContext(), "On Click\nPosition : "+(position+1)+"\nTitle : "+"", Toast.LENGTH_SHORT).show();
}
});
RecycleClick.addTo(recyclerView).setOnItemLongClickListener(new RecycleClick.OnItemLongClickListener() {
@Override
public boolean onItemLongClicked(RecyclerView recyclerView, int position, View v) {
return true;
}
});
}
private class AsyncLogin extends AsyncTask<String, String, String> {
ProgressDialog pdLoading = new ProgressDialog(MainActivity.this);
HttpURLConnection conn;
URL url = null;
@Override
protected void onPreExecute() {
super.onPreExecute();
//this method will be running on UI thread
pdLoading.setMessage("\tLoading...");
pdLoading.setCancelable(false);
pdLoading.show();
}
@Override
protected String doInBackground(String... params) {
try {
// Enter URL address where your json file resides
// Even you can make call to php file which returns json data
url = new URL("http://192.168.43.196/vibe2/essai4.php");
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return e.toString();
}
try {
// Setup HttpURLConnection class to send and receive data from php and mysql
conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(READ_TIMEOUT);
conn.setConnectTimeout(CONNECTION_TIMEOUT);
conn.setRequestMethod("GET");
// setDoOutput to true as we recieve data from json file
conn.setDoOutput(true);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
return e1.toString();
}
try {
int response_code = conn.getResponseCode();
// Check if successful connection made
if (response_code == HttpURLConnection.HTTP_OK) {
// Read data sent from server
InputStream input = conn.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
StringBuilder result = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
result.append(line);
}
// Pass data to onPostExecute method
return (result.toString());
} else {
return ("unsuccessful");
}
} catch (IOException e) {
e.printStackTrace();
return e.toString();
} finally {
conn.disconnect();
}
}
@Override
protected void onPostExecute(String result) {
//this method will be running on UI thread
pdLoading.dismiss();
List<Data> data=new ArrayList<>();
pdLoading.dismiss();
try {
JSONArray jArray = new JSONArray(result);
// Extract data from json and store into ArrayList as class objects
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
Data DataItem = new Data();
DataItem.fishImage= json_data.getString("ImagePath");
DataItem.fishName= json_data.getString("AndroidNames");
DataItem.catName= json_data.getString("titre");
DataItem.sizeName= json_data.getString("url");
DataItem.price= json_data.getInt("counter");
data.add(DataItem);
}
// Setup and Handover data to recyclerview
mAdapter = new Adapter(MainActivity.this, data);
recyclerView.setAdapter(mAdapter);
recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));
} catch (JSONException e) {
Toast.makeText(MainActivity.this, e.toString(), Toast.LENGTH_LONG).show();
}
}
}
} 这是我的点击
RecycleClick.addTo(recyclerView).setOnItemClickListener(new RecycleClick.OnItemClickListener() {
@Override
public void onItemClicked(RecyclerView recyclerView, int position, View v) {
}
});
答案 0 :(得分:0)
@Override
protected void onPostExecute(String result) {
//this method will be running on UI thread
pdLoading.dismiss();
final List<Data> data=new ArrayList<>();
pdLoading.dismiss();
try {
JSONArray jArray = new JSONArray(result);
// Extract data from json and store into ArrayList as class objects
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
Data DataItem = new Data();
DataItem.fishImage= json_data.getString("ImagePath");
DataItem.fishName= json_data.getString("AndroidNames");
DataItem.catName= json_data.getString("titre");
DataItem.sizeName= json_data.getString("url");
DataItem.price= json_data.getInt("counter");
data.add(DataItem);
}
// Setup and Handover data to recyclerview
mAdapter = new Adapter(MainActivity.this, data);
recyclerView.setAdapter(mAdapter);
recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));
RecycleClick.addTo(recyclerView).setOnItemClickListener(new RecycleClick.OnItemClickListener() {
@Override
public void onItemClicked(RecyclerView recyclerView, int position, View v) {
Intent intent = new Intent(MainActivity.this, PlayVideo.class);
intent.putExtra("url", data.get(position).sizeName);
startActivity(intent);
overridePendingTransition(R.anim.activity_back_in, R.anim.activity_back_out);
finish();
}
});
} catch (JSONException e) {
Toast.makeText(MainActivity.this, e.toString(), Toast.LENGTH_LONG).show();
}