如何从Activity更新(刷新)ListView Widget?

时间:2015-07-22 18:12:53

标签: java android

    package com.example.ss.new_my_wid;

    import android.app.Activity;
    import android.app.PendingIntent;
    import android.appwidget.AppWidgetManager;
    import android.content.ComponentName;
    import android.content.Context;
    import android.content.Intent;
    import android.net.Uri;
    import android.os.Bundle;
    import android.view.KeyEvent;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.RemoteViews;
    import android.widget.TextView;
    import android.widget.Toast;




    public class ItemChanging extends Activity implements View.OnClickListener, View.OnKeyListener
    {

        EditText EdT;
        Button btn_OK;
        Button btn_Cancel;
        Button btn_Delete;
        Intent intent;
        String str, changed_text;
        Toast tst;

        final static String CURRENT_TEXT_IN_ITEM = "current_text_in_item";
        final static String POSITION = "position";

        public ItemChanging() {
            super();
        }


        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);

            setContentView(R.layout.type_to_item);

            EdT = (EditText) findViewById(R.id.editText);
            intent = getIntent();
            str = intent.getStringExtra(CURRENT_TEXT_IN_ITEM);

            EdT.setText(str, TextView.BufferType.EDITABLE);

            btn_OK = (Button)findViewById(R.id.but_OK);
            btn_Cancel = (Button) findViewById(R.id.but_Cancel);
            btn_Delete = (Button) findViewById(R.id.but_Del);

            btn_OK.setOnClickListener(this);
            btn_Cancel.setOnClickListener(this);
            btn_Delete.setOnClickListener(this);
            EdT.setOnKeyListener(this);


        }


        @Override
        public void onClick(View v)
        {
            int pos = intent.getIntExtra(POSITION,0);
            switch (v.getId())
            {
                case R.id.but_OK:
                    changed_text = EdT.getText().toString();
                    MyFactory.data.set(pos, changed_text);
                    /*HERE need update of ListView*/

                    tst = Toast.makeText(this, changed_text, Toast.LENGTH_SHORT);
                    tst.show();
                    finish();
                    break;
                case R.id.but_Cancel:
                    finish();
                    break;
                case R.id.but_Del:
                    MyFactory.data.remove(pos);
                    MyFactory.data.add("");


                    finish();
                    break;
            }
        }

    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event)
    {
        return false;
    }
}

我有ListView的小部件和带按钮的活动。在活动中,我更改ListView中的项目。当我点击按钮时,更改保存在ListView中,但我不知道如何从activit更新listView和小部件。我可以这样做吗?

P.S。抱歉我的英文=)

2 个答案:

答案 0 :(得分:3)

修改适配器中的数据后,在Adapter对象上调用notifyDataSetChanged()

How to refresh Android listview?

答案 1 :(得分:2)

如果您对listview的数据进行了更改,那么您需要使用新数据通知适配器

你可以通过调用notifyDataSetChanged()来做到这一点;适配器上的方法

for examlple

adapter.notifyDataSetChanged();