1

所以我試圖在EditText字段中添加文本之後在一個片段中填充ListView,然後在另一個片段中單擊按鈕。在按鈕單擊片段後填充ListView

我的java編程技巧非常糟糕,所以任何代碼建議都會幫助我很多。

我希望這是有道理的。

這是我的代碼。

public class FieldsActivity extends FragmentActivity { 
private static final int NUMBER_OF_PAGES = 3; 
ViewPager mPager; 
MyFragmentPagerAdapter mMyFragmentPagerAdapter; 
ArrayList<String>siteList = new ArrayList<String>(); 
CustomAdapter ca = null; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); 
    setContentView(R.layout.fields); 
    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.topbar); 
    mPager = (ViewPager) findViewById(R.id.fieldspager); 
    mMyFragmentPagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager()); 
    mPager.setAdapter(mMyFragmentPagerAdapter); 
    mPager.setCurrentItem(1); 
} 

我的ListView位於哪裏。

public class AddSite extends Fragment { 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     RelativeLayout mRelativeLayout = (RelativeLayout) inflater.inflate(R.layout.add_site, container, false); 
     Button addSiteButton = (Button) mRelativeLayout.findViewById(R.id.addSiteButton); 
     addSiteButton.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View view) { 
       mPager.setCurrentItem(2, true); 
      } 
     }); 

     ListView lv = (ListView) findViewById(android.R.id.list); 
     CustomAdapter ca = new CustomAdapter(); 
     lv.setAdapter(ca); 
     return mRelativeLayout; 
    } 

    class CustomAdapter extends ArrayAdapter<String> { 
     CustomAdapter() { 
      super(FieldsActivity.this, android.R.layout.simple_list_item_1, siteList); 
     } 

     public View getView(int position, View convertView, ViewGroup parent) { 
      View row = convertView; 

      if (row == null) { 
       LayoutInflater inflater=getLayoutInflater(); 
       row = inflater.inflate(R.layout.list_row, null); 
      } 
      ((TextView)row.findViewById(R.id.textViewId)).setText(siteList.get(position)); 
     } 
     return row; 
    } 
} 

其中Button和EditText是。

public class CreateSite extends Fragment { 

    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     RelativeLayout mRelativeLayout = (RelativeLayout) inflater.inflate(R.layout.main, container, false); 
     final EditText siteEditText = (EditText)findViewById(R.id.siteEditText); 
     Button signInButton = (Button) mRelativeLayout.findViewById(R.id.sign_in_button); 
     signInButton.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View view) { //Click this button and add to ListView in class AddSite 
       siteList.add(0, siteEditText.getText().toString()); 
       ca.notifyDataSetChanged(); //(Bundle) cannot be applied 
       siteEditText.setText(""); 
       mPager.setCurrentItem(1, true); 
      } 
     }); 
     return mRelativeLayout; 
    } 
} 

雖然都在一個FragmentPagerAdapter

private class MyFragmentPagerAdapter extends FragmentPagerAdapter { 

    public MyFragmentPagerAdapter(FragmentManager fm) { 
     super(fm); 
    } 
    @Override 
    public Fragment getItem(int position) { 
     switch (position) { 
      case 0: return new SettingFields(); 
      case 1: return new AddSite(); 
      case 2: return new CreateSite(); 
     } 
     return null; 
    } 
    @Override 
    public int getCount() { 

     return NUMBER_OF_PAGES; 
    } 
} 

} 

所以我評論說顯示的錯誤。

希望這是有道理的。 讓我知道你是否需要看別的東西。

在此先感謝!

回答

1

你的碎片不應該直接對話。相反,每個片段應報告活動,並且活動可以決定如何處理該活動。

執行此操作的正常方法是讓Fragment定義一個接口,並讓Activity實現該接口。然後在Activity上有一個片段可以調用的方法。然後Activity可以調用另一個Fragment上的方法來使其執行一些操作。

請參閱有關Fragment communication的文檔。

+0

我將如何更改我的代碼以符合這些要求,或者是我寫的這種方式是否錯誤?換句話說,這是可以修復的還是我需要重新開始? – PhDeOliveira