gpt4 book ai didi

java - 通过外部 AsyncTask 将项目添加到 Activity 中的 ListView

转载 作者:行者123 更新时间:2023-12-02 07:51:04 25 4
gpt4 key购买 nike

简单的问题,我希望在与任务产生的 Activity 不同的 Activity 中更新异步任务的 ListView 。我的问题是关于更新其他 Activity 中的适配器,我将如何访问其他 Activity 适配器,以便我可以添加到它(使用adapter.add(item);)并通知适配器更改以更新列表其他 Activity (adapter.notifyChange();)?

这是连接任务:

公共(public)类 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);
}

}

这是我希望填充的 Activity :

公共(public)类NotificationActivity扩展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()));

}

}

我读过类似的问题,并且听说过全局适配器的使用,但不知道如何实现这样的事情。

这是最后一 block 拼图,对我的应用程序来说仍然是个谜,任何有关此事的帮助将不胜感激。

感谢您的宝贵时间,

祝一切顺利。

最佳答案

您应该有一个 Adapter 使用的 ArrayList,您可以从 Activity 访问它,并修改它。它可以是 public static,也可以通过 Activity 的 getter 进行访问,该 getter 作为参数传递给 Activity

当您访问ArrayList时,您可以做任何您想做的事情,然后在ListView上为其他调用invalidateViews() > Activity

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

现在,当您的适配器调用其 getView() 方法时,它会获取您更新的列表。

关于java - 通过外部 AsyncTask 将项目添加到 Activity 中的 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10221094/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com