Constructing Dynamic List View In Android
Iam trying to create a list view dynamically, Please provide me an example for list view in android..开发者_StackOverflow社区.
Activity class......
public class UsersListActivity extends ListActivity{
RegisterUser registerUser;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] statesList = getListOfAllUsers();
Toast.makeText(getApplicationContext(),
"States Array lentgh is : " + statesList.length,
Toast.LENGTH_LONG).show();
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item,
statesList));
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(getApplicationContext(),
"You selected : "+((TextView) view).getText(), Toast.LENGTH_SHORT).show();
}
});
}}
and create an xml file named list_item.xml and paste the below code.
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="16sp" android:textColor="#ffffff" android:textStyle="bold" android:background="@drawable/border_cell">
</TextView>
You can create your own List View by extending Base adapter class
public class ListAdapterDroidman extends BaseAdapter{
private ArrayList<ListitemDroidman> list = new ArrayList<ListitemDroidman>();
@SuppressWarnings("unused")
private Context context;
public ListAdapterDroidman(Context context) {
this.context = context;
}
public void addItem(ListitemDroidman item) {
list.add(item);
}
public void addItem(ListitemDroidman item,int pos)
{
list.add(pos, item);
}
public void removeItem(int pos)
{
list.remove(pos);
}
public void clearList()
{
list.clear();
}
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return list.get(position);
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
return list.get(position);
}
}
Then create your own layout
public class ListitemDroidman extends LinearLayout {
TextView filename;
ImageView iv;
public ListitemDroidman(Context context) {
super(context);
}
public void setInfo(Context context, Bitmap icon, String fname,int colorId) {
iv=new ImageView(context);
iv.setImageBitmap(icon);
iv.setPadding(0, 0, 20, 0);
addView(iv);
filename = new TextView(context);
filename.setTextSize(20);
filename.setTextColor(getResources().getColor(colorId));
filename.setText(fname);
addView(filename);
}
}
Now create listitems in your activityClass create object of the adapter that u have created
myListitem = new ListitemDroidman(this);
icon = BitmapFactory.decodeResource(getResources(),
R.drawable.folder_icon);
myListitem.setInfo(getApplicationContext(), icon, filelist[i],
R.color.color_white);
add the listitem to the adaper as in normal list
la_file.addItem(myListitem);
Try this tutorial to understand how to use ListView. http://developer.android.com/resources/tutorials/views/hello-listview.html
Why don't you just take a look at the ApiDemos of your android SDK..
You can find 14 examples of ListActivities implementations..
Or just search here or on Google, you can find lots of examples
Then if you want to achieve something in particular you can ask a more specific question here, does it sound fair?
精彩评论