如果文件存在,则从ListView中删除项目

时间:2014-03-09 19:25:08

标签: java android xml listview

我在MapsListActivity中创建了一个列表视图,它显示了位于string.xml中的数组中的项目。

此列表视图允许查看地图或下载相对地图(如果不存在)。

在Switch中,case 0将用户发送到另一个名为MyMapsActivity的活动。

MyMapsActivity扩展了MapsActivity。

MyMapsActivity应显示相同的列表视图,但只显示已在设备中下载的地图列表。

我的想法是使用if else检查设备中是否存在地图,因此如果地图存在,请不要显示设备中不存在的地图列表。

我该如何解决这个问题?到目前为止,我所尝试的一切都是无用的。

MapsListActivity.Java

public class MapsListActivity extends AlertDownload implements OnItemClickListener{


private static final File PRATO5_MAP = new File(Environment.getExternalStorageDirectory().getPath() + "/osmdroid/tiles/", "pratonew5.map");
private static final File PRATO4_MAP = new File(Environment.getExternalStorageDirectory().getPath() + "/osmdroid/tiles/", "pratonew4.map");
private static final File PRATO3_MAP = new File(Environment.getExternalStorageDirectory().getPath() + "/osmdroid/tiles/", "pratonew3.map");
private static final File PRATO2_MAP = new File(Environment.getExternalStorageDirectory().getPath() + "/osmdroid/tiles/", "pratonew2.map");
private static final File PRATO1_MAP = new File(Environment.getExternalStorageDirectory().getPath() + "/osmdroid/tiles/", "pratonew1.map");





ListView listView;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    String osmdroidFolder = "/osmdroid/";
      String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
      File myOsmdroidFolder = new File(extStorageDirectory + osmdroidFolder);
      myOsmdroidFolder.mkdir();

  String tilesFolder = "/osmdroid/tiles/";
      File myTilesFolder = new File(extStorageDirectory + tilesFolder);
      myTilesFolder.mkdir();

    listView = (ListView) findViewById(R.id.mapsList);
    listView.setOnItemClickListener(this);

}


@Override
public void onItemClick(AdapterView<?> adapter, View view, int position, long id) {
    switch (position){
    case 0:{
        Intent Info = new Intent(MapsListActivity.this, MyMapsActivity.class);
        startActivity(Info);
    }
    break;
    case 1:{

        if (PRATO1_MAP.exists()) {
            Intent Info = new Intent(MapsListActivity.this, MyMapsActivity.class);
            startActivity(Info);
        }
        else{
            DialogDownload1();
        }   
    }
    break;
    case 2:{

        if (PRATO2_MAP.exists()) {
            Intent Info = new Intent(MapsListActivity.this, MyMapsActivity.class);
            startActivity(Info);
        }
        else{
            DialogDownload2();
        }   
    }
    break;
    case 3:{

        if (PRATO3_MAP.exists()) {
            Intent Info = new Intent(MapsListActivity.this, MyMapsActivity.class);
            startActivity(Info);
        }
        else{
            DialogDownload3();
        }   
    }
    break;
    case 4:{

        if (PRATO4_MAP.exists()) {
            Intent Info = new Intent(MapsListActivity.this, MyMapsActivity.class);
            startActivity(Info);
        }
        else{
            DialogDownload4();
        }   
    }
    break;
    case 5:{

        if (PRATO5_MAP.exists()) {
            Intent Info = new Intent(MapsListActivity.this, MyMapsActivity.class);
            startActivity(Info);
        }
        else{
            DialogDownload5();
        }   
    }
    break;
    }

}

    }

MyMapsActivity.java

    public class MyMapsActivity extends MapsListActivity {



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

activity_main.xml中

        <?xml version='1.0' encoding='utf-8'?>
        <LinearLayout xmlns:android='http://schemas.android.com/apk/res/android'
          android:layout_width='fill_parent'
          android:layout_height='fill_parent'
          android:orientation='vertical' >

         <ListView
            android:id="@+id/mapsList"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:entries="@array/maps_array"
           />
       </LinearLayout>

string.xml

    <?xml version="1.0" encoding="utf-8"?>
    <resources>

        <string name="app_name">Maps</string>
        <string name="action_settings">Settings</string>

        <string-array name="maps_array">
            <item>MyMaps</item>
            <item>Prato1</item>
            <item>Prato2</item>
            <item>Prato3</item>
            <item>Prato4</item>
            <item>Prato5</item>
        </string-array>

    </resources>

1 个答案:

答案 0 :(得分:0)

为什么不在“/ osmdroid / tiles /”目录中查找所有mapfiles,然后将它们动态地添加到MyMapsActivity中的ListView中。您可以使用Javas File类的listFiles()方法。

File f = new File(extStorageDirectory + tilesFolder);        
File file[] = f.listFiles();

ArrayAdapter adapter = new ArrayAdapter(this, R.layout.list_view_row_item, file);  
listView.setAdapter(adapter);

类似的东西,如果地图不在你的瓷砖文件夹中,它将不会显示。