2014-12-03 145 views
0

我有一个显示项目从sqlite数据库中的一个片段的列表视图。当我点击一个项目时,它会打开一个菜单以允许打开,添加到另一个列表视图或删除。我希望能够将其添加到另一个片段中的另一个列表视图上。我尝试使用捆绑将它添加到另一个listview,但我没有运气。我有一个创建另一个数据库表的计划,用于存储添加的数据并将其显示在新的列表视图中。这会工作吗?或者你们有没有其他的建议?将列表视图传递给另一个列表视图

回答

1

您可以将数据从一个片段发送到另一个片段。做这件事的最好方法是通过父母的活动。类似这样的:

public class MyActivity extends FragmentActivity implements MyFragmentListener { 
    MyFragment1 frag1; 
    MyFragment2 frag2; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(0); 
     FragmentManager fm = getSupportFragmentManager(); 
     frag1 = (MyFragment1) fm.findFragmentByTag("frag1"); 
     frag1.setListener(this); 
     frag2 = (MyFragment2) fm.findFragmentByTag("frag2"); 
    } 

    // Call this function from frag1 when you want to send the data to frag2 
    public void addToFrag2(ListItem item) { 
     frag2.addToList(item); 
    } 
} 

// Define whatever methods the fragments want to use to pass data back and forth 
public interface MyFragmentListener { 
    void addToFrag2(ListItem item); 
} 
相关问题