Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 向Listview顶部添加元素_Android_Android Listview - Fatal编程技术网

Android 向Listview顶部添加元素

Android 向Listview顶部添加元素,android,android-listview,Android,Android Listview,我正在开发一个应用程序,其功能如下: 在给定URL的情况下,从internet加载10个图像 开始时,10个URL位于传递给我的baseadapter类的arraylist中。在列表视图的顶部有两个按钮 之后,在顶部添加一个图像(按钮2),在底部添加一个图像(按钮1)。我似乎无法将图像添加到listview的开头(R.Id.Button2下的代码) 它当前只在底部放置一个图像。 public class MainActivity extends Activity implements

我正在开发一个应用程序,其功能如下:

  • 在给定URL的情况下,从internet加载10个图像

  • 开始时,10个URL位于传递给我的baseadapter类的arraylist中。在列表视图的顶部有两个按钮

  • 之后,在顶部添加一个图像(按钮2),在底部添加一个图像(按钮1)。我似乎无法将图像添加到listview的开头(R.Id.Button2下的代码)

  • 它当前只在底部放置一个图像。

         public class MainActivity extends Activity implements OnClickListener {
    
    String dirPath;
    MyAdapter Ada;
    Button B,B2;
     ListView L;
     ArrayList<String> data= new ArrayList<String>();
     ArrayList<String> FirstList = new ArrayList<String>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        B= (Button)findViewById(R.id.button1);
        B2 =(Button)findViewById(R.id.button2);
         dirPath = getFilesDir().getAbsolutePath() + File.separator + "newfoldername";
    
        FirstList.add("url 1");
        FirstList.add("url 2");
        FirstList.add("url 3");
        FirstList.add("url 4");
        FirstList.add("url 5");
        FirstList.add("url 6");
        FirstList.add("url 7");
        FirstList.add("url 8");
        FirstList.add("url 9");
        FirstList.add("url 10);
        L=(ListView) findViewById(R.id.listView1);
        B.setOnClickListener(this);
        B2.setOnClickListener(this);
        data= (ArrayList<String>) FirstList.clone();
    
    
    
         Ada = new MyAdapter(MainActivity.this,data,dirPath);
        L.setAdapter(Ada);
    
    
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
    
    
    
        switch(v.getId())
        {
           case R.id.button1:
               data.add("url 11");
               Ada.notifyDataSetChanged();
    
            break;
    
           case R.id.button2:
               ArrayList<String> n = new ArrayList<String>();
               n.add("url 12");
    
                 n.addAll(data);
    
    
                Ada = new MyAdapter(MainActivity.this,n,dirPath);
                L.setAdapter(Ada);
                Ada.notifyDataSetChanged();
    
    
               break;
    
    
    
    
    
        }
    
     }
    
    
    
       }
    
    public类MainActivity扩展活动实现OnClickListener{
    字符串路径;
    Myada;
    按钮B,B2;
    列表视图L;
    ArrayList数据=新的ArrayList();
    ArrayList FirstList=新的ArrayList();
    @凌驾
    创建时受保护的void(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    B=(按钮)findViewById(R.id.button1);
    B2=(按钮)findViewById(R.id.button2);
    dirPath=getFilesDir().getAbsolutePath()+File.separator+“newfoldername”;
    添加(“url 1”);
    添加(“url 2”);
    添加(“url 3”);
    添加(“url 4”);
    添加(“url 5”);
    添加(“url 6”);
    添加(“url 7”);
    添加(“url 8”);
    添加(“url 9”);
    添加(“url 10”);
    L=(ListView)findViewById(R.id.listView1);
    B.setOnClickListener(本);
    B2.setOnClickListener(本);
    data=(ArrayList)FirstList.clone();
    Ada=新的MyAdapter(MainActivity.this、data、dirPath);
    L.setAdapter(Ada);
    }
    @凌驾
    公共布尔onCreateOptions菜单(菜单){
    //为菜单充气;这会将项目添加到操作栏(如果存在)。
    getMenuInflater().充气(R.menu.main,menu);
    返回true;
    }
    @凌驾
    公共void onClick(视图v){
    //TODO自动生成的方法存根
    开关(v.getId())
    {
    案例R.id.button1:
    数据。添加(“url 11”);
    Ada.notifyDataSetChanged();
    打破
    案例R.id.按钮2:
    ArrayList n=新的ArrayList();
    n、 添加(“url 12”);
    n、 addAll(数据);
    Ada=新的MyAdapter(MainActivity.this,n,dirPath);
    L.setAdapter(Ada);
    Ada.notifyDataSetChanged();
    打破
    }
    }
    }
    
    方法
    list.add()
    将始终在底部添加项目。要将项目添加到列表顶部,请尝试:

    1. Create an empty list, and add the new item into the list
    2. using addAll method of the new list created, add the first list 
    that has all the other elements
    3. Pass the new list that is populated to the adapter class, 
    or replace the old list with the new list
    
    结果: 您希望添加的项目现在将位于顶部


    希望这个提示能有所帮助!

    基本上,Rat-a-tat-a-tat Ratatouille的答案几乎是完整的,我在各个阶段唯一要做的改变是:

  • 创建一个空列表并向其中添加一个对象

  • 手动或使用
    addAll
    添加一组新对象

  • 定义要从底部堆叠的列表

  • 将新列表传递到适配器中

  • 调用适配器。notifyDatasetChanged()


  • 您可以在特定位置添加。从数据库中获取数据时,使用2 arg.add列的add()并按日期排序。ArrayList n=new ArrayList();n.add(“url 12”);n.addAll(data);data.clear();data=(ArrayList)n.clone();Ada=new MyAdapter(MainActivity.this,data,dirPath);L.setAdapter(Ada);Ada.notifyDataSetChanged();尝试记录在列表n中输入的值,找出错误所在,甚至可能是删除Ada.notify方法,甚至使用相同的arraylist数据,并使用方法arraylist.add(位置,值)我确实在n中记录了这些值,这是正确的。url的值的顺序是正确的。我只是显示了错误的图像