0

我在给定项目列表的情况下创建对话框并进入对话框。在对话框中,我还添加了搜索功能。当我单击项目时,它会获得正确的项目位置,但是当我搜索列表时,它没有用数据项目更新,而是我无法准确检索可点击项目。以下是我的代码。

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



}
4

1 回答 1

3

要更新数据,您可以通知数据集已更改-

adapter.notifyDataSetChanged();

或者另一种选择是清除您在适配器中填充的数组列表,并使用新值更新数组列表并将其填充回来 -

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

比清除并再次分配新值

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

并再次填充它。

谢谢

于 2013-08-28T04:39:37.750 回答