0

を介してアクティビティのリストビューに項目を追加するクイック質問、私は、タスクが生成されたのとは別のアクティビティで非同期タスクからリストビューを更新しようとしています。私の質問は、他のアクティビティのアダプタを更新することです。どのように他のアクティビティのアダプタにアクセスして追加することができますか(adapter.add(item);を使用して)、リストを更新するためにアダプタに変更を通知します他のアクティビティ(adapter.notifyChange();)?ここ外部AsyncTask

接続タスクである:

パブリッククラスConnectionTaskはAsyncTask <コンテキストを拡張し、文字列、ボイド> {

private String mText; 
private Context mContext; 
private int NOTIFICATION_ID = 1; 
private Notification mNotification; 
private NotificationManager mNotificationManager; 




@SuppressWarnings("unused") 
private NotificationActivity noteact = new NotificationActivity(); 


public ConnectionTask(Context context){ 

    this.mContext = context; 

    //Get the notification manager 
    mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE); 



} 








@Override 
protected void onPreExecute() { 

    Log.i("PushNote", "onPreExecute"); 
} 






public void setmText(String mText){ 
    this.mText = mText; 
} 


public String getmText(){ 
    return mText; 
} 


@Override 
protected Void doInBackground(Context... params) { 
    Socket clientSocket = null; 
    //Creates the is connected boolean and sets it to false 
    boolean connected = false; 



    String ipaddr = getmText(); 

    // define a writer and a reader, so that we can interact with the 
    // Server 
    BufferedReader inFromServer = null; 



    InetAddress addr = null; 
    try { 

     addr = InetAddress.getByName(ipaddr); 
     } catch (UnknownHostException e1) { 
     // TODO Auto-generated catch block 
     publishProgress(e1.toString()); 
     e1.printStackTrace(); 
    } 



    // Dynamically find IP of current Localhost 
    String HostName = addr.getHostName(); 



    int port = 6789; 


    try { 
     // Lets try and instantiate our client and define a port number. 


     clientSocket = new Socket(HostName, port); 
     // once the client is connected sets the isConnected boolean to true 
     connected = true; 



     // lets also link the writer and reader to the client. 
     inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); 

     // make sure to always catch any exceptions that may occur. 
     } catch (UnknownHostException e) { 
     // always print error to "System.err" 
     publishProgress(e.toString()); 
     // 2 different types of exceptions, so we want to output meaning 
     // information for both. 
     } catch (IOException e) { 
     publishProgress(e.toString()); 
    } 



    // Boolean which indicates if the client is connected or not, 
    // if connected it takes in the next line of user input and returns the servers response. 
    while (connected) { 



     // Send the user message to the server 




     // read the reply from the server 
     String reply = null; 
     try { 
      reply = inFromServer.readLine(); 
      } catch (IOException e) { 
      // TODO Auto-generated catch block 

      e.printStackTrace(); 
      publishProgress("Failed to connect."); 
      System.exit(1); 

     } 

     if (reply != null){ 

      // output the reply as a notification 
      if (isCancelled()){ 
       break; 
      } 
      publishProgress(reply); 


      } else { 
      try { 
       inFromServer.close(); 
       } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
       publishProgress(e.toString()); 
       System.exit(1); 
      } // the reader 
      try { 
       clientSocket.close(); 
       } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
       publishProgress(e.toString()); 
       System.exit(1); 
      } // and the client socket 
     } 

    } 


    // always remember to close all connections. 






    // TODO Auto-generated method stub 
    return null; 
} 

@SuppressWarnings({ "unchecked", "rawtypes" }) 
@Override 
protected void onProgressUpdate(String... item) { 

    Notification("Push2Note: ", item[0]); 






} 




public void Notification(String contentTitle, String contentText) { 

    //Build the notification using Notification.Builder 
    long[] vibrate = {0,100,200,300}; 

    PendingIntent pendingIntent; 
    Intent intent = new Intent(); 
    intent.setClass(mContext,NotificationActivity.class); 
    pendingIntent = PendingIntent.getActivity(mContext, 0, intent, 0); 


    NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext) 
    .setSmallIcon(android.R.drawable.presence_online) 
    .setAutoCancel(true) 
    .setVibrate(vibrate) 
    .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)) 
    .setContentIntent(pendingIntent) 
    .setContentTitle(contentTitle) 
    .setContentText(contentText); 

    //Get current notification 
    mNotification = builder.getNotification(); 



    //Show the notification 
    mNotificationManager.notify(NOTIFICATION_ID, mNotification); 
} 

}

ここ移入するために探して活性IMである:

public class NotificationActivity extends ListActivity {

/** Called when the activity is first created. */ 
    @SuppressWarnings({ "unchecked", "rawtypes" }) 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.notifications); 

     setListAdapter(new ArrayAdapter(this, 
       android.R.layout.simple_list_item_checked, new ArrayList())); 

    } 

}

アイブ氏は、トラフ同様の質問を読んで、グローバルなアダプタの使用のTELLを聞いたことがあるが、私はそのようなことを実装する方法は考えています。

これは私のアプリケーションと私に謎に残っているパズルの最後の部分であり、この問題に関する助けがあれば幸いです。

ありがとうございます。

すべてのベスト。

+0

ここにコードを記入してください。それで、誰も助けてくれる人はいないでしょう –

+0

安定していることを十分に確信して、私は関連するコードで自分の投稿を修正しましたが、そのコンセプトを持っている人はすぐに答えることができますコードを覗き込む。 – patrickjquinn

答えて

1

Adapterが使用するArrayListがあります。Activityからアクセスできます。Activityはそれを変更します。 public staticのいずれかになります。または、Activityにパラメータとして渡されたActivityからゲッターを介してアクセスできます。

ArrayListにアクセスしたときは、何でも構いませんし、invalidateViews()ListViewActivityに電話することができます。お使いのアダプタはそのgetView()メソッドを呼び出したときに

items.remove(position); 
MainListActivity.listView.invalidateViews(); 

は今、それはあなたが更新されたリストを取得します。

+0

例えば、ArrayListをConnectionTaskで公開して、NotificationActivityのアダプタでArrayListを使用してから、ConnectionTaskを変更して無効にしますか?入力のおかげで – patrickjquinn

+0

ArrayListは、アダプタで使用されているものと同じインスタンスにする必要があります。また、ListViewにアクセスしてinvalidateViewsを呼び出す必要があります。 – Demonick

1

1つの方法は、結果をアクティビティに戻し、アダプタリストに追加してadapter.NotifyDatasetChanged();を呼び出すことです。

0

私は、非同期タスクの配列リストに追加するのではなく、アクティビティで空の配列リストを作成してアダプタに追加したほうが簡単だと答えました。これは、AsyncTaskが実行される前にアクティビティがアクティブでなければならないことを意味します(存在しない場合は、存在しないアダプタに追加しようとします)。コード:ConnectionTaskで

私はpublishprogressする値を渡され、その後

NotificationActivity.adapter.add(item[0]); 

を実行して、私の活動に私が今持っている:すべてのヘルプみんなのため

public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 

ArrayList<String> mArrayList = new ArrayList<String>(); 

adapter = new ArrayAdapter<String>(this, R.layout.recentrow,mArrayList); 


setListAdapter(adapter); 


ListView listView = getListView(); 
listView.setTextFilterEnabled(true); 


} 

おかげで、couldn」をあなたが私に物事を再考させることなく、ここにいた。