下面是我的代码我希望每5秒listview将刷新新值我该怎么办请检查我的代码我只是按照这个简单的教程http://www.mkyong.com/android/android-listview-example/现在我想每运行5秒钟更新一次代码应用程序崩溃,因为可能b通知里面可运行的告诉我为什么?
public class ListMobileActivity extends ListActivity {
Handler mHandler;
static int n = 4;
private static String[] MOBILE_OS = new String[n];
MobileArrayAdapter setListAdapter ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MOBILE_OS[0] = "Android1";
MOBILE_OS[1] = "iOS1";
MOBILE_OS[2] = "WindowsMobile1";
MOBILE_OS[3] = "Blackberry1";
this.mHandler = new Handler();
this.mHandler.postDelayed(m_Runnable,5000);
setListAdapter(new MobileArrayAdapter(this, MOBILE_OS));
}
private final Runnable m_Runnable = new Runnable()
{
public void run()
{
MOBILE_OS[0] = "hello1";
MOBILE_OS[1] = "hello2";
MOBILE_OS[2] = "hllo3";
MOBILE_OS[3] = "hello4";
Toast.makeText(ListMobileActivity.this, "in
runnable",Toast.LENGTH_SHORT).show();
setListAdapter.notifyDataSetChanged();
ListMobileActivity.this.mHandler.postDelayed(m_Runnable, 5000);
}
};//runnable
}
private final Context context;
private final String[] values;
public MobileArrayAdapter(Context context, String[] values) {
super(context, R.layout.list_mobile, values);
this.context = context;
this.values = values;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.list_mobile, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.label);
ImageView imageView = (ImageView) rowView.findViewById(R.id.logo);
textView.setText(values[position]);
// Change icon based on name
String s = values[position];
System.out.println(s);
return rowView;
}
}
答案 0 :(得分:0)
使用Timer Class,您可以按照以下步骤每隔5秒更新一次ListView:
public class ListViewRefreshEvery5Seconds extends ListActivity
{
Timer timer;
ArrayList<String> arr1;
ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
arr1 = new ArrayList<String>();
for(int i=1;i<=5;i++)
{
arr1.add("Item "+i);
}
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arr1);
setListAdapter(adapter);
setTimerForAdvertise();
}
void setTimerForAdvertise() {
timer = new Timer();
TimerTask updateProfile = new CustomTimerTask();
timer.scheduleAtFixedRate(updateProfile, 5000, 5000);
}
public class CustomTimerTask extends TimerTask
{
private Handler mHandler = new Handler();
@Override
public void run()
{
new Thread(new Runnable()
{
@Override
public void run()
{
mHandler.post(new Runnable()
{
@Override
public void run()
{
for(int i=1;i<=5;i++)
{
arr1.add("Item "+i);
}
adapter.notifyDataSetChanged();
}
});
}
}).start();
}
}
}
或者如果您只想更新一次ListView,那么您可以像这样简单地使用Handler:
new Handler().postDelayed(new Runnable()
{
@Override
public void run()
{
// TODO Auto-generated method stub
for(int i=1;i<=5;i++)
{
arr1.add("Item "+i);
}
adapter.notifyDataSetChanged();
}
}, 5000);