Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/194.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_Xml - Fatal编程技术网

Android 如何通过单击按钮将相同的listview相乘

Android 如何通过单击按钮将相同的listview相乘,android,xml,Android,Xml,我的ListItem的屏幕截图也是如此。我在另一个XML布局上有一个floatingAction按钮。每次单击floatingActionButton时,我都希望用相同的listItem一个接一个地填充我的布局。我知道我必须为此使用for循环,但我一直在思考如何在每次单击按钮时重复(重新创建?)相同的列表项。我不明白为什么ArrayAdapters是合适的。在我的主UI线程上,到目前为止,我只引用了我的小部件 这是我的listItem XML: <?xml version="1.0" en

我的ListItem的屏幕截图也是如此。我在另一个XML布局上有一个floatingAction按钮。每次单击floatingActionButton时,我都希望用相同的listItem一个接一个地填充我的布局。我知道我必须为此使用for循环,但我一直在思考如何在每次单击按钮时重复(重新创建?)相同的列表项。我不明白为什么ArrayAdapters是合适的。在我的主UI线程上,到目前为止,我只引用了我的小部件

这是我的listItem XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/semesterListItem"
              android:orientation="horizontal"
              android:layout_width="match_parent"
              android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="6dp"
        >
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Semester 1"
            android:layout_weight="4"
            android:textColor="#FFFFFF"
            android:background="#3F51B5"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="DELETE"
            android:layout_weight="0.6"
            android:clickable="true"
            android:textAlignment="center"
            />

    </LinearLayout>

</LinearLayout>

这是我的activity_main.xml

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:background="#FFFFFF"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context="myapp.onur.journeygpacalculator.MainActivity">

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="2dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        >

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/addActionButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="16dp"
            android:clickable="true"
            app:borderWidth="0dp"
            android:src="@drawable/ic_add_black_24dp" />
    </LinearLayout>

</RelativeLayout>

试试这段代码,我假设您已经准备好了要加载到listView的数据数组。当您点击该按钮(或到达listView的末尾)时,将使用相同或不同的数据加载更多数据

public class MyClass extends Activity{

private ListView _lv;
private Button _btn;

//otherviews and stuff

protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  super.setContentView(R.layout.mylayout);

  _btn = (Button)findViewById(R.id.button);
  _lv = (ListView)findViewById(R.id.listView); //listView in your layout 

       /*
          _arrayAdapter to load the data into your listView,
          if you want to load data from a webservice you'll
          have to make a custom adapter and replace like this
          MyAdapter _myAdapter = new MyAdapter(this, dataList);

       */

           ArrayAdapter<String> _arrayAdapter = new ArrayAdapter<String>(
             this, 
             android.R.layout.simple_list_item_1,
             your_array_list ); 
           // replace your_array_list with your array of data

     _lv.setAdapter(_arrayAdapter); 

     //button to add more data to listview
     _btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
         //add another row to the list
          _arrayAdapter = new ArrayAdapter<String>(
             this, 
             android.R.layout.simple_list_item_1,
             your_array_list ); 
          /* if you've more than one array you can 
             load the next array of data by replacing 
             your_array_list with new array here.*/

         _arrayAdapter.notifyDataSetChanged(); 
         // notify the adapter to update list with new data. 
         // otherwise the new data won't show up. 

         });   
   }
公共类MyClass扩展活动{
私有列表视图_lv;
私人按钮_btn;
//其他的观点和东西
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
super.setContentView(R.layout.mylayout);
_btn=(按钮)findViewById(R.id.Button);
_lv=(ListView)findViewById(R.id.ListView);//布局中的ListView
/*
_arrayAdapter将数据加载到listView中,
如果您想从Web服务加载数据,您需要
必须制作一个自定义适配器,然后像这样更换
MyAdapter _MyAdapter=新的MyAdapter(此,数据列表);
*/
ArrayAdapter\u ArrayAdapter=新的ArrayAdapter(
这
android.R.layout.simple\u list\u item\u 1,
您的_数组_列表);
//用数据数组替换\u数组\u列表
_低压设置适配器(_arrayAdapter);
//按钮向listview添加更多数据
_btn.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
//将另一行添加到列表中
_arrayAdapter=新的arrayAdapter(
这
android.R.layout.simple\u list\u item\u 1,
您的_数组_列表);
/*如果有多个数组,则可以
通过替换来加载下一个数据数组
您的\u数组\u列表中有新数组*/
_arrayAdapter.notifyDataSetChanged();
//通知适配器使用新数据更新列表。
//否则,新数据将不会显示。
});   
}

just call在刷新适配器之前不要清除arraylist,您将获得附加到listview的相同项。如何?我从未见过。粘贴适配器类没有数据。上面的屏幕截图显示了一个listitem。我想在每次单击floatactionbutton时添加另一个列表项。