动态ListView - 按钮上的刷新单击

时间:2013-03-17 07:43:18

标签: android android-listview

Android 2.3.3

我在stackoverflow上看到了一些关于刷新listview的问题。但它们都不适合我。

我有一个动态列表视图,里面有一个按钮。数据正在从数据库填充,每个listview行都是自定义布局。按下(清除)按钮,数据库被清除(工作正常),列表视图应该刷新(要计算的部分)。

以下是代码......

import java.util.ArrayList;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.mobilevoiceapps.advancedvoicecalculatorfree.R;

public class History extends ListActivity {

    ListView lvHistory;
    ArrayAdapter<String> adHistory;
    ArrayList<String> alHistoryCopy = new ArrayList<String>();
    String[] emptyList = { "Your Calculations History is Empty. Please Perform Some Calculations and visit again." };

    Database db;
    Cursor c;

    String expression = "";
    String result = "";

    boolean isEmpty = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        // setContentView(R.layout.history);

        lvHistory = getListView();
        final MyCustomAdapter myad = new MyCustomAdapter();

        db = new Database(this);
        c = db.retrieve_history();

        if (!((c.moveToFirst()) || c.equals(null) || c.getCount() == 0)) {

        } else if (c.getCount() > 0) {
            c.moveToFirst();
        }

        LayoutInflater header = getLayoutInflater();

        View header_View = header.inflate(R.layout.history_header, null);
        lvHistory.addHeaderView(header_View);

        View second_Header_View = header.inflate(
                R.layout.history_second_header, null);
        lvHistory.addHeaderView(second_Header_View);

        Button btnClearHistory = (Button) second_Header_View
                .findViewById(R.id.btnClearHistory);
        btnClearHistory.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                db.clear_history();
                myad.notifyDataSetChanged();
                lvHistory.invalidateViews();
                lvHistory.refreshDrawableState();
            }

        });

        lvHistory.setAdapter(myad);

    }

    class MyCustomAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return c.getCount();
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub

            HistoryViewHolder viewHolder;

            if (convertView == null) {
                LayoutInflater li = getLayoutInflater();
                convertView = li.inflate(R.layout.history_row, null);

                viewHolder = new HistoryViewHolder();

                viewHolder.txtcnt = (TextView) convertView
                        .findViewById(R.id.txtView_History_Count);
                viewHolder.txtexp = (TextView) convertView
                        .findViewById(R.id.txtView_History_Expression);
                viewHolder.txtres = (TextView) convertView
                        .findViewById(R.id.txtView_History_Result);

                convertView.setTag(viewHolder);
            } else {
                viewHolder = (HistoryViewHolder) convertView.getTag();
            }

            viewHolder.txtcnt.setText(String.valueOf(position + 1));
            System.out.println("Position"
                    + viewHolder.txtcnt.getText().toString());

            c.moveToPosition(position);

            expression = c.getString(c.getColumnIndex("expression"));
            result = c.getString(c.getColumnIndex("result"));

            viewHolder.txtexp.setText(expression);
            System.out.println("Expression"
                    + viewHolder.txtexp.getText().toString());
            viewHolder.txtres.setText(result);
            System.out.println("Result"
                    + viewHolder.txtres.getText().toString());

            return convertView;
        }

        public class HistoryViewHolder {
            TextView txtcnt;
            TextView txtexp;
            TextView txtres;
        }

    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();

        if (db != null) {
            db.close();
        }

        if (c != null) {
            c.close();
        }
    }

}

编辑:::使用ScreenShots :::

enter image description here

enter image description here

5 个答案:

答案 0 :(得分:0)

试试这个,您需要在使用ListView删除一个项目后刷新myad.notifyDataSetChanged();

  btnClearHistory.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

       YourActivity.this.runOnUiThread(new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            myad.notifyDataSetChanged();
        }
    });
    }

});

答案 1 :(得分:0)

尝试从BaseAdapter扩展MyCustomAdapter并使用notifyDataSetChanged刷新ListView。

显示数据库内容的更好方法是CursorAdapter(或CursorLoader) - 此类适配器会侦听更新通知并自动重新加载内容。

答案 2 :(得分:0)

尝试在c = db.retrieve_history();之后添加db.clear_history();

@Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            db.clear_history();
            c = db.retrieve_history();
            myad.notifyDataSetChanged();
            lvHistory.invalidateViews();
            lvHistory.refreshDrawableState();
        }

希望这有助于你。

答案 3 :(得分:0)

只需重新启动您的活动..

ActivityName.this.finish();
Intent intent=new Intent(this, ActivityName.class);
startActivity(intent);

答案 4 :(得分:0)

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_loja, container, false);

    listatividade = (ListView) view.findViewById(R.id.listservico);

    String[] valueatividade = new String[] { "atividade 1",
                                             "atividade 2",
                                             "atividade 3" };

    adapteratividade = new ArrayAdapter<String>(getActivity(),
            android.R.layout.simple_list_item_1, android.R.id.text1, valueatividade);

    listatividade.setAdapter(adapteratividade);

    listaatividade.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,int position, long id) {

            int itemPosition     = position;
            String  itemValue    = (String) listaatividade.getItemAtPosition(position);
            Toast.makeText(getActivity(), "Position :"+itemPosition+"  ListItem : " +itemValue , Toast.LENGTH_LONG) .show();
            updateData();
        }

    });

    return view;
}

private void updateData() {

    getActivity().runOnUiThread(new Runnable() {
        @Override
        public void run() {
            String[] valueatividade = new String[] { "atividade 4", "atividade 5", "atividade 6" };
            adapteratividade = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1, android.R.id.text1, valueatividade);
            listatividade.setAdapter(adapteratividade);
        }
    });

}