你需要在每个 setTextSize 之后添加这一行
myAdapter.notifyDataSetChanged();
编辑:
所以代码看起来像这样:
SimpleCursorAdapter myCursorAdapter; // define the adapter above onCreate method
seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
int Blast;
@Override
public void onStopTrackingTouch(SeekBar seekBar){
TextView shopName = (TextView)findViewById(R.id.item_version);
prefs = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor ed = prefs.edit();
ed.putFloat("fontsize", ((TextView) findViewById(R.id.item_version)).getTextSize());
ed.commit();
// myList.setSelection(1);
((TextView) findViewById(R.id.item_version)).setTextSize(TypedValue.COMPLEX_UNIT_PX, seekBar.getProgress());
myCursorAdapter.notifyDataSetChanged();
}
@Override
public void onStartTrackingTouch(SeekBar seekBar){
((TextView) findViewById(R.id.item_version)).setTextSize(TypedValue.COMPLEX_UNIT_PX,seekBar.getProgress());
myCursorAdapter.notifyDataSetChanged();
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser){
((TextView) findViewById(R.id.item_version)).setTextSize(TypedValue.COMPLEX_UNIT_PX,progress);
myCursorAdapter.notifyDataSetChanged();
Blast = progress;
}
});
public void viewAll1(){
Cursor cursor = myDB.getAllData2();
String[] from = new String[]{DBAdapter.KEY_NAME};
int[] to = new int[]{R.id.item_version};
myCursorAdapter = new SimpleCursorAdapter(this, R.layout.item_layout, cursor, from, to, 0);
ListView myList = (ListView) findViewById(R.id.listView);
myList.setAdapter(myCursorAdapter);
}
EDIT2:
您需要在自定义适配器的 getView 中添加 setTextSize,因为 notifyDataSetChange 实际上调用 getView 方法,这是一个工作示例,但当然使用了不同类型的适配器,即基本适配器:
import android.app.Activity;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.SeekBar;
import android.widget.TextView;
public class MainActivity extends Activity {
SeekBar seekbar;
cursorAdapter myCursorAdapter;
String str[]={"Shop Name"};
ListView list;
TextView tv;
int globProg=20; //used default value for textsize 20 you can put anything else
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list=(ListView) findViewById(R.id.listView);
seekbar= (SeekBar) findViewById(R.id.seekBar);
myCursorAdapter= new cursorAdapter(MainActivity.this,str);
list.setAdapter(myCursorAdapter);
seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
int Blast;
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
globProg= seekBar.getProgress(); //saving value in the global variable
myCursorAdapter.notifyDataSetChanged(); //this method calls the getview again so the view is recreated with the new size
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
globProg= seekBar.getProgress();
myCursorAdapter.notifyDataSetChanged();
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
globProg= seekBar.getProgress();
myCursorAdapter.notifyDataSetChanged();
Blast = progress;
}
});
}// end of onCreate
public class cursorAdapter extends BaseAdapter
{
public String list[];
Activity activity;
public cursorAdapter(Activity activity,String list[]) {
super();
this.activity = activity;
this.list = list;
}
@Override
public int getCount() {
return list.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return position;
}
private class ViewHolder {
TextView textView;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
LayoutInflater inflater = activity.getLayoutInflater();
convertView = inflater.inflate(R.layout.item_version, null);
holder = new ViewHolder();
holder.textView = (TextView)convertView.findViewById(R.id.item_version);
holder.textView.setText(list[position]);
holder.textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, globProg); // setTextSize here
convertView.setTag(holder);
return convertView;
}
}
}
EDIT3:
public void viewAll1(){
Cursor cursor = myDB.getAllData2();
String[] from = new String[]{DBAdapter.KEY_NAME};
int[] to = new int[]{R.id.item_version};
int layoutid=R.layout.item_version;
myCursorAdapter= new cursorAdapter(context , layoutid,cursor,from, to);
ListView myList = (ListView) findViewById(R.id.listView);
myList.setAdapter(myCursorAdapter);
}
public class cursorAdapter extends SimpleCursorAdapter {
private Context appContext;
private int layout;
private Cursor mycursor;
public cursorAdapter(Context context, int layout, Cursor c, String[] from,int[] to) {
super(context, layout, c, from, to);
this.appContext=context;
this.layout=layout;
this.mycursor=c;
}
@Override
public int getCount() {
return 1;
}
@Override
public Object getItem(int arg0) {
return null;
}
@Override
public long getItemId(int arg0) {
return 0;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView textView = (TextView) view.findViewById(R.id.item_version);
String title = cursor.getString( cursor.getColumnIndex( DBAdapter.KEY_NAME ) );
textView.setText(title);
textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, globProg);
}
}