すでに作成されたリストビュー(以下のコード)に対してgetView()を実装するにはどうすればよいですか(getview()を作成するにはどうすればよいですか)。textVIewアイテムのフォントを変更し、さらに属性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"
/>
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;
}