2017-08-06 123 views
0

在此先感謝,這裏是我的問題。我有一個數據庫,只要點擊「SHOW DATA」按鈕(在同一個活動(佈局)上),我必須顯示它的內容。所以當用戶再次單擊該按鈕時,我想刪除佈局中的視圖,以便數據庫的內容不會顯示兩次。我已經使用removeAllView()和removeAllViewsInLayout(),但它似乎不工作(肯定我沒有正確使用此方法)。removeAllView()和removeAllViewsInLayout()不會刪除

下面是Java代碼:

public void onClickShowData(){ 

    LinearLayout ll = (LinearLayout) findViewById(R.id.cardViewContainer); 

    ll.removeAllViews(); 


    showAll.setOnClickListener(
      new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        Cursor cursor = myDB.getAllData(); 

        if (cursor.getCount() == 0) { 
         //Toast.makeText(MainActivity.this, "There is no data", Toast.LENGTH_SHORT).show(); 
         showData("Error", "Nothing found"); 
         return; 
        } 


        StringBuffer tmp = new StringBuffer(); 

        LinearLayout ll = (LinearLayout)findViewById(R.id.cardViewContainer); 
        Context context = getApplicationContext(); 

        LinearLayout ll2 = new LinearLayout(context); 
        ll2.setOrientation(LinearLayout.VERTICAL); 
        ll2.setLayoutParams(new ActionBar.LayoutParams(
          ViewGroup.LayoutParams.MATCH_PARENT, 
          ViewGroup.LayoutParams.MATCH_PARENT)); 

        TextView tv = new TextView(context); 

        CardView cv = new CardView(context); 

        cv.setId(R.id.cardView); 

        while (cursor.moveToNext()){ 
         tmp.append("name : " + cursor.getString(0) + "\n" + 
           "calory : " + cursor.getString(1) + "\n" + 
           "protein : " + cursor.getString(2) + "\n" + 
           "lipid : " + cursor.getString(3) + "\n" + 
           "clucid: " + cursor.getString(4)); 
         ll.addView(cv); 
         cv.addView(ll2); 
         tv.setText(tmp.toString()); 
         ll2.addView(tv); 
        } 




       } 
      } 
    ); 
} 

,這是佈局:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_database" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="gafood.mobiledeviceproject.arianchitgar.foodmakerwithga.databaseActivity"> 


    <ScrollView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" 
      android:id="@+id/scrollViewLL"> 

      <EditText 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:inputType="textPersonName" 
       android:ems="10" 
       android:layout_alignParentTop="true" 
       android:layout_centerHorizontal="true" 
       android:id="@+id/Name" 
       android:hint="Food name"/> 

      <EditText 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:inputType="numberDecimal" 
       android:ems="10" 
       android:layout_below="@+id/Name" 
       android:layout_centerHorizontal="true" 
       android:id="@+id/Calory" 
       android:hint="Calory"/> 

      <EditText 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:inputType="numberDecimal" 
       android:ems="10" 
       android:layout_below="@+id/Calory" 
       android:layout_alignRight="@+id/Calory" 
       android:layout_alignEnd="@+id/Calory" 
       android:id="@+id/protein" 
       android:hint="Protein" /> 

      <EditText 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:inputType="numberDecimal" 
       android:ems="10" 
       android:layout_below="@+id/protein" 
       android:layout_centerHorizontal="true" 
       android:id="@+id/lipid" 
       android:hint="Lipid" /> 

      <Button 
       android:text="Add food" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_below="@+id/lipid" 
       android:layout_alignParentLeft="true" 
       android:layout_alignParentStart="true" 
       android:layout_marginTop="22dp" 
       android:id="@+id/addFood" /> 

      <Button 
       android:text="Update" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_alignBottom="@+id/addFood" 
       android:layout_centerHorizontal="true" 
       android:id="@+id/updateFood" /> 

      <Button 
       android:text="Delete" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_alignTop="@+id/updateFood" 
       android:layout_alignParentRight="true" 
       android:layout_alignParentEnd="true" 
       android:id="@+id/delete" /> 

      <Button 
       android:text="Food DB" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_below="@+id/addFood" 
       android:layout_alignParentLeft="true" 
       android:layout_alignParentStart="true" 
       android:id="@+id/showAll" /> 

      <Button 
       android:text="search" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_below="@+id/updateFood" 
       android:layout_alignLeft="@+id/updateFood" 
       android:layout_alignStart="@+id/updateFood" 
       android:id="@+id/search" /> 

      <Button 
       android:text="Advanced" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_below="@+id/delete" 
       android:layout_alignLeft="@+id/delete" 
       android:layout_alignStart="@+id/delete" 
       android:id="@+id/advancedSearch" /> 

      <LinearLayout 
       android:orientation="vertical" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:id="@+id/cardViewContainer"></LinearLayout> 


     </LinearLayout> 
    </ScrollView> 


</RelativeLayout> 


enter code here 

When the app starts When i click 3 times on show DB

回答

0

您必須刪除

LinearLayout ll = (LinearLayout) findViewById(R.id.cardViewContainer); 

這條線從內onClickShowData()方法,並添加

LinearLayout ll; 

要將活動類的頂部。 那麼的onCreate(捆綁savedInstanceState)方法添加,

ll = (LinearLayout) findViewById(R.id.cardViewContainer); 

在當前的代碼中,要添加意見,爲11和調用ll.removeAllViews()在不同的對象。

最終代碼應該是這樣的,

public class YourActivityName extends Activity { 
LinearLayout ll; 
@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     ll = (LinearLayout) findViewById(R.id.cardViewContainer); 
     // .... other code // 
} 

public void onClickShowData(){ 
    ll.removeAllViews(); 
    showAll.setOnClickListener(
      new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        Cursor cursor = myDB.getAllData(); 

        if (cursor.getCount() == 0) { 
         //Toast.makeText(MainActivity.this, "There is no data", Toast.LENGTH_SHORT).show(); 
         showData("Error", "Nothing found"); 
         return; 
        } 


        StringBuffer tmp = new StringBuffer(); 


        Context context = getApplicationContext(); 

        LinearLayout ll2 = new LinearLayout(context); 
        ll2.setOrientation(LinearLayout.VERTICAL); 
        ll2.setLayoutParams(new ActionBar.LayoutParams(
          ViewGroup.LayoutParams.MATCH_PARENT, 
          ViewGroup.LayoutParams.MATCH_PARENT)); 

        TextView tv = new TextView(context); 

        CardView cv = new CardView(context); 

        cv.setId(R.id.cardView); 

        while (cursor.moveToNext()){ 
         tmp.append("name : " + cursor.getString(0) + "\n" + 
           "calory : " + cursor.getString(1) + "\n" + 
           "protein : " + cursor.getString(2) + "\n" + 
           "lipid : " + cursor.getString(3) + "\n" + 
           "clucid: " + cursor.getString(4)); 
         ll.addView(cv); 
         cv.addView(ll2); 
         tv.setText(tmp.toString()); 
         ll2.addView(tv); 
        } 

       } 
      } 
    ); 
} 
} 
0

您與showAll.setOnClickListener設置你的聽衆onClick()但被執行的代碼不包括ll.removeAllViews()。將點擊偵聽器更改爲類似以下內容,以便在單擊該按鈕時刪除視圖。

showAll.setOnClickListener(
      new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
    LinearLayout ll = (LinearLayout) findViewById(R.id.cardViewContainer); 
    ll.removeAllViews(); 

...etc.