提前致谢,这是我的问题。我有一个数据库,每当" SHOW DATA"单击按钮(在相同的活动(布局)上)。因此,当用户再次单击该按钮时,我想删除布局中的视图,以便数据库的内容不会显示两次。我已经使用了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
答案 0 :(得分:0)
你必须删除
LinearLayout ll = (LinearLayout) findViewById(R.id.cardViewContainer);
此行来自onClickShowData()方法,并添加
LinearLayout ll;
到活动课程的顶部。 然后在onCreate(Bundle savedInstanceState)方法中添加,
ll = (LinearLayout) findViewById(R.id.cardViewContainer);
在当前代码中,您将向ll添加视图并在另一个对象中调用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);
}
}
}
);
}
}
答案 1 :(得分:0)
您正在使用onClick()
设置showAll.setOnClickListener
侦听器,但执行的代码不包括ll.removeAllViews()
。将单击侦听器更改为以下内容,以便在单击按钮时删除视图。
showAll.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
LinearLayout ll = (LinearLayout) findViewById(R.id.cardViewContainer);
ll.removeAllViews();
...etc.