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