1
MainActivityの(fab)をクリックする必要があります。同じアクティビティでこれを試してみたので、私のMainActivityのメソッドを実行しますが、私のコマンドを実行しようとするとMainActivityので、フラグメント内で特定のプロセスが実行されるため、結果が得られません。私は...私はそれを行う方法がわからない、意味私はLlenarDatosReproduccion実行したいこのフラグメント上のフラグメントを実行するアクティビティコントロール
public class MainActivity extends AppCompatActivity {
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Fragment
new Reproducir.LlenarDatosReproduccion();
Toast.makeText(getApplicationContext(),"Entró al play",Toast.LENGTH_SHORT).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public static class PlaceholderFragment extends Fragment {
private static final String ARG_SECTION_NUMBER = "section_number";
public PlaceholderFragment() {
}
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
TextView textView = (TextView) rootView.findViewById(R.id.section_label);
textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
return rootView;
}
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
if(position==0){
return new Reproducir();
}else {
return PlaceholderFragment.newInstance(position + 1);
}
}
@Override
public int getCount() {
// Show 3 total pages.
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "REPRODUCIR";
case 1:
return "PLAYLIST";
case 2:
return "CANCIONES";
}
return null;
}
}
}
:
public class Reproducir extends Fragment {
private static TextView tvTitle,tvArtist;
FloatingActionButton fabPlay;
private String url="http://streaming.hotmixradio.fr/hotmixradio-hits-128.mp3";
private ImageView ivAlbum;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View rootView=inflater.inflate(R.layout.tab_reproduccion,container,false);
//new LlenarDatosReptoduccion();
tvTitle=(TextView)rootView.findViewById(R.id.tv_title);
//tvTitle.setText(url);
tvArtist=(TextView)rootView.findViewById(R.id.tv_title);
return rootView;
}
public static class LlenarDatosReproduccion extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
try {
return downloadUrl(urls[0]);
} catch (IOException e) {
return "Ups! Error.Parece que la url es inválida";
}
}
@Override
protected void onPostExecute(String result) {
Log.i("POSTExecute","Ingesó antes del try");
try {
Log.i("POSTExecute","Ingesó al try");
JSONObject responseJSON = new JSONObject(result.toString());
JSONObject cancionJSON = responseJSON.getJSONObject("cancion");
tvTitle.setText(cancionJSON.get("title").toString());
Log.d("ARTISTA",cancionJSON.get("artist").toString());
//tvArtist.setText(cancionJSON.getString(2));
} catch (JSONException e) {
Log.i("POSTExecute","Ingesó al catch!");
e.printStackTrace();
}
super.onPostExecute(result);
}
}
private static String downloadUrl(String myUrl) throws IOException {
Log.i("URL"," LA URL URL URL: "+myUrl);
myUrl.replace(" ","%20");
InputStream is=null;
int lengh=500;
try{
return "{\"cancion\":{\"title\":\"Versace On The Floor (Vs David Guetta)\",\"artist\":\"BRUNO MARS\",\"duration\":\"18783\"}}";
}finally {
if(is!=null){
is.close();
}
}
}
}あらかじめ
、おかげで!
onPostExecute()の後でフラグメントからActivityにコールバックしますか?あなたの期待は何ですか? –