1
메인 액티비티의 (fab)을 클릭해야합니다. 이미 동일한 액티비티에서이 작업을 시도 했으므로 프래그먼트에서 메소드를 실행하지만, 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로 콜백하고 싶습니까? 당신의 기대는 무엇입니까? –