返回活动时使用自定义基准适配器更新列表视图?

时间:2016-03-16 23:51:41

标签: android listview object refresh

我正在尝试在我的应用中保存新调查时更新我的​​列表视图。

但我还没有实现这个功能。

这是我的一些代码:

public class MainActivity extends AppCompatActivity {
    private ListView lv1;
    private GeoXML xml;
    private ArrayList<Releve> listeReleve = new ArrayList<Releve>();

    public void onClickUndefined(View view) {
        Toast.makeText(view.getContext(), "Fonction non implémenté", Toast.LENGTH_LONG).show();
    }


    public void onClickCreateReport(View view) {
        Intent intent = new Intent(MainActivity.this, NewSurveyActivity.class);
        intent.putExtra("ReleveExistant", false);
        startActivity(intent);
    }

    @Override
    public void onResume() {
        super.onResume();
        listeReleve.clear();
        scanForReleve();
        lv1.deferNotifyDataSetChanged();
    }



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        ImageView imgNouveauReleve = (ImageView) findViewById(R.id.Img_Nouveau_Releve);
        ImageView imgBoutonAjouter = (ImageView) findViewById(R.id.bouton_ajouter);
        TextView txtNouveauReleve = (TextView) findViewById(R.id.MA_TitreListeReleve);
        lv1 = (ListView) findViewById(R.id.MA_ListeReleves);

        //supprimerTout();
        //scanForReleve();

        //ArrayList<Releve> listeReleve = GetSearchResults();
        //listeReleve.clear();

        if (!listeReleve.isEmpty()) {

            imgNouveauReleve.setVisibility(View.INVISIBLE);
            imgBoutonAjouter.setVisibility(View.VISIBLE);
            txtNouveauReleve.setVisibility(View.VISIBLE);
            lv1.setVisibility(View.VISIBLE);

            lv1.setAdapter(new MyCustomBaseAdapter(this, listeReleve));
            lv1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> a, View v, int position, long id) {
                    Object o = lv1.getItemAtPosition(position);
                    Releve fullObject = (Releve) o;
                    //Toast.makeText(MainActivity.this, "Vous avez choisi : " + " " + fullObject.getProjet(), Toast.LENGTH_LONG).show();
                    Intent intent = new Intent(MainActivity.this, NewSurveyActivity.class);
                    intent.putExtra("Releve", fullObject);
                    intent.putExtra("ReleveExistant", true);
                    startActivity(intent);
                }
            });
        } else {

            imgNouveauReleve.setVisibility(View.VISIBLE);
            imgBoutonAjouter.setVisibility(View.INVISIBLE);
            txtNouveauReleve.setVisibility(View.INVISIBLE);
            lv1.setVisibility(View.INVISIBLE);
        }
    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }


    public boolean supprimerTout (){
        String filepath = "";
        ContextWrapper contextWrapper = new ContextWrapper(getApplicationContext());
        File dir = contextWrapper.getDir(filepath, Context.MODE_PRIVATE);

        //File dir = new File(releve.getPath());
        if (dir.isDirectory())
        {
            String[] children = dir.list();
            for (int i = 0; i < children.length; i++)
            {
                new File(dir, children[i]).delete();
            }
        }

        dir.delete();
        return true;

    }

    public void scanForReleve(){

        ContextWrapper contextWrapper = new ContextWrapper(getApplicationContext());
        File directory = contextWrapper.getDir("", Context.MODE_PRIVATE);

        File dir = new File(directory.getPath() + "/releve/");
        GeoXML xmlObj = new GeoXML();

        if (dir.isDirectory())
        {
            String[] children = dir.list();
            for (int i = 0; i < children.length; i++)
            {
                listeReleve.add(xmlObj.lireXML(directory.getPath(), children[i]));
            }
        }

    }    

}

这是我第一次使用onResume()方法。

不知道这是否正确。

1 个答案:

答案 0 :(得分:0)

您需要使用NewSurveyActivity通知startActivityForResult()的结果。使用以下链接实现它。

How to manage `startActivityForResult` on Android?

只有RESULT_OK才能更新列表。