2011-02-02 12 views
6

私はAndroidの開発には全く新しいです。誰かが私に関連するAPIを知らせてTextView(または画面全体)を動的に更新できるのですか?はAndroidのTextViewを動的に更新します

例:検索アプリケーションがあり、すべての結果を待って表示するのではなく、検索結果を表示する必要があります。

+0

主なアプリケーションの実行をブロックする検索を行わない場合は、別のスレッドで検索を実行する必要があります。次に、新しいTextViewオブジェクトでコンテナを定期的に更新するようにします。したがって、空の線形レイアウトを持つことができ、定期的に検索スレッドを呼び出して結果がどのようなものかを確認し、線形レイアウトに追加することができます。 – DeliveryNinja

+0

あなたの質問はまだ答えられていませんか? – winklerrr

答えて

1

TextViewに設定する新しいテキストがある場合は、textView.setText(newText)と入力してください。ここで、newTextは更新されたテキストです。 newTextが変更されたときはいつでもこのメソッドを呼び出します。それはあなたが探しているものですか?

+0

ありがとうございましたhmm ...ある程度答えています...しかし、私は古いデータを新しく来るデータに加えて維持する必要があります...どうすればいいですか...リストビューを使うべきです... – prash

+0

@古いデータを追跡するために 'textView.getText()'を使うことができます。最初に: 'CharSequence old = textView.getText();'、次に古いデータと新しいデータを連結して 'TextView'に戻します:' textView.setText(old + "" + new); ' – winklerrr

20

いくつかの手順がありますが、あなたが知っていることとあなたがどれくらいいないかについては言及していません。あなたのUI初期構造を仮定し

は、解像度/レイアウトで定義され、それがどこかに、あなたの活動でのTextViewが含まれています

public void updateTextView(String toThis) { 
    TextView textView = (TextView) findViewById(R.id.textView); 
    textView.setText(toThis); 
} 
+3

戻り値は冗長ですが、ありがとうございます! – Austin

0

まず、あなたはとても

private volatile ArrayList<String>  searchResults; 
ListView searchList = (ListView) findViewById(R.id.listYOURLISTVIEW); 
listAdapter = new ArrayAdapter<String>(this, R.layout.blacklist, R.id.list_content, searchResults); //blacklist is a layout to paint the fonts black 
searchList.setAdapter(listAdapter); 

、リストビューを必要としますここに役立つものがあります。

private Thread refreshThread; 
private boolean isRefreshing = false; 

private void reinitializeRefreshThread() 
{ 
    if (!refreshThread.equals(null)) refreshThread.stop(); 
    Log.d(LOGTAG+"::reinitializeRefreshThread()", "Creating new thread!\n"); 
    isRefreshing = true; 
    refreshThread = (new Thread(new Runnable() 
    { 
     public void run() 
     { 
      Looper.prepare(); 
      while (isRefreshing) 
      { 
       //GRAB YOUR SEARCH RESULTS HERE 
       //make sure the methods are synchronized 
       //maybe like 
       String[] results = grabResults(); //doesn't need to be synchronized 
       addResultList(results); 

       Message message = myHandler.obtainMessage(); 
       myHandler.sendMessage(message); 
       try 
       { 
        Thread.sleep(2000); 
       } catch (InterruptedException e) 
       { 
        e.printStackTrace(); 
       } 
       Log.d(LOGTAG+"->refreshThread", "Refresh finished!\n"); 
      } 
     } 
    })); 
    Log.d(LOGTAG+"::reinitializeRefreshThread()", "Refresh thread started!\n"); 
    refreshThread.start(); 
} 

final Handler myHandler = new Handler() 
{ 
    public void handleMessage(android.os.Message msg) 
    { 
     listAdapter.notifyDataSetChanged(); 
    }; 
}; 

どこ

public synchronized void addResultList(String[] results) 
{ 
    // remove the whole list, repopulate with new data 
    searchResults.clear(); 
    for (int i = 0; i < results.length; i++) 
    { 
     addResult(results[i]); 
    } 
} 

private synchronized void addResult(CharSequence result) 
{ 
    if (!searchResults.contains(result.toString())) 
     searchResults.add(result.toString()); 
    else 
     Toast.makeText(getApplicationContext(), "Result " + result.toString() + " was already on the list!", Toast.LENGTH_SHORT).show(); 
} 

このすべては、メインスレッドにメッセージを送ることによって、それがメインスレッド以外のスレッドから「変更」GUIにあなたをできるようになりますつまり、スレッドセーフであることUIを更新する必要があります。ご覧のように、refreshThreadは検索コレクションを更新し(メソッドが同期されていることを確認して)、後でメイン(UI)スレッドに通知してリストを更新します。

あなたは、おそらくこの便利すぎ

searchList.setOnItemClickListener(new AdapterView.OnItemClickListener() 
    { 
     public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) 
     { 
      listIndex = arg2; 
      selectedItem = (String) searchList.getItemAtPosition(listIndex); 
      Toast.makeText(getApplicationContext(), "Result[" + listIndex + "] " + selectedItem + " selected!", Toast.LENGTH_SHORT).show(); 
     } 
    }); 

ありますがLISTINDEXとのSelectedItemを初期化するか、単にEMを捨てる必要があるようないくつかの未決であってもよいが、一般的に、これはで私のためにトリックをした可能性があるでしょう非常に似たような状況

はそれが役立ちます:)あなたは、別のスレッド内にある場合

0

、あなたはUIスレッド内部のTextViewを更新してくださいホープ(私は新しいエントリで時間をかけて、リストを移入するバックグラウンドスレッドを持っています) 。

private void updateTextView(final String s) { 
    MainActivity.this.runOnUiThread(new Runnable() { 
     @Override 
     public void run() { 
      TextView tv= (TextView) findViewById(R.id.tv); 
      tv.setText("Text = "+s); 
     } 
    }); 

} 
関連する問題