Add Items to ListView - Android

后端 未结 3 780
终归单人心
终归单人心 2020-12-05 11:16

This is my first experience with android. I\'m trying to add items to my ListView. I use Tabs, and the only way to see that the item was added is to change tab and then come

相关标签:
3条回答
  • 2020-12-05 11:42
    ListView myListView = (ListView) rootView.findViewById(R.id.myListView);
    ArrayList<String> myStringArray1 = new ArrayList<String>();
    myStringArray1.add("something");
    adapter = new CustomAdapter(getActivity(), R.layout.row, myStringArray1);
    myListView.setAdapter(adapter);
    

    Try it like this

    public OnClickListener moreListener = new OnClickListener() {
    
        @Override
        public void onClick(View v) {
            adapter = null;
            myStringArray1.add("Andrea");
            adapter = new CustomAdapter(getActivity(), R.layout.row, myStringArray1);
            myListView.setAdapter(adapter);
            adapter.notifyDataSetChanged();
        }       
    };
    
    0 讨论(0)
  • 2020-12-05 11:46
    public OnClickListener moreListener = new OnClickListener() {
    
        @Override
          public void onClick(View v) {
              adapter.add("aaaa")
          }
    }
    
    0 讨论(0)
  • 2020-12-05 11:56

    Try this one it will work

    public class Third extends ListActivity {
    private ArrayAdapter<String> adapter;
    private List<String> liste;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_third);
         String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
                    "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
                    "Linux", "OS/2" };
         liste = new ArrayList<String>();
         Collections.addAll(liste, values);
         adapter = new ArrayAdapter<String>(this,
                    android.R.layout.simple_list_item_1, liste);
         setListAdapter(adapter);
    }
     @Override
      protected void onListItemClick(ListView l, View v, int position, long id) {
         liste.add("Nokia");
         adapter.notifyDataSetChanged();
      }
    }
    
    0 讨论(0)
提交回复
热议问题