在android studio中更新列表视图

时间:2017-03-14 19:39:56

标签: android listview notifydatasetchanged

我正在android studio中创建一个应用程序。

我需要使用通过短信发送的新网址更新列表视图

如何将新的网址添加到我的列表中?

这是我目前的主要代码。

FileShare.ReadWrite

MyListAdapter:

 public class MainActivity extends AppCompatActivity {

 ListView list;
WebView webView;
ArrayList<String> sites = new ArrayList<> ("https://www.google.com", "https://www.yahoo.com", "https://www.apple.com");
String [] url = {"https://www.google.com", "https://www.yahoo.com", "https://www.apple.com"};

MyBroadcastReceiver receiver;


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



    list = (ListView)findViewById(R.id.listview);
    MyListAdapter adapter = new MyListAdapter(this, sites);
    sites.add("new site");
    adapter.notifyDataSetChanged();
    list.setAdapter(adapter);
    list.setDividerHeight(5);
    webView = (WebView)findViewById(R.id.webview);
    webView.setWebViewClient(new WebViewClient(){});

    list.setOnItemClickListener(new AdapterView.OnItemClickListener(){
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long id){
            Toast.makeText(getApplicationContext(),sites[position], Toast.LENGTH_SHORT).show();
            webView.loadUrl("https://www.google.com");
            webView.loadUrl(url[position]);

        }
    });

    receiver = new MyBroadcastReceiver();
    IntentFilter filter = new IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED);
    registerReceiver(receiver,filter);
}


} 

}

2 个答案:

答案 0 :(得分:2)

您定义 网站 作为最终也无法更新它,而不是String []使用字符串的arraylist:

  ArrayList<String> sites = new ArrayList<>();

更改适配器的构造函数以接受ArrayList而不是数组,并且当接收到新数据时使用它:

sites.add("new site !");
adapter.notifyDataSetChanged();
如果您喜欢这样,请在Adapter的构造函数中

  public class MyListAdapter extends ArrayAdapter<String> {

    private Context context;
    private ArrayList<String> array;

    public MyListAdapter(Context ctx, ArrayList<String> mArray) {
        super(ctx, 0, mArray);
        this.context = ctx;
        this.array = mArray; 
    }

    //other methods
}

adapter.notifyDataSetChanged(); 可以更新数据集和listView更新!

但如果您喜欢以下内容:

 public class MyListAdapter extends ArrayAdapter<String> {

    private Context context;
    private ArrayList<String> array;

    public MyListAdapter(Context ctx, ArrayList<String> mArray) {
        super(ctx, 0, mArray);
        this.context = ctx;
        for (int i = 0; i < mArray.size(); i++) // copy global ArrayList
            this.array.add(mArray.get(i));
    }

    //other methods
}

首先,您必须创建一个方法来更新适配器的localDataset然后调用this.adapter.notifyDataSetChanged();

如下所示:

 public void newDataReceived(ArrayList<String> newArr){
            this.array.clear();
            for (int i = 0; i < newArr.size(); i++)
                this.array.add(newArr.get(i));

            this.notifyDataSetChanged();
        }

并在收到新数据时调用适配器方法!

 sites.add("new site ");
    adapter.newDataReceived(sites); 

代码是手动输入的,可能有错误的语法,我希望它可以帮助你

答案 1 :(得分:0)

尝试使用ArrayList,然后在需要在该arrayList中添加新URL时添加。然后调用adapter.notifyDataSetChanged()

如果您知道可能需要更新值

,请不要使用final