我正在學習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