I am New to Android. My Requirement is to check an item of list view and i want show the 开发者_如何学运维 selected items in next activity.
can anyone help me..
Thanks in advance
You have to create 3 things:
A XML layout that represents each row of your ListView, let's call it row.xml. It should be placed in your /res/layout folder so you can access it in Java as "R.layout.row":
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/myRow"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<CheckBox
android:id="@+id/myCheckbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"/>
<TextView
android:text="Hello"
android:id="@+id/myText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
The ListActivity (or regular Activity) in which you want to display the ListView:
public class MyActivity extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_activity_layout);
ArrayList<MyObjectType> data = new ArrayList<MyObjectType>();
// Populate your data list here
MyCustomAdapter adapter = new MyCustomAdapter(this, data);
setListAdapter(adapter);
}
Then, you need to design the Custom Adapter that describes how to display the objects of type MyObjectType:
public class MyAdapter extends BaseAdapter{
private LayoutInflater inflater;
private ArrayList<MyObjectType> data;
public EventAdapter(Context context, ArrayList<MyObjectType> data){
// Caches the LayoutInflater for quicker use
this.inflater = LayoutInflater.from(context);
// Sets the events data
this.data= data;
}
public int getCount() {
return this.data.size();
}
public URL getItem(int position) throws IndexOutOfBoundsException{
return this.data.get(position);
}
public long getItemId(int position) throws IndexOutOfBoundsException{
if(position < getCount() && position >= 0 ){
return position;
}
}
public int getViewTypeCount(){
return 1;
}
public View getView(int position, View convertView, ViewGroup parent){
MyObjectType myObj = getItem(position);
if(convertView == null){ // If the View is not cached
// Inflates the Common View from XML file
convertView = this.inflater.inflate(R.layout.row, null);
}
((TextView)convertView.findViewById(R.id.myText)).setText(myObj.getTextToDisplay());
return convertView;
}
}
This should get you started, comment if you need more explanation.
精彩评论