【问题标题】:How to refresh Listview within BaseAdapter? [duplicate]如何在 BaseAdapter 中刷新 Listview? [复制]
【发布时间】:2014-05-24 12:23:32
【问题描述】:

我的应用程序包含列表视图,Listview 从本地数据库sqlite 检索数据,通过使用适配器这是我的适配器类代码:

public class CommandsListAdapter extends BaseAdapter {

    public static String ID = null;
    private List<String> data;

    ArrayList<HashMap<String, String>> commandList = new ArrayList<HashMap<String, String>>();

    private ArrayAdapter<String> listAdapter;

    Context context;

    public CommandsListAdapter(Context con,
            ArrayList<HashMap<String, String>> list) {

        commandList = list;
        context = con;


    }

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


    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return commandList.get(arg0);
    }

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

    @Override
    public View getView(final int index, View convertView, ViewGroup arg2) {
        // TODO Auto-generated method stub


        if (convertView == null) {
            LayoutInflater mInflater = (LayoutInflater) context
                    .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

            convertView = mInflater.inflate(R.layout.people_list, null);
        }




        TextView tvTime = (TextView) convertView.findViewById(R.id.timeOfBlock);
        TextView tvName = (TextView) convertView.findViewById(R.id.name);

        tvName.setText(commandList.get(index).get(MyDatabase.Number_Block));
        tvTime.setText(commandList.get(index).get(MyDatabase.Time_Of_Block));

        Typeface tf = Typeface.createFromAsset(context.getAssets(),
                "segoeuil.ttf");
        tvTime.setTypeface(tf);
        tvName.setTypeface(tf);

        convertView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                // Message.message(context,
                // commandList.get(index).get(MyDatabase.Block_Table_Id));

                Intent i = new Intent(context, MessageDetail.class);

                i.putExtra(ID,
                        commandList.get(index).get(MyDatabase.Block_Table_Id)
                                .toString());

                i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

            //  context.startActivity(i);

            }
        });

        convertView.setOnLongClickListener(new OnLongClickListener() {

            public boolean onLongClick(View v) {
                // TODO Auto-generated method stub
                ID=commandList.get(index).get(MyDatabase.Block_Table_Id);
                removeListItem(v, index);
                return false;
            }
        });

        return convertView;
    }

    protected void removeListItem(View rowView, final int positon) {

        final Animation animation = AnimationUtils.loadAnimation(
                context, android.R.anim.slide_out_right);
        rowView.startAnimation(animation);
        Handler handle = new Handler();
        handle.postDelayed(new Runnable() {

            @Override
            public void run() {
                commandList.remove(positon);



            MyDatabase db=new MyDatabase(context);  
            db.open();
        //  db.deleteBlock(ID);
            db.close();

                Message.message(context, ID);

            }
        }, 1000);

    }



}

这是我的活动类代码:

public class ListOfBlock extends Activity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_of_block);

        ActionBar actionBar = getActionBar();
        // for color
        actionBar.setBackgroundDrawable(new ColorDrawable(Color
                .parseColor("#a20000")));

        if (actionBar != null) {
            actionBar.setTitle(getResources().getString(
                    R.string.title_activity_list_of_block));
            actionBar.setDisplayHomeAsUpEnabled(true);
            // actionBar.setIcon(R.drawable.ic_launcher);
        }

        ListView lvCommands = (ListView) findViewById(R.id.ListOfBlockerListView);

        lvCommands.setAdapter(new CommandsListAdapter(getApplicationContext(),
                new MyDatabase(getApplicationContext()).getBlockers()));

    }


}

现在我想在 getview 方法中刷新列表视图,我不知道该怎么做,我找到了一些答案,但这对我没有帮助,感谢您的帮助。

【问题讨论】:

  • getView 方法在列表视图中添加视图并自动刷新列表
  • 在使用 sqlite 时不要使用 Base Adapter,而是使用 SimpleCursorAdapter,正确使用时会自动刷新
  • Khalid Taha 如何做到这一点你能告诉我吗?
  • pskink 我知道 SimpleCursorAdapter 这太容易了,但我有一个设计不错的列表视图,如果我使用SimpleCursorAdapter,我就不能像我想要的那样设计。
  • 刷新是指列表视图中的内容被修改、删除或添加新内容时更新吗?

标签: android listview android-listview


【解决方案1】:
YourActivity.this.recreate();

这将重新创建您的列表。在执行这些操作的活动中保留一个变量说 n=false。然后,当您使用列表回调活动时,只需将其传递为 true。

在您执行这些操作的活动中,

Intent i = new Intent(Operations.this,ListActivity.class);
ListActivity.n=true;
startActivity(i);

在ListActivity的onCreate中创建一个类变量boolean n=false;

if(n){
 ListActivity.this.recreate();
 }

希望对您有所帮助。干杯!

【讨论】:

    【解决方案2】:

    我希望这对你有所帮助,在类适配器中这样做:-

    ArrayList<HashMap<String, String>> commandList = new ArrayList<HashMap<String, String>>();
    
    
    
    private ArrayList<HashMap<String, String>> searchArrayList;
    
    public CommandsListAdapter(Context con,
            ArrayList<HashMap<String, String>> list) {
    
        commandList = list;
        context = con;
    
        searchArrayList=list;
    }
    

    并创建一个函数来像这样更新您的listview,也在适配器类中:-

    public void updateResults(ArrayList<HashMap<String, String>> results) {
            searchArrayList = results;
            //Triggers the list update
            notifyDataSetChanged();
        }
    

    以及任何你想更新listview 的地方调用这个函数,我为你做了这个Sister,像这样:-

    updateResults(commandList); 
    

    可以肯定的是,上面的这些代码都是你的代码,但我做了一些修改,updateResults 函数commandList 的参数是你已经定义的 ArrayAdapter。 我希望这是工作

    【讨论】:

    • OOOOOOOOMMMMMMMGGGG,太棒了,是的,我想要这个,谢谢你的回答太棒了,谢谢兄弟:)\
    【解决方案3】:

    只需调用 BaseAdapter 类的notifyDataSetChanged() 即可刷新列表视图项。根据您的要求适当地调用它。 干杯。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-18
      • 2013-06-04
      • 2012-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-16
      • 1970-01-01
      相关资源
      最近更新 更多