2016-05-01 8 views
0

最初はボタンが1つしか表示されない単純なアプリケーションを作成しようとしています。 私がしたいことは、ユーザーがクリックするたびに、別の(クリック可能な)ボタンが下に表示されることです。 私はこのコードで試してきましたが、動作していないようです(NullPointerExceptionがあります)...インターネットが役に立たない。リストビューでボタンを動的に作成する[アンドロイド]

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#ffc1c1" 
    android:orientation="vertical"> 
    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:paddingLeft="0dp" 
     android:id="@+id/add" 
     android:text="add button" 
     android:paddingStart="@dimen/activity_horizontal_margin"/> 
    <ListView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/listView"> 
    </ListView> 
</LinearLayout> 

...とここに私のJavaコードは次のとおりです: ここに私のXMLコードです

public class ListButtonActivity extends AppCompatActivity { 

    private ListView listView; 
    private Button add; 
    private Button b1, b2, b3; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_chat); 

     add = (Button) findViewById(R.id.add); 
     final Button[] statesList = {b1,b2,b3}; 
     b1 = add; 
     b2 = add; 
     b3 = add; 
     add.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       listView = (ListView) findViewById(R.id.listView); 

       ArrayAdapter<Button> adapter = new ArrayAdapter<Button>(chatActivity.this, 
         android.R.layout.simple_list_item_1, android.R.id.text1, statesList); 
       listView.setAdapter(adapter); 
      } 
     }); 

    } 

} 

答えて

0

U'reが少なすぎるためにあまりにもムーチョ作ります。 レイアウトからリストビューを削除するだけです。代わりに別のボタンを追加しますが、その表示をfalseに変更します。 そして、ユーザーがボタンをクリックすると、真の

レイアウトへの可視性を変更します。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
tools:context="com.example.charlie.testandroidstudioinmac.MainActivity"> 

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="My Button1" 
    android:onClick="btn1Clicked" 
    android:id="@+id/button" /> 

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="My second Button" 
    android:visibility="invisible" 
    android:id="@+id/button2" /> 
</LinearLayout> 

そして、あなたの活動:

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
} 

public void btn1Clicked(View view){ 
    Button btn2 = (Button) findViewById(R.id.button2); 
    btn2.setVisibility(View.VISIBLE); 
} 

しかし、あなたは、ListViewコントロールを使用する必要がある場合、あなたが必要とTU

の代わりにCustomAdapterを使用
android.R.layout.simple_list_item_1 //this is predefined for textonly listviews 
+0

これはまさに私がやろうとしていたことではありません...私は、ユーザーが望むだけ多くのボタンを追加したいと思っています。私は彼が求めるボタンの数を見分けることができず、私はjavacodeに1000個のボタンを作成したくない。あなたはそこに解決策があると思いますか? – mwstudingi

0

0に新しい項目を追加する必要があります。その後、新しい項目を追加したアダプターを表示する必要があります。

アダプターにnotifyDataSetChanged()というメソッドを使用すると、新しいアイテムが追加されます。

+0

開発していただけますか?私はstatesListにボタンを追加してappendメソッドを追加し、 "adapter.notifyDataSetChanged();"を書きました。アダプタの定義後にエミュレータからnullPointerExceptionが返されます。それは、nullのオブジェクト参照でtoStringを呼び出そうとしていると言います – mwstudingi

0

リストビューはまだ必要ありませんが、ListViewは新しいユーザーにとっては少し複雑です。

そして、あなたのMainActivity.java上

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

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Create New Button" 
    android:onClick="createBtnClicked" 
    android:id="@+id/button2" /> 

<ScrollView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/scrollView" > 

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

    </LinearLayout> 
</ScrollView> 

があり
public class MainActivity extends AppCompatActivity { 
LinearLayout ll; 
int cont=0; 

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

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

} 

public void createBtnClicked(View v){ 
    Button btn = new Button(this); 
    btn.setText("tbn "+cont++); 
    ll.addView(btn); 
} 

..あなたは、Androidのメモリがサポートできる限り多くのボタンを追加することができます は、ちょうどこのレイアウトを追加します。

関連する問題