开发者

Delete button in TableRow

开发者 https://www.devze.com 2023-03-13 03:50 出处:网络
I have an Edit button in my header. And I populating my TableLayout in a loop. In The loop I am inflating a Delete button.

I have an Edit button in my header. And I populating my TableLayout in a loop. In The loop I am inflating a Delete button.

Following is the code I have used :

MainActivity :

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.cookbooklist);

    btnHeaderEdit = (Button)findViewById(R.id.btnEdit);
    btnHeaderEdit.setVisibility(View.VISIBLE);
    delete_button = (View)findViewById(R.id.btnDelete);
    listTable = (TableLayout)findViewById(R.id.cookbookListTable);
    btnHeaderEdit.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
        delete_button.getTag();
        delete_button.setVisibility(View.VISIBLE);  
        }
    });

    for(int i=0;i<=10;i++)
    {
        // Row to display news title
        final TableRow newsRow = new TableRow(this);
        newsRow.setMinimumHeight(200);
        newsRow.setClickable(true);
        newsRow.setTag(i);
        newsRow.setBackgroundColor(Color.WHITE);

        // Some other views

        //create inflater
        LayoutInflater inflater = getLayoutInflater();   
         delete_button = inflater.inflate(R.layout.deletebutton,
                    (ViewGroup) newsRow, false);
         delete_button.setTag(i);
         delete_button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                listTable.removeView(newsRow);
            }
        });

        //Add views in table

        newsRow.addView(delete_button);
        listTable.addView(newsRow);

    }
}  

delebutton.xml :

<Button 
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/btnDelete" 
android:layout_width="100dip" 
android:visibility="inv开发者_运维百科isible"
android:layout_height="40dip" 
android:padding="10sp" 
android:text="Delete">  
</Button>  

Question :

The problem I am facing is that when I click the Edit button in header, Delete button appears only in the last row. When I setVisibility of Delete button to Visible in layout, it appears in all rows. But I want it to be invisible in the start. And on clicking the Edit button it should appear in all rows. I Am wondering how to achieve that.

Can someone assist me? Let me know if more code is needed.

Thanks The problem I am facing is that when


Hi Stone i think you have to use a boolean flag. which wil determine whether the delete button should be visible or not. Try with the following example. Its working fine. On click of edit button if delete buttons are invisible i make all as visible and viceversa. Try the following code.

See My Working Example

Sample2.java

import java.util.ArrayList;
import java.util.Iterator;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class Sample2 extends Activity {
    boolean showDeleteButtonFlag = false;
    Button delteBtn2 = null;
    Button delteBtn1 = null;
    Button editBtn = null;
    ArrayList<Button> deleteButtonList = new ArrayList<Button>(); 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        setContentView(R.layout.tablerow);
        delteBtn1 = (Button) findViewById(R.id.delete1);
        delteBtn2 = (Button) findViewById(R.id.delete2);
//      add all delete buttons buttons to the array list
        deleteButtonList.add(delteBtn1);
        deleteButtonList.add(delteBtn2);
        editBtn = (Button) findViewById(R.id.edit);
        setVisibilityOfDeleteButtons();
        editBtn.setOnClickListener(new OnClickListener() {

            public void onClick(View arg0) {
                // if delete button is visible make it as invisible and viceversa
                showDeleteButtonFlag = !showDeleteButtonFlag;
                setVisibilityOfDeleteButtons();
            }
        });


    }


    void setVisibilityOfDeleteButtons(){

        if(showDeleteButtonFlag){
            Iterator<Button> itr =deleteButtonList.iterator();
            while(itr.hasNext()){
                itr.next().setVisibility(View.VISIBLE);
            }
        }else{
            Iterator<Button> itr =deleteButtonList.iterator();
            while(itr.hasNext()){
                itr.next().setVisibility(View.INVISIBLE);
            }
        }
    }
}

tablerow.xml

<?xml version="1.0" encoding="utf-8"?>
<TableLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <TableRow>
        <Button
            android:id="@+id/edit"
            android:layout_width="100dip"
            android:layout_height="60dip"
            android:text="Edit" />

        <Button
            android:id="@+id/delete1"
            android:layout_width="100dip"
            android:layout_height="60dip"
            android:text="Delete" />
    </TableRow>

    <TableRow>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Password:"
            android:textColor="#ffffff"
            android:textStyle="bold" />

        <Button
            android:id="@+id/delete2"
            android:layout_width="100dip"
            android:layout_height="60dip"
            android:text="Delete" />
    </TableRow>
</TableLayout>
0

精彩评论

暂无评论...
验证码 换一张
取 消