Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/204.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android:删除以编程方式添加的ID为';s_Android_Android Layout_Android Intent_Android Gridlayout_Android Imagebutton - Fatal编程技术网

Android:删除以编程方式添加的ID为';s

Android:删除以编程方式添加的ID为';s,android,android-layout,android-intent,android-gridlayout,android-imagebutton,Android,Android Layout,Android Intent,Android Gridlayout,Android Imagebutton,进展如何?我正在为一个项目创建一个小培训应用程序,它运行良好,只是我遇到了一个格式问题。因此,我需要一个csv文件,其中包含客户的名称和年龄。从中创建了一个数组,然后我得到了一个包含网格布局的滚动视图,我从客户机数组中创建了图像按钮。没关系。我在最后有一个添加客户端按钮,按钮和它的活动都很好,但是当你回到主屏幕时,按钮都被搞乱了(巨大的,放错地方等)。所以我想我会循环并删除所有按钮,然后重新填充主屏幕,除了,因为我是通过编程创建它们的,我不知道如何找到它们来删除它们。我尝试将它们的id设置为数组

进展如何?我正在为一个项目创建一个小培训应用程序,它运行良好,只是我遇到了一个格式问题。因此,我需要一个csv文件,其中包含客户的名称和年龄。从中创建了一个数组,然后我得到了一个包含网格布局的滚动视图,我从客户机数组中创建了图像按钮。没关系。我在最后有一个添加客户端按钮,按钮和它的活动都很好,但是当你回到主屏幕时,按钮都被搞乱了(巨大的,放错地方等)。所以我想我会循环并删除所有按钮,然后重新填充主屏幕,除了,因为我是通过编程创建它们的,我不知道如何找到它们来删除它们。我尝试将它们的id设置为数组的索引,但随后出现空指针错误

创建按钮的函数:

public void fillActivity_main(){
    if(listPopulated == false) { // check to see if its aready been created
        populateClientList();//fill array with client objects
        listPopulated = true;
    }
    //setup asset manager
    AssetManager am = getApplicationContext().getAssets();

    //Create the "GridLayout Image Board"
    GridLayout buttonBoard = (GridLayout) findViewById(R.id.buttonboard);
    int idealWidth = buttonBoard.getWidth(); //get width of the board
    int idealHeight = buttonBoard.getHeight() / 2;//same

    //create the Listeners, this is a place holder for now but will eventually use SetCurrentClient() (or maybe just switch to Start screen, with the current client?)
    View.OnClickListener imageClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            System.out.println("CLICK AT: " + v.getId());
            Client temp = clientList[v.getId()];

            Intent i = new Intent(getApplicationContext(), DisplayClient.class);
            System.out.println(temp.getName());
            i.putExtra("name", temp.getName());
            System.out.println(i.getStringExtra("name"));
            i.putExtra("age", Integer.toString(temp.getAge()));
            startActivity(i);

        }
    };
    int j = 0; //used the keep track of the id's we set for the buttons
    for (int i = 0; i < clientList.length; i++) {
        if (clientList[i] != null) {
            //creation and ID setting
            ImageButton imgbutton = (ImageButton) new ImageButton(this);
            imgbutton.setId(i);

            //Layout shit
            imgbutton.setImageResource(R.mipmap.ic_launcher);
            imgbutton.setMinimumWidth(idealWidth);
            imgbutton.setMinimumHeight(idealHeight);

            imgbutton.setOnClickListener(imageClickListener);


            //check and set image
            if(clientList[i].getClientImage().equals(" ")) {
                try{
                    imgbutton.set(am.openFd(clientList[i].getClientImage()));}
                    catch(Exception ex){
                         ex.toString();
               }
                Log.d("ClientImageCheck", "No picture found for " + clientList[i].getName());
            }
            buttonBoard.addView(imgbutton);
            j++;
        }


    }
    //create the new Client Button at the end of all the rest.
    Button newClientButton = (Button) new Button(this);
    newClientButton.setText("+");  // obvious
    newClientButton.setLayoutParams(new LinearLayout.LayoutParams(GridLayout.LayoutParams.WRAP_CONTENT, GridLayout.LayoutParams.WRAP_CONTENT));
    newClientButton.setWidth(idealWidth);
    newClientButton.setHeight(idealHeight);
    View.OnClickListener newClientListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(getApplicationContext(), CreateClientForm.class);
            startActivityForResult(i, 199);
            //System.out.println("Doing good so far, leaving the createclient form bnut still in main");

        }
    }; // create listener
    newClientButton.setOnClickListener(newClientListener); // assign listener
    buttonBoard.addView(newClientButton); //add the button the buttonBoard, after all the clients have been added
}
public void fillActivity_main(){
如果(listPopulated==false){//检查其区域是否已创建
populateClientList();//用客户端对象填充数组
listPopulated=true;
}
//设置资产管理器
AssetManager am=getApplicationContext().getAssets();
//创建“GridLayout图像板”
GridLayout按钮板=(GridLayout)findViewById(R.id.buttonBoard);
int idealWidth=buttonBoard.getWidth();//获取板的宽度
int idealHeight=buttonBoard.getHeight()/2;//相同
//创建监听器,这是一个占位符,但最终将使用SetCurrentClient()(或者使用当前客户端切换到开始屏幕?)
View.OnClickListener imageClickListener=新建视图。OnClickListener(){
@凌驾
公共void onClick(视图v){
System.out.println(“单击:”+v.getId());
Client temp=clientList[v.getId()];
Intent i=新的Intent(getApplicationContext(),DisplayClient.class);
System.out.println(temp.getName());
i、 putExtra(“name”,temp.getName());
System.out.println(即getStringExtra(“名称”));
i、 putExtra(“age”,Integer.toString(temp.getAge());
星触觉(i);
}
};
int j=0;//用于跟踪我们为按钮设置的id
for(int i=0;i
执行删除操作的函数:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    //Check which request we're responding to
    if (requestCode == 199) {
        // Make sure request was successful
        if (resultCode == RESULT_OK) { 
            // The user made a name and crap.
            Bundle extras = data.getExtras();
            String name = extras.getString("name");
            int age = extras.getInt("age");



            Client temp = new Client(name, age);
            addClientToArray(temp);
            System.out.println(name + "attempted add to array");

        }
    for(int i = 0; i<clientList.length; i++ ){
        View v = findViewById(i);
        ((ViewManager) v.getParent()).removeView(v);
    }
    fillActivityMain();
}
activityresult上受保护的void(int-requestCode、int-resultCode、Intent-data){
//检查我们正在响应的请求
if(requestCode==199){
//确保请求成功
如果(resultCode==RESULT_OK){
//用户说出了一个名字和废话。
Bundle extras=data.getExtras();
字符串名称=extras.getString(“名称”);
int age=额外的GEINT(“年龄”);
客户临时工=新客户(姓名、年龄);
addClientToArray(温度);
System.out.println(name+“试图添加到数组”);
}

对于(int i=0;i是否可以在删除现有图像的同时添加替换图像?如果是,请尝试以下操作:

for(int i = 0; i < buttonBoard.getChildCount(); i++) {
ImageButton tempButton = (ImageButton) buttonBoard.getChildAt(i);
tempButton.setVisibility(View.INVISIBLE);
buttonBoard.addView(yourImageButtonHere, i); //adds a new ImageButton in the same cell you are removing the old button from
buttonBoard.removeView(tempButton);
}
for(int i=0;i

这种方法还可以防止GridLayout重新排列子视图的位置。我认为,如果删除子视图,默认行为是GridLayout将重新排列子视图,以便在网格的开头没有空单元格。我希望这是有意义的。

这种方法有很多错误

主要来说,您不必手动创建
图像按钮
,然后将它们添加到
网格布局
。这就是诸如或
回收视图
之类的回收视图的用途。事实上,您应该使用这些按钮来避免布局中有太多图像

B
for(int i = 0; i < buttonBoard.getChildCount(); i++) {
ImageButton tempButton = (ImageButton) buttonBoard.getChildAt(i);
tempButton.setVisibility(View.INVISIBLE);
buttonBoard.addView(yourImageButtonHere, i); //adds a new ImageButton in the same cell you are removing the old button from
buttonBoard.removeView(tempButton);
}