2013-08-28 37 views
0

我創建對話框,並進入對話框給出一個items.and對話框我也添加了搜索功能。當我點擊項目獲得正確的項目位置,但是當我搜索列表它沒有更新數據項目,而我不能檢索完全可點擊的項目。 以下是我的代碼。如何更新列表查看項目的位置,當我更新lisview

public void uploadFromDirve(View vi) { 
    EditText et; 

    //setContentView(R.layout.list_dialog); 
    // TODO Auto-generated method stub 
    listDialog = new Dialog(Project_Define_Activity.this); 
    listDialog.setTitle("Select Item"); 
    LayoutInflater li = (LayoutInflater) Project_Define_Activity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View v = li.inflate(R.layout.list_dialog, null, false); 
    listDialog.setContentView(v); 
    listDialog.setCancelable(true); 

    //there are a lot of <span id="IL_AD7" class="IL_AD">settings</span>, for dialog, <span id="IL_AD1" class="IL_AD">check</span> them all out! 
    ListView list1 = (ListView) listDialog.findViewById(R.id.listview); 
    adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,names); 
    list1.setAdapter(adapter); 

    et=(EditText)listDialog.findViewById(R.id.edit_Search); 
    et.addTextChangedListener(new TextWatcher() { 

     @Override 
     public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) { 
      // When user changed the Text 
      Project_Define_Activity.this.adapter.getFilter().filter(cs); 
      adapter.notifyDataSetChanged(); 

     } 

     @Override 
     public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, 
       int arg3) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void afterTextChanged(Editable arg0) { 
      // TODO Auto-generated method stub       
     } 


    }); 

    //list1.setAdapter(new ArrayAdapter<String>(Project_Define_Activity.this,android.R.layout.simple_list_item_1,names)); 
    //now that the dialog is set up, it's time to <span id="IL_AD2" class="IL_AD">show</span> it 

    list1.setOnItemClickListener(new OnItemClickListener(){ 

     @Override 
     public void onItemClick(AdapterView<?> adapter, View arg1, final int arg2, 
       long arg3) { 

      // TODO Auto-generated method stub 
      AlertDialog.Builder builder = new AlertDialog.Builder(Project_Define_Activity.this); 
      builder.setMessage("Attach file "+arg2) 
      .setPositiveButton("Attach ", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
        System.out.println("OK CLICKED"); 
        Log.e("Selected", names.get(arg2)); 
        fileflag = 1; 
        fileindex = arg2; 
        listDialog.cancel(); 

        nameOfFile.setText(names.get(arg2)); 
       } 
      }); 
      builder.setNegativeButton("cancel", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
        dialog.dismiss(); 
        listDialog.cancel(); 
       } 
      }); 
      AlertDialog alert = builder.create(); 
      alert.setTitle("Information"); 
      alert.show(); 

     } 

    }); 

    listDialog.show(); 



} 

回答

3

用於更新數據,你可以通知該數據集更改─

adapter.notifyDataSetChanged(); 

或另一種選擇是,清除你在適配器填充你的數組列表,並用新值更新數組列表和填充回喜歡 -

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

比透明,並再次分配新值

print_copy.clear(); 
print_copy.add(data); 
print_copy.add(data); 

並重新填充它。

謝謝