2013-02-13 40 views
0

我已经创建了无线频道的ListView并且它工作正常,onResume()调用了更新列表的AsyncTask,但更新后,点击监听器停止工作,即使我尝试再次设置它。这是我的代码:androids listView的setOnItemClickListener只有在不更新数据集的情况下才有效。更新后它停止

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);} 
     } 
    } 
} 

它是什么? 谢谢你!

回答

0

发现它:我必须在每次notifyDataSetChanged后重新设置setOnItemClickListener。

相关问题