刷新按钮的onClick列表视图的数据必须在android系统获得更新视图、按钮、数据、列表

2023-09-07 04:56:04 作者:似是故人来

可能重复:结果  如何刷新列表视图的Andr​​oid?

我要寻找使用刷新按钮刷新列表视图数据.. DES:在我的应用程序有刷新按钮,当我在从服务器来必须把刷新和放它列表视图数据的onclick;显示列表视图更新数据。结果我不知道该怎么做。 PLZ建议我用的例子..在此先感谢。下面是我的code。

Mainactivity.java

 的ListView =(ListView控件)findViewById(R.id.homelistView);listView.setTextFilterEnabled(真);最后EfficientAdapter objectAdapter =新EfficientAdapter(本);listView.setAdapter(objectAdapter);按钮refreshButton =(按钮)findViewById(R.id.refreshButton); refreshButton.setOnClickListener(新OnClickListener(){    @覆盖    公共无效的onClick(视图v){    objectAdapter.notifyDataSetChanged();    }}); 

EfficientAdapter.java

 公共静态类EfficientAdapter延伸BaseAdapter {    私人LayoutInflater mInflater;    私人上下文的背景下;    公共EfficientAdapter(上下文的背景下){        mInflater = LayoutInflater.from(上下文);        this.context =背景;    }    公众诠释的getCount(){        返回CountriesList.name.length;    }    公共对象的getItem(INT位置){        返回的位置;    }    众长getItemId(INT位置){        返回的位置;    }    公共查看getView(INT位置,查看convertView,父母的ViewGroup){        ViewHolder持有人;        如果(convertView == NULL){            convertView = mInflater.inflate(R.layout.homemplebrowview,NULL);            持有人=新ViewHolder();            holder.text1 =(TextView中)convertView                    .findViewById(R.id.name);            holder.text2 =(TextView中)convertView                    .findViewById(R.id.mrn);            holder.text3 =(TextView中)convertView                    .findViewById(R.id.date);            holder.text4 =(TextView中)convertView                    .findViewById(R.id.age);            holder.text5 =(TextView中)convertView                    .findViewById(R.id.gender);            holder.text6 =(TextView中)convertView                    .findViewById(R.id.wardno);            holder.text7 =(TextView中)convertView                    .findViewById(R.id.roomno);            holder.text8 =(TextView中)convertView                    .findViewById(R.id.bedno);            holder.btnList =(按钮)convertView.findViewById(R.id.listbutton);         // holder.btnList.setOnClickListener(本);            holder.btnList.setOnClickListener(新OnClickListener(){                @覆盖                公共无效的onClick(视图v){                    接下来意向=新意图(背景下,SeviceDetails.class);                    context.startActivity(下);                }            });            convertView.setTag(保持器);        }其他{            支架=(ViewHolder)convertView.getTag();        }        holder.text1.setText(CountriesList.name [位置]);        holder.text2.setText(CountriesList.mrn [位置]);        holder.text3.setText(CountriesList.actualstart [位置]);        holder.text4.setText(CountriesList.age [位置]);        holder.text5.setText(CountriesList.gender [位置]);        holder.text6.setText(CountriesList.wardNo [位置]);        holder.text7.setText(CountriesList.roomNo [位置]);        holder.text8.setText(CountriesList.bedNo [位置]);        返回convertView;    }    静态类ViewHolder {        公共按钮btnList;        公众的TextView text8;        公众的TextView text7;        公众的TextView text6;        公众的TextView text5;        公众的TextView文本4;        公众的TextView文本1;        公众的TextView文本2;        公众的TextView文字3;    }    @覆盖    公共无效notifyDataSetChanged()//在适配器类创建这个功能    {        super.notifyDataSetChanged();    }}} 

解决方案

在适配器中添加新的值并调用 notifyDataSetChanged

动作编辑器实时布局,布局实时验证,Java8升级,Kotlin实时模板,构建数据实时分析,AndroidStudio4值得更新 c2017 1 10的博客 CSDN博客

请按照以下步骤来更新列表视图。

  1)首先新值添加到您的CountriesList。2)在指定EfficientAdapter到listiew,使得它的一个对象,然后分配对象。3)你叫notifyDataSetChanged EfficientAdapter类创建一个函数()。4)用EfficientAdapter对象调用notifyDataSetChanged()函数。 

示例

  EfficientAdapter objectAdapter =新EfficientAdapter(本);listView.setAdapter(objectAdapter);@覆盖公共无效notifyDataSetChanged()//在适配器类创建这个功能{    super.notifyDataSetChanged();} 

现在,在刷新按钮的onClick事件调用objectAdapter.notifyDataSetChanged()。

Possible Duplicate: How to refresh Android listview?

I am looking for using Refresh button refreshing the listview data.. DES:In my app there is refresh button when i onclick on it Listview data which coming from server must get refresh & show update listview data. And I have no idea how to do that. plz suggest me with example.. Thanks in advance. below is my code.

Mainactivity.java

listView = (ListView) findViewById(R.id.homelistView);
listView.setTextFilterEnabled(true);   
final EfficientAdapter objectAdapter = new EfficientAdapter(this);
listView.setAdapter(objectAdapter);


Button refreshButton= (Button)findViewById(R.id.refreshButton);
 refreshButton.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
    objectAdapter.notifyDataSetChanged();

    }
});

EfficientAdapter.java

public static class EfficientAdapter extends BaseAdapter {
    private LayoutInflater mInflater;
    private Context context;

    public EfficientAdapter(Context context) {
        mInflater = LayoutInflater.from(context);
        this.context=context;

    }


    public int getCount() {
        return CountriesList.name.length;
    }

    public Object getItem(int position) {

        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.homemplebrowview, null);
            holder = new ViewHolder();
            holder.text1 = (TextView) convertView
                    .findViewById(R.id.name);
            holder.text2 = (TextView) convertView
                    .findViewById(R.id.mrn);
            holder.text3 = (TextView) convertView
                    .findViewById(R.id.date);
            holder.text4 = (TextView) convertView
                    .findViewById(R.id.age);
            holder.text5 = (TextView) convertView
                    .findViewById(R.id.gender);
            holder.text6 = (TextView) convertView
                    .findViewById(R.id.wardno);
            holder.text7 = (TextView) convertView
                    .findViewById(R.id.roomno);
            holder.text8 = (TextView) convertView
                    .findViewById(R.id.bedno);                  
            holder.btnList = (Button)convertView.findViewById(R.id.listbutton);
         //   holder.btnList.setOnClickListener(this);

            holder.btnList.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {                       
                    Intent next=new Intent(context, SeviceDetails.class);
                    context.startActivity(next);
                }
            });


            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.text1.setText(CountriesList.name[position]);
        holder.text2.setText(CountriesList.mrn[position]);
        holder.text3.setText(CountriesList.actualstart[position]);
        holder.text4.setText(CountriesList.age[position]);
        holder.text5.setText(CountriesList.gender[position]);
        holder.text6.setText(CountriesList.wardNo[position]);
        holder.text7.setText(CountriesList.roomNo[position]);
        holder.text8.setText(CountriesList.bedNo[position]);

        return convertView;
    }


    static class ViewHolder {
        public Button btnList;
        public TextView text8;
        public TextView text7;
        public TextView text6;
        public TextView text5;
        public TextView text4;
        public TextView text1;
        public TextView text2;
        public TextView text3;
    }

    @Override
    public void notifyDataSetChanged() // Create this function in your adapter class
    {
        super.notifyDataSetChanged();
    }


}



}

解决方案

Add new values in your adapter and call notifyDataSetChanged .

Follow the step to update listview.

1) First Add new values to your CountriesList .
2) Before Assign EfficientAdapter to listiew, make one object of it and then assign that object.
3) Create one function in your  EfficientAdapter class called notifyDataSetChanged().
4) Call notifyDataSetChanged() function using EfficientAdapter object.

Example

EfficientAdapter objectAdapter = new EfficientAdapter(this);
listView.setAdapter(objectAdapter);

@Override
public void notifyDataSetChanged() // Create this function in your adapter class
{
    super.notifyDataSetChanged();
}

Now call objectAdapter.notifyDataSetChanged() on onClick event of refresh button.