我有两个类:MainActivity和ClientListManager。
CLientListManager在接收时将消息发送到MainActivity(通过处理程序),期望更新ListView。但是,情况并非如此。
正确接收消息的内容,但ListView本身没有更新(我尝试使用listView和适配器进行更新但不起作用)。
该应用还有一个ActionBar。当我点击ActionBar的一个特定选项卡时,我们在这里讨论的listView被夸大了。
现在,当我点击该标签时,只有列表视图才会更新。我想在某些方法上更新ListView,如myArrayAdapter.add()或myListView.add()(即动态)
仅供参考。 ,CLM从另一个线程接收消息,并且在接收消息时,CLM的处理程序创建另一个消息(并附加一些数据)并将其发送到mainAcitivty。
我尝试实现一个新的应用程序,我使用非Acitvity Thread通过非活动线程更新listView的内容 - > MainActivity线程消息发送机制(我通过发送List发送数据),我能够做到这一点没有错误,但在这里它不会发生。为什么?
MainActivity.java
package com.webServer;
public class MainActivity extends Activity {
Activity mainAct;
ActionBar actionbar;
String log;
Handler mainHandler;
int toast = 1, dialog = 2, IPMessage = 3, clientActionBar = 4, serverActionBar = 5, displayServerListUpdate = 6, updateClientListFromUpdatingThread = 9;
public static String internaldirectoryName;
Boolean clientStatus = false, serverStatus = false;
ListView serverListView, clientListView;
static int mainServerPort;
final ArrayList serverList = new ArrayList();
final ArrayList clientList = new ArrayList();
String remoteServerIPForClient;
String remoteServerPortForClient;
Handler clientListHandler;
ArrayAdapter serverAdapter;
ArrayAdapter clientsAdapter;
int p = 0;
/**
* Called when the activity is first created.
* <p/>
* 1)Sets up the MainHandler extensively used by other threads and classes.
* <p/>
* 2) Sets up the actionBar, lists and adapters.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
View clientView = getLayoutInflater().inflate(R.layout.clients, null);
serverListView = (ListView)findViewById(R.id.s_list);
clientListView = (ListView)clientView.findViewById(R.id.c_log);
serverAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, serverList);
ArrayAdapter clientLogAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, clientList);
serverListView.setAdapter(clientLogAdapter);
clientListView.setAdapter(serverAdapter);
ListView online_list = (ListView) findViewById(R.id.connected_clients);
serverList.add("one");
for (int i = 0; i <= 100; i++)
serverList.add(SystemClock.uptimeMillis());
Resources res = getResources();
log = res.getString(R.string.log_text);
setupActionBar();
mainHandler = new Handler() {
@Override
public void handleMessage(Message m) {
if (m.getData().getInt("POST") == toast) {
Toast.makeText(MainActivity.this, m.getData().getString("CONTENT"), Toast.LENGTH_SHORT).show();
} else if (m.getData().getInt("POST") == dialog) {
new AlertDialog.Builder(MainActivity.this).setMessage(m.getData().getString("CONTENT")).setNegativeButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
}).show();
} else if (m.getData().getInt("POST") == IPMessage) {
serverList.add(m.getData().getString("IPAddress"));
} else if (m.getData().getInt("POST") == clientActionBar) {
actionbar.setTitle(m.getData().getString("CONTENT"));
if (m.getData().getBoolean("CONNECTED"))
actionbar.setIcon(R.drawable.tick);
else if (!(m.getData().getBoolean("CONNECTED")))
actionbar.setIcon(R.drawable.red);
} else if (m.getData().getInt("POST") == displayServerListUpdate) {
String[] temp = m.getData().getStringArray("COMPLETE CLIENT LIST");
new AlertDialog.Builder(MainActivity.this).setMessage("Updating display list with " + temp[0]).setNegativeButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
}).show();
serverList.clear();
for (int i = 0; i < temp.length; i++)
serverList.add(temp[i]);
} else if (m.getData().getInt("POST") == updateClientListFromUpdatingThread) {
Toast.makeText(MainActivity.this, "Received is " +m.getData().getStringArrayList("COMPLETEC").toString() , Toast.LENGTH_SHORT).show();
serverAdapter.addAll(m.getData().getStringArrayList("COMPLETEC"));
serverAdapter.notifyDataSetChanged();
}