我有一个简单的布局只有
1 SeekBar“seekBar”
和
1 ListView“listView”
如何使用SeekBar的“onProgressChanged”方法更新ListView?
应该使用什么代码?
package ahmed_smae.timestables;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SeekBar;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
会员变量......
SeekBar seekBar;
ListView listView;
ArrayList<Integer> numbersArrayList = new ArrayList<Integer>();
onCreate方法
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
构建列表
for(int i = 1; i <= 100; i++){
numbersArrayList.add(i);
}
激活SeekBar和ListView
seekBar = (SeekBar) findViewById(R.id.seekBar);
listView = (ListView) findViewById(R.id.numbersListView);
链接适配器
final ArrayAdapter<Integer> numberListAdapter = new ArrayAdapter<Integer>(MainActivity.this, android.R.layout.simple_list_item_1, numbersArrayList);
listView.setAdapter(numberListAdapter);
seekBar.setMin(numberListAdapter.getItem(0));
seekBar.setMax(numberListAdapter.getCount() + 1);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
Log.i("TimesTable", "" + i);
设置代码以使用SeekBar Progress Change
更新ListView“listView”项目什么代码在这里?
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
}
答案 0 :(得分:0)
您可以使用。
轻松刷新适配器numberListAdapter.notifydatasetchanged();
答案 1 :(得分:0)
只需将numberArraysList放在OnSeekBarChangeListener的onProgressChanged方法内。这样...
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
ArrayList<Integer> numbersArrayList = new ArrayList<Integer>();
Log.i("TimesTable", "" + i);