0

How do I implement getView() (how do i create getview() )for an already created listview (code below). so that I can change font of the textVIew items and further attribute stylig

MainActivity.java

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ListView listView1 = (ListView) findViewById(R.id.listView1);

String[] items = { "some", "fancy", "items", "to", "show" };

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            R.layout.listitem, items);

 listView1.setAdapter(adapter);
 }

activity_main.xml

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
 tools:context=".MainActivity" >

<ListView 
  android:id="@+id/listView1" 
  android:layout_height="fill_parent"
  android:layout_width="fill_parent" />

 </RelativeLayout>

listview.xml

<?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="wrap_content" 
android:textSize="20sp"
android:padding="22dp"
android:gravity="center_horizontal"
/>

changed MainActivity.java

  public class MainActivity extends Activity {
   Context context;
    ArrayAdapter<String> adapter;
     String[]  items = { "some", "fance", "rock", "shot", "trance" };
   @Override
   protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ListView listView1 = (ListView) findViewById(R.id.listView1);




    adapter = new ArrayAdapter<String>(this,
                R.layout.genre_list, items);

    listView1.setAdapter(adapter);}
    public View getView(int position, View convertView, ViewGroup parent) {

    View row;

    if (null == convertView) {
         LayoutInflater mInflater = (LayoutInflater) context
                 .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    row = mInflater.inflate(R.layout.genre_list, null);

    } else {

    row = convertView;

    }

    TextView tv = (TextView)row.findViewById(android.R.id.title);
    Typeface font = Typeface.createFromAsset(getAssets(),"roboto.ttf");  
    Toast.makeText(getApplicationContext(), "font fetched!",  
         Toast.L  ENGTH_LONG).show();
    tv.setText(items[position]);
    tv.setTypeface(font);
    return row;

    }
user1979237
  • 11
  • 2
  • 7

2 Answers2

0

getView() is a method in Adapter classes, like ArrayAdapter and SimpleCursorAdapter. You must extend an Adapter class, in your case ArrayAdapter, to access getView().

This Google Talk by Romain Guy, an Android lead programmer, discusses the best practices to extend an Adapter in great detail.


That said you can change the font and many other style attributes through XML, you don't necessarily need to create a custom Adapter.

Community
  • 1
  • 1
Sam
  • 86,580
  • 20
  • 181
  • 179
  • check changed MainActivity.java in my edit this does not work I don't think getView is being called – user1979237 Jan 16 '13 at 18:57
  • No, it is not being called. You **must** write a custom Adapter to use `getView()`. Please watch the video above, it describes how to do what you want and there is no reason for me to repeat its contents here. The video also demonstrates that the method your are trying to use can be improved with ViewHolders among other things. – Sam Jan 16 '13 at 19:05
0

for Changing font for TextView you will need to create Custom ArrayAdapter. but you can do this by just overriding default getView method as :

Typeface font = Typeface.createFromAsset(getAssets(),
                                 "yourfontname.ttf");

listView1.setListAdapter(new 
              ArrayAdapter<string>(this,R.layout.listitem, items) {

@Override

public View getView(int position, View convertView, 
                                       ViewGroup parent) {

View row;

if (null == convertView) {

row = mInflater.inflate(R.layout.listitem, null);

} else {

row = convertView;

}

TextView tv = (TextView)row.findViewById(android.R.id.text1);

tv.setText(items[position]);
tv.setTypeface(font);
return row;

}

});
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213