2016-07-14 34 views
1

所以即时通讯尝试弄清楚,如果我可以将一个自定义适配器的意图额外传递给一个活动,然后将该意图额外传递给另一个活动?可能将意图额外从自定义适配器传递到另一个自定义适配器的活动?

是这样的可能或有没有更好的方式来做我想做的事情?

+0

什么是你想更具体做什么?你可以分享一些代码,显示你到目前为止的尝试吗?没有看到你的代码很难提供帮助。 – Tharkius

回答

1
Activity A: 

Intent intent = new Intent(A.this, B.class); 
intent.putExtra("someString", "string"); 
startActivity(intent): 

Activity B: 

onCreate(...) { 
String myString = getIntent().getStringExtra("someString"); 
MyAdapter myAdapter = new MyAdapter(B.this, myString); 
} 

MyAdapter : 

Context myContext; 
String myString; 

MyAdapter(Context context, String string) { 
this.myContext = context; 
this.myString = string 
} 

现在,你必须从活性的字符串转换适配器:)

0

我认为,你知道如何意图额外送活动。而你正在寻找的是将同样意图转发给另一项活动的方式。你可以这样做:

Intent intent = new Intent(this, SecondActivity.class); 
intent.putExtras(getIntent()); 
0

上面提到的所有答案都可以。我最终使用的是我的列表视图上的onItemClickListener,并以这种方式获取了信息。相反,在自定义适配器启动意图,我就开始活动中的自定义适配器是在意向。

yourListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 

      //This is where you get the item clicked and specifically what you want from the custom adapter. 
      String yourString = ((TextView) view.findViewById(R.id.yourTextView)).getText().toString(); 



      Intent goToNewActivity = new Intent(YourActivity.this, MainActivity.class); 
      goToNewActivity.putExtra("yourExtra", yourString); 
      startActivity(goToNewActivity); 
     } 
    }); 
0
yourListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 

      //This is where you get the item clicked and specifically what you want from the custom adapter. 
      String yourString = ((TextView) view.findViewById(R.id.yourTextView)).getText().toString(); 



      Intent goToNewActivity = new Intent(YourActivity.this, MainActivity.class); 
      goToNewActivity.putExtra("yourExtra", yourString); 
      startActivity(goToNewActivity); 
     } 
    });