Tuesday, June 26, 2012

A while ago in the study how to customize the ListView 
Now share for everyone 
Imagine a ListView may be so long 



If you use the built-the ListView SampleAdapter at most, you become so 

title mouth
context
title mouth
context
title mouth
context
title mouth
context
title mouth
context




If we want to plug plug Button or plug you want to put things how to do? 

Very simple to use BaseAdapter can easily be done 

First, we look at what is BaseAdapter 

BaseAdapter is a super useful categories it allows you to define many kinds of View 

For example, Spinner, ListView, GridView 

We want to how to define an own ListView? 

First, we open a new Project called MyListView 

To let MyListView inherit the ListActivity (Note! Not the Activity!) 

Add a new category called MyAdapter and inheritance BaseAdapter 

You will find the need to override the four methods, namely, 





int getCount() Object getItem(int position) long getItemId(int position) View getView(int position, View view, ViewGroup parent) 


If you are using eclipse should automatically generate these four methods 
These four methods to explain a little 
We know that the ListView is composed of a one 
Each column we can see it as a View 
Combined is a whole ListView 
GetCount () is made ​​in the end the number of column method 
If we are to make a column is to use the getItem () method 
If you want to obtain the id of a column the use getItemId () this method 
Then one of our most important methods to make changes to View the contents of a column 
Is to use getView () this method 

First, change into how good you want to ListView? 
Suppose we want a Button of the first plug and then plug a picture last plus the text description 
May be president like this 
button in Figure text
button in Figure text
button in Figure text
button in Figure text
button in Figure text


Then we started! 
First in the establishment of a list.xml inside plus a 
This is the point into the first one to see the screen 
Again is increase of adapter.xml a 
Which is put in each column of the ListView will lay down according to what we want 
My Layout is set for RelativeLayout thus associated property 


 <Button android:id="@+id/AdapterButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5px"> <!-- 位置要調整好放在左邊--> </Button> <ImageView android:id="@+id/AdapterImage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@+id/AdapterButton"<!-- 設定在Button的右邊--> android:layout_marginLeft="25px"> </ImageView> <TextView android:id="@+id/AdapterText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@+id/AdapterImage" <!-- 設定在ImageView的右邊--> android:layout_marginLeft="45px"> </TextView> 


Then we MyListView which specify the main.xml for the beginning of the screen 
setContentView (R.layout.list); 
Then to set MyAdapter for the ListView Adapter 
setListAdapter (new MyAdapter (this)); 
Then change to MyAdapter.java 
We started several variables will be defined 
private LayoutInflater adapterLayoutInflater; 
And then declared in the constructor which reads MyListView Context 
adapterLayoutInflater = LayoutInflater.from (c); 
Example because it is so we simply use the three good 
We will therefore return is set to 3 
public int getCount () { 
/ / TODO Auto-generated method stub 
return 3; 
} 
Again to obtain the view of the order 
First Button again ImageView then TextView 
Therefore, we declare as a category to read the column setTag property 


 public class TagView{ Button button; ImageView image; TextView text; public TagView(Button button,ImageView image, TextView text){ this.button = button; this.image = image; this.text = text; } } 


Then we read it in getView method which 
 TagView tag; tag = new TagView( (Button)view.findViewById(R.id.AdapterButton), (ImageView)view.findViewById(R.id.AdapterImage), (TextView)view.findViewById(R.id.AdapterText)); view.setTag(tag); 


Again is to set the map or the text of the three components above 
 tag.image.setBackgroundResource(R.drawable.icon); tag.button.setText("button"+position); tag.text.setText("text"+position); 


This all set the column view return the 
 return view; 


As a result, we made ​​the ListView and you're done 
If you go according to the above procedure should be able to see such a picture 




If you did not succeed does not matter to download this file slowly reference should not be difficult to 
http://uploadingit.com/file/me7o3idnetl93ves/BaseAdapterChangeListView.zip 
Which side there something wrong please do correct me avoid misleading you Thank you 

PS. Random change 
In a random order set 






PS2. Was reflected in the the ListView point it did not respond to it is inserted into the relationship between the Button 
Focus the properties Button snatched away so you as long as the xml file inside the properties of the Button to add this line 
android: focusable = "false" 
You will find can click on ListView 
Refer to the website 
http://iamshiao.blogspot.com/2010/12/androidbaseadapterlistview.html 
http://www.iteye.com/topic/540423 
http://developer.android.com/reference/android/widget/ListView.html 
http://developer.android.com/reference/android/widget/BaseAdapter.html 
http://disanji.net/2010/11/25/android-baseadapter-spinner-listview-gridview/

Leave a Reply

Subscribe to Posts | Subscribe to Comments

- Copyright © .Hacking Cracking Tricks And Tutorials, Paid Scripts, Latest Exploits, 0Day Vulnerability, - Skyblue - Powered by Blogger - Designed by Johanes Djogan -