【发布时间】:2017-09-05 12:51:07
【问题描述】:
我正在尝试使用不同的 ListViews 和选项制作一个简单的选项卡式应用程序。 我想管理选项卡和 MainActivity 之间的关系,现在的问题是我无法在菜单按钮中创建 addItem() 函数,以便在选项卡式应用程序中向我的 ListView(在 Fragment 内)添加新项目。 我已经搜索了很多,但我发现只在一个简单的 ListView(在 MainActivity 内)中添加一个项目或在选项卡式应用程序中添加非动态项目。
所以这里是我的选项卡式应用程序的屏幕和我正在尝试使用的 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 中实现了列表和菜单项……但我无法在 Fragment 中实现它们。所以,有两个选项。首先,在 MainActivity 中实现 CustomList 和所有菜单按钮,但是我不知道如何将这个“制作的自定义列表”传递给片段(在“目录”选项卡中)。其次,在 Fragment(“Catalogo”选项卡)中实现所有内容,但是,我无法像在 MainActivity 中那样实现按钮功能。
-
好的,只是想知道:只有在显示目录片段时才会看到“添加”按钮,或者您总是看到它?
-
总是这样,我在 MenuLayout 中制作了添加按钮。但是很抱歉,看起来如此混乱,所以我将重新制作所有内容并再次发布。
标签: java android listview android-fragments android-tablayout