2012-02-27 15 views
0

私は画像とテキストをxmlから取得しようとしています...しかし、この進歩がバックグラウンドに起こるように、私はいくつかの配列にデータを渡すことができるスレッドを使用しようとしました。この配列を使ってtextViewを作成します。私が実行すると、私はスレッドでは、プログラムが正常に動作データを取得し、配列に入れて参照してください...しかし、setTextとsetImageBitmapで、私はそれらの配列が空を参照してください!それはどうしたらできますか?スレッドのxmlからデータを取得していますか?

私はbasicly XMLからデータを取得するため、このチュートリアルを以下のよ

http://p-xr.com/android-tutorial-how-to-parseread-xml-data-into-android-listview/

そして、ここでは私のコードです:

mainActivity上
public class mainPage { 

HashMap<Integer, String> desc; 
HashMap<Integer, Bitmap> bitMap; 
ScrollView scroll; 
View app, temp; 
static int j = 0; 
static View first; 
static NodeList nodes; 
static TextView textView, textView1, textView2; 
static ImageView img, image1, image2; 
LinearLayout parent; 
HashMap<Integer, View> others; 
Runnable get; 

public mainPage() { 

    this.app = MainActivity.app; 
    this.scroll = (ScrollView) app.findViewById(R.id.mainPage_scroll); 
    this.parent = new LinearLayout(scroll.getContext()); 

    parent.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 
      LayoutParams.FILL_PARENT)); 
    parent.setOrientation(LinearLayout.VERTICAL); 
    scroll.addView(parent); 

    LayoutInflater inflater = LayoutInflater.from(parent.getContext()); 

    others = new HashMap<Integer, View>(); 

    // add first item to main page 
    first = inflater.inflate(R.layout.main_page_first_item, null); 
    parent.addView(first); 
    others.put(0, first); 

    for (int i = 2; i < 10; i++) { 
     temp = inflater.inflate(R.layout.main_page_item, null); 
     others.put(i, temp); 
     parent.addView(temp); 
    } 

} 

public void setPage() { 

    // take datas from XML 
    String xml = XMLfunctions.getXML(); 
    Document doc = XMLfunctions.XMLfromString(xml); 

    // which datas need to be taken, by tag 
    nodes = doc.getElementsByTagName("image"); 

    desc = new HashMap<Integer, String>(); 
    bitMap = new HashMap<Integer, Bitmap>(); 

    new Thread(new Runnable() { 
     public void run() { 
      for (j = 0; j < 20; j++) { 
       if (j != 1) { 
        Element e = (Element) nodes.item(j); 
        desc.put(j, XMLfunctions.getValue(e, "description")); 
        Log.d("try", desc.get(j)); 
        Log.d("tryy", 
          XMLfunctions.getValue(e, "description")); 
        bitMap.put(j, XMLfunctions.getContactPhoto(XMLfunctions 
          .getValue(e, "path"))); 
       } 
      } 
     } 
    }).start(); 

    // add the first item to main page 
    textView = (TextView) (others.get(0)).findViewById(R.id.text); 
    img = (ImageView) (others.get(0)).findViewById(R.id.image); 
    Log.d("first", desc.get(0) + desc.get(0)); 
    if (textView != null) 
     textView.setText(desc.get(0)); 
    if (img != null) 
     img.setImageBitmap(bitMap.get(0)); 

    // add the rest of items to main page 
    for (j = 2; j < 10; j++) { 
     if (j % 2 == 0) { 
      textView1 = (TextView) (others.get(j)).findViewById(R.id.text1); 
      image1 = (ImageView) (others.get(j)).findViewById(R.id.image1); 
      Log.d("others", j + ": " + desc.get(j) + desc.get(j)); 
      if (textView1 != null) 
       textView1.setText(desc.get(j)); 
      if (image1 != null) 
       image1.setImageBitmap(bitMap.get(j)); 
     } else { 
      textView2 = (TextView) (others.get(j - 1)) 
        .findViewById(R.id.text2); 
      image2 = (ImageView) (others.get(j - 1)) 
        .findViewById(R.id.image2); 
      Log.d("others", j + ": " + desc.get(j) + desc.get(j)); 
      if (textView2 != null) 
       textView2.setText(desc.get(j)); 

      if (image2 != null) 
       image2.setImageBitmap(bitMap.get(j)); 
     } 
    } 

} 

    } 

そして最後に、私は「このクラスに到達するためにこれらのコードを使用していますこれが必要なのかどうかわからないけど、私もやっています。

mainPage fillMain = new mainPage(); 
fillMain.setPage(); 

答えて

0

AsyncTaskはあなたが

+0

私はそれもやってみましたが、私はそれをやり遂げることができませんでしたので、私はこの方法を試してみましたが、奇妙なことに、配列の値は変わりません... nullになる – yahya

+0

@yahyaあなたはそれの例を投稿できますか?バックグラウンドプロセスでエラーが発生している可能性がありますか? –

+0

@BrianMains私の答えをチェックしてください、私は私のAsyncTaskを投稿しました... – yahya

0

この例をチェックしてください使用したいものである:スレッドタスク前後の方法火災のhttp://www.vogella.de/articles/AndroidPerformance/article.html

つを、したがって、あなたがそれを使用することができるはずですがデータを取得します。 doInBackgroundメソッドの後にonPostExecuteの呼び出しが実際の作業を行います。 onPostExecuteは何でも得ますdoInBackgroundが返ってきます...

関連する問題