2016-12-22 11 views
1

Shy Wardの貴重な助けを得て、レイアウトにビューを追加することができました。今私は、2番目の削除ボタン( "quitar")が押されたときにそれらを削除しようとしています。 私は、次のコードを試してみたが、それは私のために動作しません:ボタンを押したときにリストビューに追加された最後のビューを削除する

public void quitar(View v) { 
    int id = 0; 
    if(!viewList.isEmpty()) { 
     id = viewList.get(viewList.size()-1).getId(); 
     viewList.remove(id); 
    } 
} 

その後、私はリストからビューを削除する前に、多分私は「物理的にそれを削除する」べきだと思いました。私は良い結果なしにこれを試してみました:私は、私はArrayAdapterを使用する必要があるかもしれないが、私はそれを行う方法を知らないことを感知何を読んでから、

public void quitar(View v) { 
    int id = 0; 
    RelativeLayout panelJuego = (RelativeLayout) findViewById(R.id.panelJuego); 
    Circulo circulo = new Circulo(this, 30, 30, "#000000"); 
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(100, 100); 
    if(!viewList.isEmpty()) { 
     id = viewList.get(viewList.size()-1).getId(); 
     params.addRule(id); 
     panelJuego.addView(circulo, params); 
     viewList.remove(id); 
    } 
}                    

。 これはArrayAdapterを使って私の試みになりますが、それはまた、正常に動作しません:

public class MainActivity extends Activity { 
    ArrayList<Circulo> viewList = new ArrayList<Circulo>(); 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
    } 

    public void poner(View v) { 
     ArrayAdapter<Circulo> adapter = new ArrayAdapter(this, R.id.panelJuego, viewList); 
     RelativeLayout panelJuego = (RelativeLayout) findViewById(R.id.panelJuego); 
     Circulo circulo = new Circulo(this, 30, 30, "#FF0000"); 
     circulo.setId(View.generateViewId()); 
     RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(100, 100); 
     if(!viewList.isEmpty()) { 
      int id = viewList.get(viewList.size()-1).getId(); 
      params.addRule(RelativeLayout.RIGHT_OF, id); 
     } 
     panelJuego.addView(circulo, params); 
     viewList.add(circulo); 
     adapter.add(circulo); 
     adapter.notifyDataSetChanged(); 
    } 

    public void quitar(View v) { 
     int id = 0; 
     ArrayAdapter<Circulo> adapter = new ArrayAdapter(this, R.id.panelJuego, viewList); 
     if(!viewList.isEmpty()) { 
      adapter.remove(adapter.getItem(viewList.size()-1)); 
      adapter.notifyDataSetChanged();; 
     } 
    } 


    public class Circulo extends View { 
     private int radio = 30; 
     private String color; 
     int Cx, Cy; 

     public Circulo(Context context, int x, int y, String color) { 
      super(context); 
      Cx = x; 
      Cy = y; 
      this.color = color; 
     } 

     @Override 
     protected void onDraw(Canvas canvas) { 
      super.onDraw(canvas); 
      Paint paint = new Paint(); 
      paint.setStyle(Paint.Style.FILL); 
      paint.setColor(Color.parseColor(color)); 
      canvas.drawCircle(Cx, Cy, radio, paint); 
     } 
    } 
} 

メインレイアウト:私はこれは私がしようとしているものをよりよく理解するのに役立つことを願って

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

    <RelativeLayout 
     android:id="@+id/panelJuego" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.76" 
     android:orientation="horizontal" > 

    </RelativeLayout> 
    <Button 
     android:id="@+id/button2" 
     style="?android:attr/buttonStyleSmall" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentLeft="true" 
     android:onClick="quitar" 
     android:text="Quitar" /> 

    <Button 
     android:id="@+id/button1" 
     style="?android:attr/buttonStyleSmall" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_marginLeft="20dp" 
     android:layout_toRightOf="@+id/button2" 
     android:onClick="poner" 
     android:text="Poner" /> 

</RelativeLayout>   

を行う。

+0

あなたがより多くのスニペットを追加するかどうかを理解することをお勧めします。このhttp://stackoverflow.com/questions/39669876/removing-last-item-of-a-listview – Sridhar

+0

を試してみてくださいコードまたはイメージのイラスト: –

+0

リストからビューを削除しても、元のデータソースにはまだその値が含まれているため、ビューは完全に削除されません。したがって、リストから削除した後もデータソースから削除してください –

答えて

0

私は最終的に私がやろうとしていたことを得ることができました、赤い円を追加し、そのために2つのボタンを使って最後のものを削除することができました。そのためのコードはでした:

public void poner(View v) { 
    RelativeLayout panelJuego = (RelativeLayout) findViewById(R.id.panelJuego); 
    Circulo circulo = new Circulo(this, 30, 30, "#FF0000"); 
    circulo.setId(View.generateViewId()); 
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(80, 80); 
    if(!viewList.isEmpty()) { 
     int id = viewList.get(viewList.size()-1).getId(); 
     params.addRule(RelativeLayout.RIGHT_OF, id); 
    } 
    panelJuego.addView(circulo, params); 
    viewList.add(circulo); 
    } 

public void quitar(View v) { 
    int id = 0; 
    if(!viewList.isEmpty()) { 
     id = viewList.size() - 1; 
     viewList.get(id).setVisibility(View.INVISIBLE); 
     viewList.remove(id); 
    } 
} 


public class Circulo extends View { 
    private int radio = 30; 
    private String color; 
    int Cx, Cy; 

    public Circulo(Context context, int x, int y, String color) { 
     super(context); 
     Cx = x; 
     Cy = y; 
     this.color = color; 
    } 

    @Override 
    protected void onDraw(Canvas canvas) { 
     super.onDraw(canvas); 
     Paint paint = new Paint(); 
     paint.setStyle(Paint.Style.FILL); 
     paint.setColor(Color.parseColor(color)); 
     canvas.drawCircle(Cx, Cy, radio, paint); 
    } 
} 
関連する問題