0

我想从对话框中清除我的应用程序中的适配器。

主要活动:

public class SearchActivity extends ListActivity {
private File currentDir;
private FileArrayAdapter adapter;
private String searchStr = "";
List<Option>dir = new ArrayList<Option>();
List<Option>fls = new ArrayList<Option>();
private String requestedStartDir;
public void readConf(){ //Methode "Konfiguration lesen"
    try {
    File conf=new File("/sdcard/config.xml"); //Konfigurationsdatei
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();//
    DocumentBuilder db = dbf.newDocumentBuilder();//
            Document doc = db.parse(conf);//XML parsen
            requestedStartDir=doc.getElementsByTagName("Startordner").item(0).getTextContent();//String mit Tag "Startordner" befüllen
    }
    catch (Exception e) { //
        e.printStackTrace();//Fehlerbehandlung (keine)
      }
}

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.search_row);
    String gotDir="";
    readConf();
    Bundle extras = getIntent().getExtras();
    if(extras != null){
        searchStr = extras.getString("search");
        gotDir = extras.getString("Dir");
    }
    currentDir = new File(gotDir);
    adapter = new FileArrayAdapter(this,R.layout.search_layout,fls);
    fill(currentDir);
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    try {
    // TODO Auto-generated method stub
    super.onListItemClick(l, v, position, id);
    Option o = adapter.getItem(position);
    onFileClick(o);
    }
    catch (Exception e){
        Log.e("TEST", Log.getStackTraceString(e));
    }
}

private void onFileClick(Option o)
{
    try {
        File file = new File(o.getPath());
        Log.d("TEST", o.getPath());
        Intent opdf = new Intent(Intent.ACTION_VIEW);
        opdf.setDataAndType(Uri.fromFile(file), "application/pdf");
        opdf.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
        startActivity(opdf);
    }
    catch (Exception e){
        Log.e("TEST", Log.getStackTraceString(e));
    }

}

public static String removeExtension(String s) {

    String separator = System.getProperty("file.separator");
    String filename;

    // Remove the path upto the filename.
    int lastSeparatorIndex = s.lastIndexOf(separator);
    if (lastSeparatorIndex == -1) {
        filename = s;
    } else {
        filename = s.substring(lastSeparatorIndex + 1);
    }

    // Remove the extension.
    int extensionIndex = filename.lastIndexOf(".");
    if (extensionIndex == -1)
        return filename;

    return filename.substring(0, extensionIndex);
}

private void fill(File f)
{
    File[]dirs = f.listFiles();
      try{
         for(File ff: dirs)
         {
            if(ff.isDirectory()){
                dir.add(new Option(ff.getName(),"File",ff.getAbsolutePath()));
                File r = new File (ff.getPath());
                fill2(r);
            }
            else{
             String Name = (ff.getName().toLowerCase(Locale.GERMAN));
             if (Name.contains(searchStr.toLowerCase(Locale.GERMAN))){
                 fls.add(new Option(removeExtension(ff.getName()),"Folder",ff.getAbsolutePath()));
             }
            }
         }
     }catch(Exception e)
     {

     }
     Collections.sort(dir);
     Collections.sort(fls);
     dir.addAll(fls);
     this.setListAdapter(adapter);
     if (adapter.isEmpty()){
         Toast toast = Toast.makeText(this, "Keine Ergebnisse gefunden", Toast.LENGTH_LONG);
            toast.show();
            finish();
        }
     this.setListAdapter(adapter);
}
private void fill2(File f)
  {
    File[]dirs = f.listFiles();

     try{
         for(File ff: dirs)
         {
             if(ff.isDirectory()){
                 dir.add(new Option(ff.getName(),"Folder",ff.getAbsolutePath()));
                 fill2(ff);
             }
             else{
                 String Name = (ff.getName().toLowerCase(Locale.GERMAN));
                 if (Name.contains(searchStr.toLowerCase(Locale.GERMAN))){
                     fls.add(new Option(removeExtension(ff.getName()),"Folder",ff.getAbsolutePath()));
                 }
             }

         }
     }catch(Exception e)
     {

     }
}
 public void osp (View v){
  adapter.clear();
  adapter.notifyDataSetChanged();
 }

public void onSearchpressed(View view){
    final Dialog dialog = new Dialog(this);
    dialog.setContentView(R.layout.dialog);
    dialog.setTitle("Suche...");
    Button dialogButton1 = (Button) dialog.findViewById(R.id.dialogAbbruch);
    // if button is clicked, close the custom dialog
    dialogButton1.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            dialog.dismiss();
        }
    });
    Button dialogButton2 = (Button) dialog.findViewById(R.id.dialogSuche);
    // if button is clicked, close the custom dialog
    dialogButton2.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            EditText et = (EditText) dialog.findViewById(R.id.editText1);
            String searchStr = et.getText().toString();
            if (et.getText().toString().equals("")){
                Toast toast = Toast.makeText(getBaseContext(), "Bitte einen Suchbegriff eingeben", Toast.LENGTH_LONG);
                toast.show();}
            else{
                osp(view);
                dialog.dismiss();
            }
        }
    });

    Button dialogButton3 = (Button) dialog.findViewById(R.id.dialogSucheAll);

    dialogButton3.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            EditText et = (EditText) dialog.findViewById(R.id.editText1);
            String searchStr = et.getText().toString();
            File sdir = new File (requestedStartDir);
            currentDir = sdir;
            if (et.getText().toString().equals("")){
                Toast toast = Toast.makeText(getBaseContext(), "Bitte einen Suchbegriff eingeben", Toast.LENGTH_LONG);
                toast.show();}
            else{
                osp(view);
                dialog.dismiss();
            }
        }
    });

    dialog.show();
}

@Override
public void onBackPressed() {
                finish();
        }


}

文件阵列适配器:

public class FileArrayAdapter extends ArrayAdapter<Option>{

private Context c;
private int id;
private List<Option>items;

public FileArrayAdapter(Context context, int textViewResourceId,
    List<Option> objects) {
super(context, textViewResourceId, objects);
c = context;
id = textViewResourceId;
items = objects;
}

public Option getItem(int i)
 {
 return items.get(i);
 }
 @Override
  public View getView(int position, View convertView, ViewGroup parent) {

      View v = convertView;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
         v = vi.inflate(id,parent, false);
            }
      final Option o = items.get(position);
      if (o != null) {
              TextView t2 = (TextView) v.findViewById(R.id.textView2);

              if(t2!=null)
                t2.setText(o.getName());

            t2.setEllipsize(null);
            t2.setTextSize(54);
            t2.setBackgroundColor(Color.LTGRAY);
      }
      return v;
}

}

当我从 MainActivity 调用 osp(view) 时,它会清除适配器,但是当我从对话框中调用它时,它不会。我认为很容易找到我的错,但我不能。我该怎么做才能让它工作?

4

1 回答 1

0

您不能只Activity从 a 调用您的方法,Dialog因为它没有 Activity 的实例。此外,您不能osp(view)从 Activity 以外的任何其他类调用,因为View那里没有按钮。

我会在这里实现一个自定义listener,或者如果你不喜欢听众一个LocalBroadcastManager。在您选择听众的情况下,此答案可能会帮助您了解听众。

于 2013-10-28T14:36:13.267 回答