ボタンを押すと、Webページからデータを取得し、レイアウトに従って表示することになっています。これは問題なく実行されますが、クリックして戻った場合にのみ、メインのアクティビティ(ホームアクティビティ)に戻り、ボタンをもう一度押します。
初めてボタンを押したときにロードするのに永遠に時間がかかり、時々動作しません。しかし、2回目はインスタントロードです[正常に動作します]。
コードは次のとおりです-3つのクラス-HomeActivity.java、News.Java、およびMyNewsHandler.java
public class HomeActivity extends Activity
{
String username = " ";
Button alog;
Button news;
Button forumTime;
EditText usernameEntry;
SharedPreferences preferences;
ImageView image;
TextView usernametv;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
forumTime = (Button)findViewById(R.id.timeButton);
news = (Button) findViewById(R.id.newsButton); // <--- Heres the problem
alog = (Button) findViewById(R.id.alogButton);
usernameEntry = (EditText) findViewById(R.id.username);
//For news button
OnClickListener newsListener = new OnClickListener()
{
public void onClick(View v)
{
Intent intent = new Intent(HomeActivity.this, News.class);
startActivity(intent);
}
};
//Attaching listeners
//forumTime.setOnClickListener(timeListener);
//alog.setOnClickListener(alogListener);
news.setOnClickListener(newsListener);
}
public void display(String string)
{
Toast.makeText(getBaseContext(), string, 300000).show();
}
}
public class MyNewsHandler extends DefaultHandler
{
private Boolean inTitle = false;
private Boolean inDescription = false;
private Boolean inItem = false;
private Boolean inCategory = false;
static public ArrayList<String> title = new ArrayList<String>(),
description = new ArrayList<String>(), category = new ArrayList<String>();
private StringBuilder buff = null;
// All methods auto called in this order - start, characters, end
/*
* Called when an xml tag starts
* imgView.setImageResource(R.drawable.newImage);
*/
@Override
public void startElement(String uri, String localName, String qName,Attributes attributes)
{
if (inItem)
{
if (localName.equals("title"))
{
inTitle = true;
buff = new StringBuilder();
}
if (localName.equals("description"))
{
inDescription = true;
buff = new StringBuilder();
}
if (localName.equals("category"))
{
inCategory = true;
buff = new StringBuilder();
}
}
if (localName.equals("item"))
{
inItem = true;
}
}
/*
* Called when an xml tag ends
*/
@Override
public void endElement(String uri, String localName, String qName)throws SAXException
{
if (!inTitle && !inDescription && !inCategory)
{
inItem = false;
}
else
{
if (inTitle)
{
String check = buff.toString().trim();
title.add(check);
inTitle = false;
buff = null;
}
if (inDescription)
{
String check2 = buff.toString().trim();
String array [];
int index = 0;
if (check2.contains("/>"))
{
array = check2.split("/>");
index = check2.indexOf("10;");
description.add(array[array.length - 1].trim());
}
else description.add(check2);
//description.add(array[1]);
inDescription = false;
buff = null;
}
if(inCategory)
{
String check = buff.toString().trim();
category.add(check);
inCategory = false;
buff = null;
}
}
}
/*
* Called to get tag characters
*/
@Override
public void characters(char ch[], int start, int length)
{
if (buff != null)
{
for (int i = start; i < start + length; i++)
{
buff.append(ch[i]);
}
}
}
}
public class News extends ListActivity
{
String url;
TextView text1;
TextView text2;
static ImageView img;
// newsList contains the list items
static ArrayList<HashMap<String, Object>> newsList = new ArrayList<HashMap<String, Object>>();
// to store drawable id's
static HashMap<String, Integer> images = new HashMap<String, Integer>();
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//Load xml containing image view and a linear layout with 2 textviews
setContentView(R.layout.aloglistview);
img = (ImageView)findViewById(R.id.imageView2);
text1 = (TextView) findViewById(R.id.text1);
text2 = (TextView) findViewById(R.id.text2);
// url we are retrieving from
url = "http://services.runescape.com/m=news/latest_news.rss";
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(url);
String result = ""; // result of http Post
try
{
// Execute HTTP get Request
HttpResponse response = httpclient.execute(httpget);
// Extract the page content returned from the response
BufferedReader in = new BufferedReader(new InputStreamReader(
response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String line = "";
String NL = System.getProperty("line.separator");
while ((line = in.readLine()) != null)
{
sb.append(line + NL);
}
in.close();
result = sb.toString(); // resulting content of the page
} catch (ClientProtocolException e)
{
Log.d("ERROR", "ClientProtocolException=" + e);
System.err.println("ClientProtocolException=" + e);
} catch (IOException e)
{
Log.d("ERROR", "IOException=" + e);
System.err.println("IOException=" + e);
}
try
{
//Parse the resulting page
Xml.parse(result, new MyNewsHandler());
} catch (SAXException e)
{
e.printStackTrace();
} catch (Exception e)
{
e.printStackTrace();
}
//needs to match formulate list thing
SimpleAdapter adapter = new SimpleAdapter(this, newsList,
R.layout.newsitem, new String[] { "Title", "Description", "Image"},
new int[] { R.id.textnews1, R.id.textnews2, R.id.imageView2});
populateList(); //Add all items to the newslist
setListAdapter(adapter);
}
public void populateList()
{
//Method to add to images hashmap
initMap();
//reset each time the list is populated
newsList = new ArrayList<HashMap<String, Object>>();
//Display 10 items
for (int i = 0; i < 11; i++)
{
HashMap<String, Object> temp = new HashMap<String, Object>();
String title = MyNewsHandler.title.get(i);
String category = MyNewsHandler.category.get(i);
//default drawable
int drawable = R.drawable.item;
//Match title text to hash keys to set correct image
for (Map.Entry<String, Integer> entry : images.entrySet())
{
String key = entry.getKey();
Object value = entry.getValue();
if(category.contains(entry.getKey()))
{
drawable = entry.getValue();
break;
}
else drawable = R.drawable.item;
}
temp.put("Title", title);
temp.put("Description", " " + MyNewsHandler.description.get(i));
temp.put("Image",drawable);
newsList.add(temp);
}
}
public void display(String string)
{
Toast.makeText(getBaseContext(), string, 1000202).show();
}
public void initMap()
{
images.put("Behind the Scenes News",R.drawable.behindthescenes);
images.put("Customer Support News",R.drawable.customerservice);
images.put("Game Update News",R.drawable.gameupdate);
images.put("Website News",R.drawable.devblog);
images.put("Technical News",R.drawable.technicalnews);
}
}
申し訳ありませんが、コードが長すぎます。問題は動作することです(ha ..)が、最初に必要なときにすぐに読み込まれません。
これを行います:アプリを起動>ニュースボタンを押す>「ロードしない、空白の画面」
しかし、これを行うと機能します:アプリを起動>ニュースボタンを押す>戻るを押す>ニュースボタンを押す>「インスタントロード」
ボタンを押す前に事前に解析した提案に従い、画像を完全に削除する別の試みも行いました(これは遅延の可能性があると考えています)。変わりはない。それでも同じ動作。
どんな提案でも大歓迎です!