【问题标题】:Refresh button onClick Listview data must get update in android [duplicate]刷新按钮onClick Listview数据必须在android中更新[重复]
【发布时间】:2012-09-09 07:36:27
【问题描述】:

可能重复:
How to refresh Android listview?

我正在寻找使用刷新按钮刷新列表视图数据.. DES:在我的应用程序中有刷新按钮,当我点击它时来自服务器的列表视图数据必须刷新并显示更新列表视图数据。
我不知道该怎么做。请给我举个例子..提前谢谢。下面是我的代码。

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


}



}

【问题讨论】:

标签: android android-layout android-intent android-emulator


【解决方案1】:

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

按照步骤更新列表视图。

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.

示例

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

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

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

【讨论】:

  • 这个问题是重复的。在 StackOverflow 上被问了很多次。
  • @ChiragRaval 感谢您的友好回答.. 请不要标记它..
  • @KarthikRao 我不会标记它。
  • @ChiragRaval 目前我在我的应用程序中使用静态数据.. 硬件来检查它。它是否令人耳目一新..
  • @KarthikRao 尝试在列表中添加更多静态数据并再次检查
【解决方案2】:

你可以这样做

public class Mainactivity extends Activity{

ListView listView;
EfficientAdapter objectAdapter;

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

listView = (ListView) findViewById(R.id.homelistView);
listView.setTextFilterEnabled(true);   
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 = new EfficientAdapter(Mainactivity.this);// adapter with new data
       listView.setAdapter(objectAdapter);
       objectAdapter.notifyDataSetChanged();

    }
});

}

}

这是我想告诉你的一个示例:

public class second extends Activity{

    ListView listView;
    ArrayAdapter<String> adapter;
    Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second);

        listView=(ListView) findViewById(R.id.listView1);
        button=(Button) findViewById(R.id.button1);

        String[] planets = new String[] { "Mercury", "Venus", "Earth", "Mars",  
                "Jupiter", "Saturn", "Uranus", "Neptune"};    
        ArrayList<String> planetList = new ArrayList<String>();  
        planetList.addAll( Arrays.asList(planets) );  

        // Create ArrayAdapter using the planet list.  
        adapter = new ArrayAdapter<String>(this, R.layout.simplerow, planetList);  

        listView.setAdapter(adapter);

        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                String[] planets = new String[] { "Mercury m", "Venus m", "Earth m", "Mars m",  
                        "Jupiter m", "Saturn m", "Uranus m", "Neptune m"};    
                ArrayList<String> planetList = new ArrayList<String>();  
                planetList.addAll( Arrays.asList(planets) );  
                adapter = new ArrayAdapter<String>(second.this, R.layout.simplerow, planetList);

                listView.setAdapter(adapter);
                adapter.notifyDataSetChanged();
            }
        });

    }

}

R.layout.simplerow 在哪里:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/rowTextView" 
 android:layout_width="fill_parent" 
 android:layout_height="wrap_content"
 android:padding="10dp"
 android:textSize="16sp" >
</TextView>

和 R.layout.second:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >





    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >

    </ListView>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

</RelativeLayout>

【讨论】:

  • 我添加了你的代码,我必须在我的静态数据 CountryList.java 或 mainaactivity 中设置新数据。目前没有得到
  • 在您的适配器类中,您使用了 Country.java 类变量,但您可以创建新的 EfficientAdapter2 类,您可以在其中使用具有不同内容的 Country2.java 类。首先您使用 objectAdapter = new EfficientAdapter(this)然后在按钮单击中,您可以使用 objectAdapter = new EfficientAdapter2(Mainactivity.this)
  • 非常感谢......它工作正常......当数据来自网络服务时,我必须在 onClick(View v) { objectAdapter = new EfficientAdapter(Mainactivity.this) ;// 带有新数据的适配器 listView.setAdapter(objectAdapter); objectAdapter.notifyDataSetChanged();再次感谢...
【解决方案3】:

您必须向 CountryList 添加新值并调用 notifyDataSetChanged() API。

【讨论】:

    【解决方案4】:
    Button refreshButton= (Button)findViewById(R.id.refreshButton);
     refreshButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
        /*Intent next=new Intent(MainActivity.this, SeviceDetails.class);
        startActivity(next);*/
         Do a notifyDataSetChanged();
    
        }
    });
    

    点击More Info

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-03
      • 2012-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-17
      相关资源
      最近更新 更多