更新listview时如何更新Listview项目位置

时间:2013-08-28 04:24:36

标签: java android listview

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

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



}

1 个答案:

答案 0 :(得分:3)

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

adapter.notifyDataSetChanged();

或另一个替代方法是清除您在适配器中填充的数组列表,并使用新值更新数组列表并将其填充回来,如 -

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

比清除并再次分配新值

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

再次填充。

由于