ListView からボタンをクリックすると、いくつかのアクションを実行しようとしています。OnClickListener は、ボタンのクリック時に呼び出されません。
ここに私の MainActivity.java があります
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ListView myListView = (ListView) findViewById(R.id.listView);
final Button myButton = (Button) findViewById(R.id.button);
//create the adapter
final rowNumAdapter rownumadapter = new rowNumAdapter(this);
myListView.setAdapter(rownumadapter);
myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
if(position==0)
{
Toast.makeText(MainActivity.this,"You just clicked"+ rownumadapter.getItem(position) , Toast.LENGTH_LONG).show();
}
}
});
myButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
rownumadapter.addrows();
}
});
}
}
これが私のrowNumAdapter.javaです
public class rowNumAdapter extends BaseAdapter {
private Context mContext;
private int numrows;
public rowNumAdapter(Context context) {
this.mContext=context;
this.numrows= 5;
}
public void addrows()
{
this.numrows+=1;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return this.numrows;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Button view = null;
if(convertView == null)
{
view = new Button(this.mContext);
}
else
{
view = (Button)convertView;
}
view.setText(" row " + position);
return view;
}
}
ここに私の activity_main.xml があります:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="10dp"
android:paddingRight="15dp">
<Button
android:layout_marginTop="15dp"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="Add View"
android:id="@+id/button"></Button>
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="false"
android:clickable="false"
>
</ListView>
</LinearLayout>