我的需求是当我点击firstfragment中的按钮加载来自数据库的含义,然后屏幕移动到下一个屏幕(滑动到secondfragment)自动从给定网址加载图像并显示在图像中,然后从给定网址自动滑动到thirdfragment音频加载,以及视频的相同任务。
给定codefragment(第二个片段)加载在意义片段(第一个片段)中,第三个加载到第二个,第四个加到第三个。
***Meaning fragment( First Fragment)***
String url2 = "";
String webdata = "";
int wifi = 0;
int first = 1;
View view;
List<String> wordslist;
ImageButton find;
EditText word;
WebView data;
TextView urdu_meaning;
ListView lv;
SqlLiteDbHelper dbHelper;
GetMeaning urdu ;
NetworkInfo mWifi;
ConnectivityManager connManager;
WifiManager wifiManager;
public static String urlword = "";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Get the view from fragmenttab1.xml
view = inflater.inflate(R.layout.meaningview, container, false);
word = (EditText) view.findViewById(R.id.word);
find = (ImageButton) view.findViewById(R.id.find);
data = (WebView) view.findViewById(R.id.web);
urdu_meaning = (TextView) view.findViewById(R.id.Urdu_Text);
lv = (ListView) view.findViewById(R.id.listofword);
Typeface tf= Typeface.createFromAsset(getActivity().getAssets(), "jameel.ttf");
urdu_meaning.setTypeface(tf);
if(first == 1)
{
try {
dbHelper = new SqlLiteDbHelper(getActivity());
dbHelper.CopyDataBaseFromAsset();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
first++;
}
word.addTextChangedListener(new TextWatcher() {
@SuppressWarnings("static-access")
@SuppressLint("InlinedApi")
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
if (!word.getText().toString().equalsIgnoreCase(""))
{
wordslist = dbHelper.Loadwords(word.getText().toString());
//wordslist.toString()
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_selectable_list_item, wordslist);
lv.setVisibility(view.VISIBLE);
lv.setAdapter(adapter);
data.setVisibility(view.INVISIBLE);
urdu_meaning.setVisibility(view.INVISIBLE);
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
lv.setOnItemClickListener(new OnItemClickListener() {
@SuppressWarnings("static-access")
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
word.setText(wordslist.get(position));
lv.setVisibility(view.INVISIBLE);
}
});
find.setOnClickListener(new OnClickListener() {
@SuppressWarnings("static-access")
@SuppressLint({ "NewApi", "DefaultLocale" })
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
lv.setVisibility(view.INVISIBLE);
data.setVisibility(view.VISIBLE);
urdu_meaning.setVisibility(view.VISIBLE);
url2 = word.getText().toString().toLowerCase();
AudioFragment.audioclick = 0;
VideoFragment.videoclick = 0;
ImageFragment.imageclick = 0;
urlword = url2;
InputMethodManager inputManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
if(url2.isEmpty())
{
Toast.makeText(getActivity(), "Word is missing! ", Toast.LENGTH_LONG).show();
}
else
{
try{
urdu = dbHelper.getDetail(word.getText().toString());
urdu_meaning.setText(urdu.Meaning.toString());
}catch(Exception e){
e.printStackTrace();
urdu_meaning.setText("Urdu word not found");
}
if (mWifi.isConnected())
{
data.loadUrl(url1 + url2);
}
else
{
Toast.makeText(getActivity(), "Network Problem", Toast.LENGTH_LONG).show();
//wifiManager.setWifiEnabled(true);
}
}
}
});
return view;
}
***Image Fragment Second Fragment***
if(imageclick == 0)
{
//imageURL1 = imageURL + MeaningFragment.urlword + ".jpg";
word.setVisibility(View.INVISIBLE);
// Execute the task
//GetXMLTask task = new GetXMLTask();
//task.execute(imageURL1);
// Create an object for subclass of AsyncTask
}
find.setOnClickListener(new OnClickListener() {
@SuppressWarnings("static-access")
@SuppressLint({ "NewApi", "DefaultLocale" }) @Override
public void onClick(View v) {
if(imageclick == 0)
{
imageURL2 = MeaningFragment.urlword.toString().toLowerCase();
word.setVisibility(View.VISIBLE);
word.setText(MeaningFragment.urlword.toString().toLowerCase());
prevword.setVisibility(View.INVISIBLE);
}
else
{
word.setVisibility(View.VISIBLE);
prevword.setVisibility(View.INVISIBLE);
imageURL2 = word.getText().toString().toLowerCase();
}
mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
InputMethodManager inputManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
if(imageURL2.isEmpty())
{
Toast.makeText(getActivity(), "Word is missing! ", Toast.LENGTH_LONG).show();
imageView.setVisibility(View.INVISIBLE);
}
else
{
imageURL2 = imageURL2 + ".jpg";
imageURL1 = (imageURL + imageURL2);
//searchurdu();
if(mWifi.isConnected())
{
try{
// Execute the task
GetXMLTask task = new GetXMLTask();
task.execute(imageURL1);
// Create an object for subclass of AsyncTask
}catch(Exception e){
e.printStackTrace();
}
//task.execute("http://www.learn2crack.com/wp-content/uploads/2014/04/node-cover-720x340.png");
// TODO Auto-generated method stub
}
else
{
Toast.makeText(getActivity(), "Network Problem", Toast.LENGTH_LONG).show();
//wifiManager.setWifiEnabled(true);
}
}
}
});
return view;
}
private class GetXMLTask extends AsyncTask<String, Void, Bitmap> {
@Override
protected Bitmap doInBackground(String... urls) {
try{
Bitmap map = null;
for (String url : urls) {
map = downloadImage(url);
}return map;
}catch(Exception e){
e.printStackTrace();
return null;
}
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
//pDialog.show();
}
// Sets the Bitmap returned by doInBackground
@Override
protected void onPostExecute(Bitmap result) {
imageView.setImageBitmap(result);
imageView.setVisibility(View.VISIBLE);
pDialog.dismiss();
if(result == null){
Toast.makeText(getActivity(), "Image Not Found", Toast.LENGTH_LONG).show();
}
}
// Creates Bitmap from InputStream and returns it
private Bitmap downloadImage(String url) {
try {Bitmap bitmap = null;
InputStream stream = null;
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inSampleSize = 1;
stream = getHttpConnection(url);
bitmap = BitmapFactory.
decodeStream(stream, null, bmOptions);
stream.close();
return bitmap;
} catch (IOException e1) {
e1.printStackTrace();
return null;
}
}
// Makes HttpURLConnection and returns InputStream
private InputStream getHttpConnection(String urlString)
throws IOException {
try {InputStream stream = null;
URL url = new URL(urlString);
URLConnection connection = url.openConnection();
HttpURLConnection httpConnection = (HttpURLConnection) connection;
httpConnection.setRequestMethod("GET");
httpConnection.connect();
if (httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
stream = httpConnection.getInputStream();
}return stream;
} catch (Exception ex) {
ex.printStackTrace();
Toast.makeText(getActivity(), " Word not Found", Toast.LENGTH_LONG).show();
return null;
}
and same method for audio and video.....