我想用不同的ListViews和選項製作簡單的選項卡式應用程序。 我想現在管理標籤和MainActivity和問題之間的關係,我不能做出的addItem()函數在一個菜單按鈕,一個新的項目在一個標籤式應用程序添加到我的ListView(片段內)。 我已經搜索了很多,我發現只是在簡單的ListView(MainActivity內部)中添加項目,或者在選項卡式應用程序中添加項目不是動態的。ANDROID - 在片段列表中添加新項目(選項卡式) - Android Studio
因此,這裏是我試圖使用的我的選項卡式應用程序和ListView的屏幕。 Custom List View Application Picture
這裏是我的代碼: 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;
}
}
對不起,但我沒有得到它..你想添加一個項目列表通過點擊一個菜單項並通知添加列表,以便它可以顯示更新列表? –
是的,你是對的,通過菜單項在列表中添加一個項目。 問題是..我實現了MainActivity中的列表和菜單項...但我無法在片段中實現它們。 因此,有兩種選擇。首先,在MainActivity中實現CustomList和所有菜單按鈕,但之後我不知道如何將此「製作的自定義列表」傳遞給片段(位於「catalogo」選項卡中)。 其次是實現Fragment(「Catalogo」選項卡)中的所有內容,但是接下來我無法像在MainActivity中那樣實現按鈕功能。 –
沒問題,只知道:只有在顯示目錄片段或者您總是看到它時,纔會看到「添加」按鈕? –