【发布时间】:2011-05-16 08:32:48
【问题描述】:
我刚刚在 Fedor 的 LazyLoading ListView 中实现了 GSON。这意味着应用程序通过 ImageLoader 类将下载的图像和文本从 Web 保存到外部存储中。
我想知道为什么如何在没有互联网连接的情况下访问此列表视图。 这里我给你一个我的 ListView 类的 sn-p:
public class ProjectsList extends Activity {
ListView lstTest;
ProjectAdapter arrayAdapter;
ArrayList<Project> prjcts=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.projects_list);
//Initialize ListView
lstTest= (ListView)findViewById(R.id.lstText);
prjcts = new ArrayList<Project>();
arrayAdapter = new ProjectAdapter(ProjectsList.this, R.layout.listitems,prjcts,ProjectsList.this);
lstTest.setAdapter(arrayAdapter);
if (isOnline())
{
WebService webService = new WebService("http://liebenwald.spendino.net/admanager/dev/android/projects.json");
Map<String, String> params = new HashMap<String, String>();
params.put("var", "");
String response = webService.webGet("", params);
try
{
Type collectionType = new TypeToken<ArrayList<Project>>(){}.getType();
List<Project> lst= new Gson().fromJson(response, collectionType);
for(Project l : lst)
{
prjcts.add(l);
ConstantData.projectsList.add(l);
}
arrayAdapter.notifyDataSetChanged();
}
catch(Exception e)
{
Log.d("Error: ", e.getMessage());
}
}
lstTest.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent care = new Intent(ProjectsList.this, ProjectDetail.class);
care.putExtra("spendino.de.ProjectDetail.position",position);
startActivity(care);
}
});
}
@Override
public void onDestroy()
{
yAdapter.imageLoader.stopThread();
lstTest.setAdapter(null);
super.onDestroy();
}
protected boolean isOnline() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnected()) {
return true;
} else {
}
});
return false;
}
}
}
如果需要更多我的代码,请告知。 谢谢
【问题讨论】:
-
在这里写你的代码
catch(Exception e) { //Your Code,,,,,,, } -
我应该在哪里写?在 onCreate 里面?
-
是的,在
OnCreate..当你没有从你的网络服务器得到任何响应时..做你想做的事.. -
我知道,这就是我实现 isOnline 方法的原因,但问题是我希望 List 显示缓存的图像和文本。使用我现在拥有的代码,当没有检测到互联网时,列表视图只会显示一个黑色的黑屏。
-
您是否能够从 sqlite 数据库中检索数据并将其转换为
Project类?
标签: android lazy-loading android-listview gson