Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何将字符串从Listview传递到另一个类中的片段?_Android_String_Listview_Fragment - Fatal编程技术网

Android 如何将字符串从Listview传递到另一个类中的片段?

Android 如何将字符串从Listview传递到另一个类中的片段?,android,string,listview,fragment,Android,String,Listview,Fragment,我有一个android项目,其中-ListClass有一个Listview。单击Listview的一个项目后,打开CardFlipActivity,其中包含两个片段,设置为查看pageflip操作。我需要在CardFlip活动的顶部片段上播放视频,并在底部片段中显示图像 1) 如何从ListClass中的listview项目开关位置传递(原始文件夹视频文件和图像文件)的字符串,以检索并显示在CardFlipActivity的片段中 列表类代码: public class ListClass ex

我有一个android项目,其中-ListClass有一个Listview。单击Listview的一个项目后,打开CardFlipActivity,其中包含两个片段,设置为查看pageflip操作。我需要在CardFlip活动的顶部片段上播放视频,并在底部片段中显示图像

1) 如何从ListClass中的listview项目开关位置传递(原始文件夹视频文件和图像文件)的字符串,以检索并显示在CardFlipActivity的片段中

列表类代码:

public class ListClass extends ListActivity {
static final String VIDEO_PATH = null;
static final String IMAGE_PATH = null;
static final String VIDEO_NAME = null;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // storing string resources into ArrayList
    String[] coronary_pathology_clips = getResources().getStringArray(R.array.coronary_pathology_clips);
    ArrayList<String> coronary_pathology_clipsList = new ArrayList<String>(Arrays.asList(coronary_pathology_clips));

    // Binding resources ArrayList to CathListAdapter
    setListAdapter(new CathListAdapter(this,R.layout.list_item, coronary_pathology_clipsList));
    getListView().setBackgroundColor(Color.GRAY);
}   
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    change(position);
}
void change(int position){
      String selectedValue = (String) getListAdapter().getItem(position);
      Toast.makeText(this, selectedValue, Toast.LENGTH_SHORT).show();

    Intent intent = new Intent(getApplicationContext(), CardFlipActivity.class);

    switch(position){  

    case 0 :
        String path1 = "android.resource://" + getPackageName() + "/" + R.raw.video1;
        String path1a = "android.resource://" + getPackageName() + "/" + R.raw.image1;

        intent.putExtra (VIDEO_PATH, path1);  
        intent.putExtra (IMAGE_PATH, path1a);               

        break;
    case 1 :
        String path2 = "android.resource://" + getPackageName() + "/" + R.raw.video2;
        String path2a = "android.resource://" + getPackageName() + "/" + R.raw.image2;

        intent.putExtra (VIDEO_PATH, path2);  
        intent.putExtra (IMAGE_PATH, path2a);   
        break;

    } 
    Bundle dataBundle = new Bundle();
    dataBundle.putString("VIDEO_NAME", selectedValue);
    intent.putExtras(dataBundle);
    startActivity(intent);
}

}

您可以使用startactivityforresult从listclass获取结果

要将字符串传递给另一个片段,可以使用
setArgument(bundle)
fragment方法。
要接收这些字符串,您可以
getArgument()
method.

如果可能的话,请详细说明一下好吗?我应该在ListClass中编写什么代码,应该在片段中添加什么代码?请查看Android开发者页面:
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.NavUtils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


public class CardFlipActivity extends Activity
    implements FragmentManager.OnBackStackChangedListener {
private Handler mHandler = new Handler();
private boolean mShowingBack = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_card_flip);

    if (savedInstanceState == null) {
        // If there is no saved instance state, add a fragment representing the
        // front of the card to this activity. If there is saved instance state,
        // this fragment will have already been added to the activity.
        getFragmentManager()
                .beginTransaction()
                .add(R.id.container, new CardFrontFragment())
                .commit();
    } else {
        mShowingBack = (getFragmentManager().getBackStackEntryCount() > 0);
    }

    // Monitor back stack changes to ensure the action bar shows the appropriate
    // button (either "photo" or "info").
    getFragmentManager().addOnBackStackChangedListener(this);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);

    // Add either a "photo" or "finish" button to the action bar, depending on which page
    // is currently selected.
    MenuItem item = menu.add(Menu.NONE, R.id.action_flip, Menu.NONE,
            mShowingBack
                    ? R.string.action_photo
                    : R.string.action_info);
    item.setIcon(mShowingBack
            ? R.drawable.ic_action_photo
            : R.drawable.ic_action_info);
    item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            // Navigate "up" the demo structure to the launchpad activity.
            // See http://developer.android.com/design/patterns/navigation.html for more.
            NavUtils.navigateUpTo(this, new Intent(this, ListClass.class));
            return true;

        case R.id.action_flip:
            flipCard();
            return true;
    }

    return super.onOptionsItemSelected(item);
}

private void flipCard() {
    if (mShowingBack) {
        getFragmentManager().popBackStack();
        return;
    }

    // Flip to the back.

    mShowingBack = true;

    // Create and commit a new fragment transaction that adds the fragment for the back of
    // the card, uses custom animations, and is part of the fragment manager's back stack.

    getFragmentManager()
            .beginTransaction()


            .setCustomAnimations(
                    R.animator.card_flip_right_in, R.animator.card_flip_right_out,
                    R.animator.card_flip_left_in, R.animator.card_flip_left_out)

            .replace(R.id.container, new CardBackFragment())

            .addToBackStack(null)

            .commit();


    mHandler.post(new Runnable() {
        @Override
        public void run() {
            invalidateOptionsMenu();
        }
    });
}

@Override
public void onBackStackChanged() {
    mShowingBack = (getFragmentManager().getBackStackEntryCount() > 0);

    // When the back stack changes, invalidate the options menu (action bar).
    invalidateOptionsMenu();
}

/*A fragment representing the frontcard.*/

public static class CardFrontFragment extends Fragment {
    public CardFrontFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_card_front, container, false);

    }
}

/*A fragment representing the back card.*/

public static class CardBackFragment extends Fragment {
    public CardBackFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_card_back, container, false);
    }
}
}