// class DynamicListItems
import java.util.ArrayList;// layout xml
import java.util.HashMap;
import android.app.ListActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.SimpleAdapter;
public class DynamicListItems extends ListActivity {
private static final String ITEM_KEY = "key";
ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
private SimpleAdapter adapter;
private EditText newValue;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dynamic_list);
newValue = (EditText) findViewById(R.id.new_value_field);
setListAdapter(new SimpleAdapter(this, list, R.layout.row, new String[] { ITEM_KEY }, new int[] { R.id.list_value }));
((ImageButton) findViewById(R.id.button)).setOnClickListener(getBtnClickListener());
}
private OnClickListener getBtnClickListener() {
return new OnClickListener() {
public void onClick(View view) {
try {
HashMap<String, String> item = new HashMap<String, String>();
item.put(ITEM_KEY, newValue.getText().toString());
list.add(item);
adapter.notifyDataSetChanged();
} catch (NullPointerException e) {
Log.i("[Dynamic Items]", "Tried to add null value");
}
}
};
}
}
// dynamic_list.xml
<?xml version="1.0" encoding="utf-8"?>// row.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/android:list"
android:layout_width="fill_parent"
android:layout_height="0px"
android:layout_weight="1"
android:cacheColorHint="#00000000" >
</ListView>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8px"
android:layout_marginLeft="8px"
android:layout_marginRight="8px"
android:layout_marginTop="6px"
android:background="#00000000"
android:orientation="horizontal" >
<ImageButton
android:id="@+id/button"
android:layout_width="100px"
android:layout_height="100px"
android:src="@android:drawable/ic_menu_add" />
</LinearLayout>
<LinearLayout
android:layout_width="230px"
android:layout_height="wrap_content"
android:layout_marginBottom="8px"
android:layout_marginLeft="0px"
android:layout_marginRight="28px"
android:layout_marginTop="8px"
android:background="#cccccc00"
android:orientation="vertical" >
<EditText
android:id="@+id/new_value_field"
android:layout_width="210px"
android:layout_height="wrap_content"
android:layout_marginBottom="8px"
android:layout_marginLeft="12px"
android:layout_marginRight="12px"
android:layout_marginTop="6px"
android:layout_weight="1"
android:textColor="#020905"
android:textSize="18sp" />
</LinearLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>sumber @ novoda
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#00000000">
<TextView
android:id="@+id/list_value"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
0 Response to " [source code] DynamicListItems "
Posting Komentar