我在片段中进行沟通时遇到问题, 以下是我的代码:
ListPolicies Fragment
public class ListPolicies extends ListFragment implements AdapterView.OnItemClickListener{
String polnames;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.list_policies, null, false);
}
public String polName(String name){
if(name!=null) {
polnames = name;
}
Log.d("Luffy", "Pos" +polnames);
return polnames;
}
String polname = polName(polnames);
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if (polname != null) {
Log.d("Luffy", "Position 00");
if (polname.equalsIgnoreCase("0")) {
Log.d("Luffy", "Position 0");
ArrayAdapter adapter = ArrayAdapter.createFromResource(getActivity(), R.array.Engineering_Insurance, android.R.layout.simple_list_item_1);
setListAdapter(adapter);
getListView().setOnItemClickListener(this);
}
}
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getActivity(), "Uploaded :" + position, Toast.LENGTH_LONG).show();
}
}
ViewPolicy片段
public class ViewPolicyFragment extends ListFragment implements AdapterView.OnItemClickListener{
String[] menutitles;
TypedArray menuIcons;
PolicyCommunicator comm;
CustomAdapter adapter;
private List<rowItem> rowItems;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.view_policy_fragment, null, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
menutitles = getResources().getStringArray(R.array.Insurance);
menuIcons = getResources().obtainTypedArray(R.array.icons);
rowItems = new ArrayList<rowItem>();
for (int i = 0; i < menutitles.length; i++) {
rowItem items = new rowItem(menutitles[i], menuIcons.getResourceId(
i, -1));
rowItems.add(items);
}
adapter = new CustomAdapter(getActivity(), rowItems);
setListAdapter(adapter);
comm = (PolicyCommunicator) getActivity();
getListView().setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
comm.policyName("" +position );
if(menutitles[position].equalsIgnoreCase("Engineering Insurance")){
Toast.makeText(getActivity(), "You have Clicked on Engineering Insurance", Toast.LENGTH_SHORT)
.show();
}
if(menutitles[position].equalsIgnoreCase("Fire Insurance")){
Toast.makeText(getActivity(), "You have Clicked on Fire Insurance", Toast.LENGTH_SHORT)
.show();
}
if(menutitles[position].equalsIgnoreCase("Liability Insurance")){
Toast.makeText(getActivity(), "You have Clicked on Liability Insurance", Toast.LENGTH_SHORT)
.show();
}
}
}
和我使用已实施方法的活动
public class AfterLoginActivity extends AppCompatActivity implements PolicyCommunicator {
Toolbar toolbar;
FragmentManager manager;
Intent intent= null, chooser= null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.after_login_activity);
toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
manager = getFragmentManager();
AfterLoginAboutFragment fragViewPolicy = new AfterLoginAboutFragment();
FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.linearLayoutLoginFragment, fragViewPolicy, "View Policy");
transaction.commit();
}
@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 void addLoginAboutFragment (View v){
AfterLoginAboutFragment fragViewPolicy = new AfterLoginAboutFragment();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.linearLayoutLoginFragment, fragViewPolicy, "View Policy");
transaction.commit();
}
public void addViewPolicyFragment (View v){
ViewPolicyFragment fragViewPolicy = new ViewPolicyFragment();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.linearLayoutLoginFragment, fragViewPolicy, "View Policy");
transaction.commit();
}
public void addMyProfile (View v){
MyProfileFragment fragProfile = new MyProfileFragment();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.linearLayoutLoginFragment, fragProfile, "My profile");
transaction.commit();
}
public void addMyPolicy (View v){
MyPolicyFragment fragMyPolicy = new MyPolicyFragment();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.linearLayoutLoginFragment, fragMyPolicy, "My Policy");
transaction.commit();
}
public void addUploadFragment (View v){
UploadPhotoFragment fragUpload = new UploadPhotoFragment();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.linearLayoutLoginFragment, fragUpload, "Uploaded Policy");
transaction.commit();
}
public void addUploadedFragment (View v){
UploadedPolicyFragment fragUploaded = new UploadedPolicyFragment();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.linearLayoutLoginFragment, fragUploaded, "Uploaded Policy");
transaction.commit();
}
@Override
public void policyName(String name) {
Log.d("Luffy","Inside PolicyName AfterLogin");
ListPolicies fragList = new ListPolicies();
FragmentTransaction transaction = manager.beginTransaction();
fragList.polName(name);
transaction.replace(R.id.linearLayoutLoginFragment, fragList, "Uploaded Policy");
transaction.commit();
}
}
输出:
10-28 14:00:18.400 2174-2174/onepiece.luffy.investaccui D/Luffy: Inside PolicyName AfterLogin
10-28 14:00:18.400 2174-2174/onepiece.luffy.investaccui D/Luffy: Posnull
10-28 14:00:18.400 2174-2174/onepiece.luffy.investaccui D/Luffy: Pos0
我希望输出为:
10-28 14:00:18.400 2174-2174/onepiece.luffy.investaccui D/Luffy: Inside PolicyName AfterLogin
10-28 14:00:18.400 2174-2174/onepiece.luffy.investaccui D/Luffy: Pos0
所以我可以调用我的Engineering_insurance listView
需要你帮助的人...... 提前致谢
答案 0 :(得分:1)
String polname = polName(polnames);
用于创建polname字符串。
public String polName(String name){
if(name!=null) {
polnames = name;
}
Log.d("Luffy", "Pos" +polnames);
return polnames;
}
是所描述的方法。如果名字!= null polonaise = name。
来自
String polname = polName(polnames);
参数是“polonames”变量,在此处声明
String polnames;
但从未初始化