我正在学习android开发,并且卡在Fragments
中。我已阅读教程指导手册和一些Android开发人员文档,因此下一步是为我的手机构建应用程序。Android ListFragment重叠
我正在尝试制作购物清单。当你启动应用程序时,会显示一个菜单,显示三个选项,有问题的是第三个。
这个想法是允许用户创建更新并从该列表中删除产品。
所以这是我迄今所做
product_crud.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="116dp">
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/name_txt"
android:hint="@string/product_name"
android:inputType="text"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn_add"
android:paddingLeft="10px"
android:text="@string/add_product"
android:layout_below="@+id/name_txt"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/android:list"
android:layout_below="@+id/btn_add"
android:layout_alignParentStart="true" />
</LinearLayout>
crudrowlayout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="10px"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn_delete"
android:text="@string/remove_product"
android:layout_gravity="right"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/text_name"
android:textSize="40px"
android:layout_alignTop="@+id/btn_delete"
android:layout_alignParentStart="true"
android:layout_alignBottom="@+id/btn_delete" />
</RelativeLayout>
PM_List_Fragment
public class PM_List_Fragment extends ListFragment {
ProductsDataSource products;
@Override
public void onActivityCreated(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
products = new ProductsDataSource(getActivity());
try {
products.open();
} catch (SQLException e) {
e.printStackTrace();
}
CrudArrayAdapter crudArrayAdapter = new CrudArrayAdapter(getActivity(),products.getAllProducts());
products.close();
setListAdapter(crudArrayAdapter);
}
}
适配器
public class CrudArrayAdapter extends ArrayAdapter<Product> {
private final List<Product> list;
private final Activity context;
static class ViewHolder{
protected TextView name;
protected Button delete;
}
public CrudArrayAdapter(Activity context,List<Product> list) {
super(context, R.layout.rowlayout,list);
this.list = list;
this.context = context;
}
@Override
public View getView(int position,View convertView,ViewGroup parent){
View view;
if(convertView == null){
LayoutInflater inflater = context.getLayoutInflater();
view = inflater.inflate(R.layout.crudrowlayout,null);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.name = (TextView)view.findViewById(R.id.text_name);
viewHolder.delete = (Button)view.findViewById(R.id.btn_delete);
view.setTag(viewHolder);
}else{
view = convertView;
}
ViewHolder holder = (ViewHolder) view.getTag();
holder.name.setText(list.get(position).getName());
return view;
}
}
和我活动
public class CrudProducts extends ListActivity {
private String msg = "Android: ";
private ProductsDataSource productsDataSource;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
// ExampleFragment fragment = (ExampleFragment) getFragmentManager().findFragmentById(R.id.example_fragment);
setContentView(R.layout.product_crud);
PM_List_Fragment pm_list_fragment = new PM_List_Fragment();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(android.R.id.content,pm_list_fragment);
Button btn_add = (Button)this.findViewById(R.id.btn_add);
btn_add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//create a form or put some input text so we can update the db!
EditText editText = (EditText)findViewById(R.id.name_txt);
if(editText.getText().length() > 0 && !productsDataSource.exists(editText.getText().toString())){
Product prod = productsDataSource.createProduct(editText.getText().toString());
// adapter.add(prod);
}else{
if(editText.getText().length() == 0)
{
Toast.makeText(getApplicationContext(),"Please insert a name",Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(getApplicationContext(),"This product already exists",Toast.LENGTH_LONG).show();
}
}
// adapter.notifyDataSetChanged();
}
});
fragmentTransaction.commit();
}
}
不用担心数据源或适配器,这里的问题是,ListView
与EditText
重叠和Button
(deleteBtn)。 列表视图显示在EditText
和Button
之上。我试图以不同的方式改变布局,但没有一个能够工作。
我读过,你可以用一个正常的活动和一个ListView
实现这个相同的功能,但我想学习如何做到这一点。
有些想法?
问题的图片:
请只发表相关的代码/布局。读者通过扫描多个文件找到相关文件是非常令人困惑的。 – Naveed
嗨,你的问题是在片段管理器。尝试用'replace()'方法替换'add()'片段。像:'transaction.replace(R.id.fragment_container,newFragment); transaction.addToBackStack(null); //提交交易 transaction.commit(); '[http://developer.android.com/training/basics/fragments/fragment-ui.html] – EugenUngurean