저와 비슷한 경우로 고민한 글이 있어서 복사해왔는데요...
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="debug.d3bugg0r"
android:versionCode="1"
android:versionName="1.0.0">
<application android:icon="@drawable/icon" android:label="@string/
app_name">
<activity android:name=".D3bugg0r"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category
android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
res/layout/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">
<ListView
android:id="@+id/listView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
src/debug/d3bugg0r/D3bugg0r.java:
package debug.d3bugg0r;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.HeaderViewListAdapter;
import android.widget.ListView;
public class D3bugg0r extends Activity implements View.OnClickListener
{
private ListView listView;
private Button addButton;
private Button removeButton;
private List<String> strings;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
listView = (ListView) findViewById(R.id.listView);
addButton = new Button(this);
addButton.setText("ADD");
addButton.setOnClickListener(this);
removeButton = new Button(this);
removeButton.setText("REMOVE");
removeButton.setOnClickListener(this);
listView.addFooterView(addButton);
listView.addFooterView(removeButton);
strings = new ArrayList<String>();
strings.add("First");
strings.add("Second");
strings.add("Third");
listView.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, strings));
}
@SuppressWarnings("unchecked")
@Override
public void onClick(View view)
{
if(view == addButton)
{
strings.add("another one");
((ArrayAdapter<String>) ((HeaderViewListAdapter) listView.getAdapter
()).getWrappedAdapter()).notifyDataSetChanged();
}
else if(view == removeButton)
{
if(strings.size() > 0)
{
strings.remove(strings.size() - 1);
((ArrayAdapter<String>) ((HeaderViewListAdapter)
listView.getAdapter()).getWrappedAdapter()).notifyDataSetChanged();
}
}
}
xml에서 버튼에 대한 레이아웃의 문제인가요??(보니깐 가로 세로 크기는 안 정해줬던데...)
저도 위에대로 쳐 봤는데 Nullpointexception이 발생하던데...도와주세요 ㅠ