ANDROID - 在片段列表中添加新项目(选项卡式) - Android Studio
我想用不同的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;
}
}
在片段使用活动的数据,最好的办法是在片段创建界面和活动类实现
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
:
我编辑了我的答案 –
对不起,我没有注意到,我在这里写了这个答案。你可以改变变量名 –
当我把你的和我的代码混合起来的时候,我搞砸了,现在它完成了我的工作。抱歉给你带来不便。 –
首先建议的。通过这种方式,您将来很容易就会记得它是一个项目,而不是一个列表。
这就是说,你应该实现一个添加页面\对话框。
有这样做的..“最佳办法”(我认为)将创建一个自定义对话框中添加多种方式。所以,你必须创建一个自定义布局,并把它作为你的布局与像这样的观点:
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();
}
像这样的东西应该工作。对于我在这里的任何问题或交代。祝你好运
不应该在片段中的onAttach ? –
对不起,但我没有得到它..你想添加一个项目列表通过点击一个菜单项并通知添加列表,以便它可以显示更新列表? –
是的,你是对的,通过菜单项在列表中添加一个项目。 问题是..我实现了MainActivity中的列表和菜单项...但我无法在片段中实现它们。 因此,有两种选择。首先,在MainActivity中实现CustomList和所有菜单按钮,但之后我不知道如何将此“制作的自定义列表”传递给片段(位于“catalogo”选项卡中)。 其次是实现Fragment(“Catalogo”选项卡)中的所有内容,但是接下来我无法像在MainActivity中那样实现按钮功能。 –
没问题,只知道:只有在显示目录片段或者您总是看到它时,才会看到“添加”按钮? –