androids listView的setOnItemClickListener仅在不更新数据集时才有效。(androids listView's setOnItemClickListene

编程入门 行业动态 更新时间:2024-10-25 11:20:05
androids listView的setOnItemClickListener仅在不更新数据集时才有效。(androids listView's setOnItemClickListener works only if do not update data set. after update it stops)

我已经创建了wifi频道的ListView并且工作正常,onResume()调用更新列表的AsyncTask,但是在更新之后,即使我尝试再次设置它,单击侦听器也会停止工作。 这是我的代码:

package co.uk.company.application.ui.wifi; import android.app.AlertDialog; import android.content.DialogInterface; import android.os.AsyncTask; import android.os.Bundle; import android.util.Log; import android.view.ContextMenu; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.AdapterView; import android.widget.EditText; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; import co.uk.company.application.R; import co.uk.company.application.injected.ApplicationGlobalState; import co.uk.company.application.ui.companyActivity; import co.uk.company.application.ui.dialog.YesNoAlertDialogCreator; import co.uk.company.sources.telnet.WifiTelnetClass; import com.google.inject.Inject; import java.util.ArrayList; import java.util.Vector; import roboguice.inject.InjectView; public class WifiAPsActivity extends companyActivity{ @Inject private ApplicationGlobalState applicationGlobalState; @InjectView(R.id.wifi_networks_list) private ListView listView1; private WifiChannels wifiChannels; private WifiChannel channelSelected; private ArrayList<WifiChannel> data; private ArrayList<WifiChannel> dataold; private Vector <WifiChannel> vect; private WifiAPsAdapter adapter; private RealtimeUpdateScreen realtimeUpdateScreen; private AdapterView.OnItemClickListener listener; @Override public void onCreate(Bundle savedInstanceState){ Log.w("","onCreate(...) start"); super.onCreate(savedInstanceState); setContentView(R.layout.activity_wifi_networks); listener= new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, final int arg2, long arg3) { arg0.showContextMenuForChild(arg1); //if(adapter.getItem(arg2).toString().endsWith(".txt")) Toast.makeText(getApplicationContext(),getString(R.string.loading), Toast.LENGTH_LONG).show(); } }; dataold= new ArrayList<WifiChannel>(); data =new ArrayList<WifiChannel>(); wifiChannels=applicationGlobalState.wifiCurrentChannels; if (wifiChannels != null) for(WifiChannel chn : wifiChannels.getChannels()){ dataold.add(chn); data.add(chn); } wifiChannels=applicationGlobalState.wifiCurrentChannels5GHz; if (wifiChannels != null) for(WifiChannel chn : wifiChannels.getChannels()){ dataold.add(chn); data.add(chn); } adapter = new WifiAPsAdapter(this,R.layout.wifi_networks_item_row, data); listView1.setAdapter(adapter); listView1.setItemsCanFocus(false); registerForContextMenu(listView1); listView1.setOnItemClickListener(listener); Log.w("","onCreate(...) finish"); } @Override public void onResume(){ Log.w("","onResume() start"); super.onResume(); //realtimeUpdateScreen = new RealtimeUpdateScreen(); //realtimeUpdateScreen.execute(new Object()); Log.w("","onResume() finish"); } @Override public void onPause(){ super.onPause(); realtimeUpdateScreen.cancel(true); realtimeUpdateScreen = null; Log.w("","onPause() finish"); } @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo){ Log.w("","onCreateContextMenu"); AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo; // menu.setHeaderTitle(fileNames.get(info.position)); channelSelected = data.get(info.position); menu.add(Menu.NONE, 0, 0, "Connect"); menu.add(Menu.NONE, 1, 1, "Disconnect"); menu.add(Menu.NONE, 2, 2, "Do nothing"); } @Override public boolean onContextItemSelected(MenuItem item) { Log.w("","onContextItemSelected"); int menuItemIndex = item.getItemId(); //final AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo(); switch (menuItemIndex){ case 0: final AlertDialog.Builder alert = new AlertDialog.Builder(this); LayoutInflater li = LayoutInflater.from(this); View promptsView = li.inflate(R.layout.dialog_with_text_fields, null); alert.setView(promptsView); final TextView SSIDfield = (TextView) promptsView.findViewById(R.id.ssid_field); final EditText input2 = (EditText) promptsView.findViewById(R.id.password_field); SSIDfield.setText(channelSelected.getName()); // final Button okButton = (Button) promptsView.findViewById(R.id.wifi_button_yes); // final Button cancelButton =(Button)promptsView.findViewById(R.id.wifi_button_no); // okButton.setOnClickListener(new View.OnClickListener() { // @Override // public void onClick(View arg0) { // // } // }); // // cancelButton.setOnClickListener(new View.OnClickListener() { // @Override // public void onClick(View arg0) { // // alert. // } // }); alert.setPositiveButton("OKK", new DialogInterface.OnClickListener(){ public void onClick(DialogInterface dialog,int id) { String SSID = channelSelected.getName(); String PASSWORD = input2.getText().toString().trim(); if(WifiTelnetClass.getInstance().connect(SSID, PASSWORD)) Toast.makeText(getApplicationContext(), SSID+" Connected", Toast.LENGTH_SHORT).show(); else Toast.makeText(getApplicationContext(), "Not Connected!", Toast.LENGTH_SHORT).show(); } }); alert.setNegativeButton("CCancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog,int id) { dialog.cancel(); } }); alert.show(); break; case 1: YesNoAlertDialogCreator.create(this, "Are You Sure?", getString(R.string.sure_delete_result), new DialogInterface.OnClickListener(){ public void onClick(DialogInterface dialog, int whichButton) { Toast.makeText(getApplicationContext(),"Disconnecting!!", Toast.LENGTH_LONG).show(); if(WifiTelnetClass.getInstance().disconnect()) Toast.makeText(getApplicationContext(), " Disconnected", Toast.LENGTH_SHORT).show(); else Toast.makeText(getApplicationContext(), "Not Disconnected!", Toast.LENGTH_SHORT).show(); } }).show(); Toast.makeText(getApplicationContext(),"Nothing!!!", Toast.LENGTH_LONG).show(); break; case 2: YesNoAlertDialogCreator.create(this, "Password please!!!", getString(R.string.sure_delete_result), new DialogInterface.OnClickListener(){ public void onClick(DialogInterface dialog, int whichButton) { } }).show(); Toast.makeText(getApplicationContext(),"It!", Toast.LENGTH_LONG).show(); break; } return true; } class RealtimeUpdateScreen extends AsyncTask{ @Override protected void onPreExecute(){ try{ Thread.sleep(50L); }catch(Exception e){} } @Override protected Object doInBackground(Object... arg0){ while(!isCancelled()){ try{ if(wifiChannels!=applicationGlobalState.wifiCurrentChannels) publishProgress(arg0); try{Thread.sleep(50);}catch(Exception e){} }catch(Exception e){Log.e("","",e);} } return null; } protected void onProgressUpdate(Object...objects ){ try{ for(WifiChannel chn_ : dataold){ adapter.remove(chn_); } }catch(Exception e){Log.e("","",e);} try{ dataold = new ArrayList<WifiChannel>(); //adapter.notifyDataSetChanged(); vect=applicationGlobalState.wifiCurrentChannels5GHz.getChannels(); for(WifiChannel chn : vect){ dataold.add(chn); adapter.add(chn); } vect=applicationGlobalState.wifiCurrentChannels.getChannels(); for(WifiChannel chn : vect){ dataold.add(chn); adapter.add(chn); } adapter.notifyDataSetChanged(); }catch(Exception e){Log.e("","",e);} } } }

它能成为什么? 谢谢!

I have creating ListView of wifi channels and it works fine, onResume() calls the AsyncTask which updates the list, but after update the click listener stops working, even if I try to set it again. Here is my code:

package co.uk.company.application.ui.wifi; import android.app.AlertDialog; import android.content.DialogInterface; import android.os.AsyncTask; import android.os.Bundle; import android.util.Log; import android.view.ContextMenu; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.AdapterView; import android.widget.EditText; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; import co.uk.company.application.R; import co.uk.company.application.injected.ApplicationGlobalState; import co.uk.company.application.ui.companyActivity; import co.uk.company.application.ui.dialog.YesNoAlertDialogCreator; import co.uk.company.sources.telnet.WifiTelnetClass; import com.google.inject.Inject; import java.util.ArrayList; import java.util.Vector; import roboguice.inject.InjectView; public class WifiAPsActivity extends companyActivity{ @Inject private ApplicationGlobalState applicationGlobalState; @InjectView(R.id.wifi_networks_list) private ListView listView1; private WifiChannels wifiChannels; private WifiChannel channelSelected; private ArrayList<WifiChannel> data; private ArrayList<WifiChannel> dataold; private Vector <WifiChannel> vect; private WifiAPsAdapter adapter; private RealtimeUpdateScreen realtimeUpdateScreen; private AdapterView.OnItemClickListener listener; @Override public void onCreate(Bundle savedInstanceState){ Log.w("","onCreate(...) start"); super.onCreate(savedInstanceState); setContentView(R.layout.activity_wifi_networks); listener= new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, final int arg2, long arg3) { arg0.showContextMenuForChild(arg1); //if(adapter.getItem(arg2).toString().endsWith(".txt")) Toast.makeText(getApplicationContext(),getString(R.string.loading), Toast.LENGTH_LONG).show(); } }; dataold= new ArrayList<WifiChannel>(); data =new ArrayList<WifiChannel>(); wifiChannels=applicationGlobalState.wifiCurrentChannels; if (wifiChannels != null) for(WifiChannel chn : wifiChannels.getChannels()){ dataold.add(chn); data.add(chn); } wifiChannels=applicationGlobalState.wifiCurrentChannels5GHz; if (wifiChannels != null) for(WifiChannel chn : wifiChannels.getChannels()){ dataold.add(chn); data.add(chn); } adapter = new WifiAPsAdapter(this,R.layout.wifi_networks_item_row, data); listView1.setAdapter(adapter); listView1.setItemsCanFocus(false); registerForContextMenu(listView1); listView1.setOnItemClickListener(listener); Log.w("","onCreate(...) finish"); } @Override public void onResume(){ Log.w("","onResume() start"); super.onResume(); //realtimeUpdateScreen = new RealtimeUpdateScreen(); //realtimeUpdateScreen.execute(new Object()); Log.w("","onResume() finish"); } @Override public void onPause(){ super.onPause(); realtimeUpdateScreen.cancel(true); realtimeUpdateScreen = null; Log.w("","onPause() finish"); } @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo){ Log.w("","onCreateContextMenu"); AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo; // menu.setHeaderTitle(fileNames.get(info.position)); channelSelected = data.get(info.position); menu.add(Menu.NONE, 0, 0, "Connect"); menu.add(Menu.NONE, 1, 1, "Disconnect"); menu.add(Menu.NONE, 2, 2, "Do nothing"); } @Override public boolean onContextItemSelected(MenuItem item) { Log.w("","onContextItemSelected"); int menuItemIndex = item.getItemId(); //final AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo(); switch (menuItemIndex){ case 0: final AlertDialog.Builder alert = new AlertDialog.Builder(this); LayoutInflater li = LayoutInflater.from(this); View promptsView = li.inflate(R.layout.dialog_with_text_fields, null); alert.setView(promptsView); final TextView SSIDfield = (TextView) promptsView.findViewById(R.id.ssid_field); final EditText input2 = (EditText) promptsView.findViewById(R.id.password_field); SSIDfield.setText(channelSelected.getName()); // final Button okButton = (Button) promptsView.findViewById(R.id.wifi_button_yes); // final Button cancelButton =(Button)promptsView.findViewById(R.id.wifi_button_no); // okButton.setOnClickListener(new View.OnClickListener() { // @Override // public void onClick(View arg0) { // // } // }); // // cancelButton.setOnClickListener(new View.OnClickListener() { // @Override // public void onClick(View arg0) { // // alert. // } // }); alert.setPositiveButton("OKK", new DialogInterface.OnClickListener(){ public void onClick(DialogInterface dialog,int id) { String SSID = channelSelected.getName(); String PASSWORD = input2.getText().toString().trim(); if(WifiTelnetClass.getInstance().connect(SSID, PASSWORD)) Toast.makeText(getApplicationContext(), SSID+" Connected", Toast.LENGTH_SHORT).show(); else Toast.makeText(getApplicationContext(), "Not Connected!", Toast.LENGTH_SHORT).show(); } }); alert.setNegativeButton("CCancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog,int id) { dialog.cancel(); } }); alert.show(); break; case 1: YesNoAlertDialogCreator.create(this, "Are You Sure?", getString(R.string.sure_delete_result), new DialogInterface.OnClickListener(){ public void onClick(DialogInterface dialog, int whichButton) { Toast.makeText(getApplicationContext(),"Disconnecting!!", Toast.LENGTH_LONG).show(); if(WifiTelnetClass.getInstance().disconnect()) Toast.makeText(getApplicationContext(), " Disconnected", Toast.LENGTH_SHORT).show(); else Toast.makeText(getApplicationContext(), "Not Disconnected!", Toast.LENGTH_SHORT).show(); } }).show(); Toast.makeText(getApplicationContext(),"Nothing!!!", Toast.LENGTH_LONG).show(); break; case 2: YesNoAlertDialogCreator.create(this, "Password please!!!", getString(R.string.sure_delete_result), new DialogInterface.OnClickListener(){ public void onClick(DialogInterface dialog, int whichButton) { } }).show(); Toast.makeText(getApplicationContext(),"It!", Toast.LENGTH_LONG).show(); break; } return true; } class RealtimeUpdateScreen extends AsyncTask{ @Override protected void onPreExecute(){ try{ Thread.sleep(50L); }catch(Exception e){} } @Override protected Object doInBackground(Object... arg0){ while(!isCancelled()){ try{ if(wifiChannels!=applicationGlobalState.wifiCurrentChannels) publishProgress(arg0); try{Thread.sleep(50);}catch(Exception e){} }catch(Exception e){Log.e("","",e);} } return null; } protected void onProgressUpdate(Object...objects ){ try{ for(WifiChannel chn_ : dataold){ adapter.remove(chn_); } }catch(Exception e){Log.e("","",e);} try{ dataold = new ArrayList<WifiChannel>(); //adapter.notifyDataSetChanged(); vect=applicationGlobalState.wifiCurrentChannels5GHz.getChannels(); for(WifiChannel chn : vect){ dataold.add(chn); adapter.add(chn); } vect=applicationGlobalState.wifiCurrentChannels.getChannels(); for(WifiChannel chn : vect){ dataold.add(chn); adapter.add(chn); } adapter.notifyDataSetChanged(); }catch(Exception e){Log.e("","",e);} } } }

what can it be? thank You!

最满意答案

找到它:我必须在每次notifyDataSetChanged之后再次调用setOnItemClickListener。

Found it: I have to setOnItemClickListener after each notifyDataSetChanged again.

更多推荐

本文发布于:2023-08-02 21:08:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1381481.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:时才   数据   listView   setOnItemClickListener   androids

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!