2017-09-05 72 views
0

我想用不同的ListViews和選項製作簡單的選項卡式應用程序。 我想現在管理標籤和MainActivity和問題之間的關係,我不能做出的addItem()函數在一個菜單按鈕,一個新的項目在一個標籤式應用程序添加到我的ListView(片段內)。 我已經搜索了很多,我發現只是在簡單的ListView(MainActivity內部)中添加項目,或者在選項卡式應用程序中添加項目不是動態的。ANDROID - 在片段列表中添加新項目(選項卡式) - Android Studio

因此,這裏是我試圖使用的我的選項卡式應用程序和ListView的屏幕。 Custom List View Application Picture

[1]: https://i.stack.imgur.com/Ze4eu.png

這裏是我的代碼: MainActivity.java

public class MainActivity extends AppCompatActivity { 



private SectionsPagerAdapter mSectionsPagerAdapter; 

/** 
* The {@link ViewPager} that will host the section contents. 
*/ 
private ViewPager mViewPager; 

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

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 
    // Create the adapter that will return a fragment for each of the three 
    // primary sections of the activity. 
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); 

    // Set up the ViewPager with the sections adapter. 
    mViewPager = (ViewPager) findViewById(R.id.container); 
    mViewPager.setAdapter(mSectionsPagerAdapter); 

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs); 
    tabLayout.setupWithViewPager(mViewPager); 

} 

片段與CustomList,我想實現的菜單按鈕方法action_add():

public class Catalogo extends Fragment { 
    ArrayList<CustomList> custom = null; 
    ListView lv = null; 
    ArrayAdapter adapter = null; 
    ArrayList<CustomList> elementos = new ArrayList<CustomList>(); 

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.catalogo, container, false); 
     ListView lv = rootView.findViewById(R.id.catalogoListView); 

     custom = addItems(); 
     adapter = new CustomAdapter(this.getContext(), custom); 
     lv.setAdapter(adapter); 

     return rootView; 
    } 

    private ArrayList<CustomList> addItems(){ 
     CustomList custom = new CustomList("Banana", "4.0"); 
     elementos.add(custom); 
     custom = new CustomList("Morango", "5.0"); 
     elementos.add(custom); 
     return elementos; 
    } 

    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(); 


     if (id == R.id.action_add){ 
      AlertDialog.Builder builder = new AlertDialog.Builder(this.getActivity()); 
      //builder.setTitle("Adicionar novo item"); //THESE ARE NOT WORKING 
      //final EditText input = new EditText(this); //THIS SHOULD BE 2 Text Fields 
      //builder.setView(input); 
      builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int which) { 
        //NEED TO IMPLEMENT HERE 
       } 
      }); 
      builder.setNegativeButton("Cancelar", new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int which) { 
        dialog.cancel(); 
       } 
      }); 
      builder.show(); 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 
    public static String preferredCase(String original, String price) 
    { 
     if (original.isEmpty()) 
      return original; 

     return original.substring(0, 1).toUpperCase() + original.substring(1).toLowerCase(); 
    } 

} 

我的CustomItem結構:

public class CustomList { 
    String name; 
    String price; 
    public CustomList(String name, String price) { 
     this.name = name; 
     this.price = price; 
    } 
    public String getName() { 
     return name; 
    } 
    public String getPrice() { 
     return price; 
    } 
} 

和結束時,我CustomAdapter:

public class CustomAdapter extends ArrayAdapter<CustomList> { 
    private final Context context; 
    private final ArrayList<CustomList> elementos; 

    public CustomAdapter(Context context, ArrayList<CustomList> elementos){ 
     super(context, R.layout.modelolista, elementos); 
     this.context = context; 
     this.elementos = elementos; 
    } 
    public View getView(int position, View convertView, ViewGroup parent){ 
     LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View rowView = inflater.inflate(R.layout.modelolista, parent, false); 

     TextView nameItem = rowView.findViewById(R.id.tvName); 
     TextView priceItem = rowView.findViewById(R.id.tvPrice); 

     nameItem.setText(elementos.get(position).getName()); 
     priceItem.setText(elementos.get(position).getPrice()); 

     return rowView; 
    } 
} 
+0

對不起,但我沒有得到它..你想添加一個項目列表通過點擊一個菜單項並通知添加列表,以便它可以顯示更新列表? –

+0

是的,你是對的,通過菜單項在列表中添加一個項目。 問題是..我實現了MainActivity中的列表和菜單項...但我無法在片段中實現它們。 因此,有兩種選擇。首先,在MainActivity中實現CustomList和所有菜單按鈕,但之後我不知道如何將此「製作的自定義列表」傳遞給片段(位於「catalogo」選項卡中)。 其次是實現Fragment(「Catalogo」選項卡)中的所有內容,但是接下來我無法像在MainActivity中那樣實現按鈕功能。 –

+0

沒問題,只知道:只有在顯示目錄片段或者您總是看到它時,纔會看到「添加」按鈕? –

回答

0

在片段使用活動的數據,最好的辦法是在片段創建界面和活動類實現

public class Catalogo extends Fragment { 
    private CatalogFragmentInteface catalogInterface; 
    //rest of your code 

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
      View rootView = inflater.inflate(R.layout.catalogo, container, false); 
      catalogIntreface.catalogInterfaceFunction(rootView); 
    } 
@Override 
    public void onAttach(Context context) { 
     super.onAttach(context); 
     if (context instanceof CatalogFragmentInterface) { 
      catalogInterface = (CatalogFragmentInterface) context; 
     } else { 
      throw new RuntimeException(context.toString() 
        + " must implement CatalogFragmentInterface"); 
     } 
    } 

    interface CatalogFragmentInterface { 
     void catalogInterfaceFunction(View view); 
    } 
    } 

而且在您的Activity類中實現此接口

public class MainActivity extends AppCompatActivity implement Catalog.CatalogFragmentInterface{ 
@Overide 
void catalogInterfaceFunction(View view) { 
    //access your listview in fragment using view variable 
} 
} 

通過這種方式,您的所有數據和列表視圖都存在於同一個類中。 我希望這會幫助你。而不是調用你的項目CustomList,考慮調用它CustomListItem

+0

我編輯了我的答案 –

+0

對不起,我沒有注意到,我在這裏寫了這個答案。你可以改變變量名 –

+0

當我把你的和我的代碼混合起來的時候,我搞砸了,現在它完成了我的工作。抱歉給你帶來不便。 –

0

首先建議的。通過這種方式,您將來很容易就會記得它是一個項目,而不是一個列表。

這就是說,你應該實現一個添加頁面\對話框。

有這樣做的..「最佳辦法」(我認爲)將創建一個自定義對話框中添加多種方式。所以,你必須創建一個自定義佈局,並把它作爲你的佈局與像這樣的觀點:

AlertDialog.Builder builder = new AlertDialog.Builder(activity); 
LayoutInflater inflater = activity.getLayoutInflater(); 
View dialogView = inflater.inflate(R.layout.my_custom_view, null); 
builder.setView(dialogView); 

現在,你可以簡單地實現所有listners並通過獲取項目充塞這裏Button byButton = (Button)dialogView.getItemById(R.id.mybutton);

對於任何信息檢查this answer可能會幫助你(還有很多其他人)。

現在我們懷念的是刷新列表視圖,這個我覺得是最簡單的方法:

您可以在Activity保存自定義Fragment的參考。然後在Fragment中添加AddItemToList()之類的方法。現在,當用戶點擊 「添加」 按鈕,你可以從你的片段引用調用此方法並實現方法如下(+ - ):

回調(MainActivity)

//add this param 
private MyFragment myFragment; 

//when you show the fragment add this: 
myFragment = myFragmentIstance; 

//on the save of the dialog, add this: 
//do all checks and create item 
CustomListItem cli = new CustomListItem(editTextName.getText().toString(), editTextPrice.getText().toString()); 
myFragment.AddItemToList(cli); 

AddItemToList(片段):

public void AddItemToList(CustomListItem cli){ 
    myCustomListItemList.Add(cli); 
    myAdapterView.notifyDataSetChanged(); 
} 

像這樣的東西應該工作。對於我在這裏的任何問題或交代。祝你好運

+0

不應該在片段中的onAttach ? –

相關問題