0

安卓 2.3.3

我在 stackoverflow 上看到了一些关于刷新列表视图的问题。但他们都没有为我工作。

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

下面是代码...

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();
        }
    }

}

编辑 ::: 使用屏幕截图 :::

在此处输入图像描述

在此处输入图像描述

4

5 回答 5

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();
        }
    });
    }

});
于 2013-03-17T07:48:31.157 回答
0

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

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

于 2013-03-17T07:49:44.910 回答
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();
        }

希望这对您有所帮助。

于 2013-03-17T10:21:41.170 回答
0

只需重新开始您的活动..

ActivityName.this.finish();
Intent intent=new Intent(this, ActivityName.class);
startActivity(intent);
于 2013-09-18T07:48:31.813 回答
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);
        }
    });

}
于 2016-08-19T23:19:11.457 回答