简体   繁体   中英

Add items from arraylist to listview on button click

I have an arraylist List<Cars> myCars = new ArrayList<Cars>(); My MainActivity layout has a listview and a button. There is one more layout ("each_car.xml") and its content is as below:-

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

        <TextView
            android:id="@+id/tv_carName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" />

        <TextView
            android:id="@+id/tv_carMaker"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" />
    </LinearLayout>
</LinearLayout>

My Cars class has two fields,"name" and "manufacturer".

public class Cars{
    String name;
    String manufacturer;
}

I am populating this list(myCars) through an async-task. After the task is complete ie inside onPostExecute(), I am calling a method in my MainActivity which will populate a listview with the items in the list. Below is the method :-

public void populateListView(List<Cars> data){

    ArrayAdapter<Cars> carAdapter = new CarAdapter(MainActivity.this, data);
    ListView dataList = (ListView) findViewById(R.id.car_listView);
    dataList.setAdapter(carAdapter);    
}

Below is my CarAdapter class :-

public class CarAdapter extends ArrayAdapter<Cars>{

    private LayoutInflater inflater;
    List<Cars> dummyData = new ArrayList<Cars>();
    MainActivity host;

    public CarAdapter(MainActivity host, List<Cars> data)
    {
        super(host,R.layout.each_car,data);
        inflater = host.getWindow().getLayoutInflater();
        dummyData = data;

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {

        View itemView = convertView;

        if(itemView == null)
        {
            itemView = inflater.inflate(R.layout.each_car,parent,false);
        }

        Cars currentData = dummyData.get(position);
        Log.d("Testing Position","Position :- "+position);
        TextView carName = (TextView) itemView.findViewById(R.id.tv_carName);
        carName.setText(currentData.getName());
        TextView carMaker = (TextView) itemView.findViewById(R.id.tv_carMaker);
        carMaker.setText(currentData.getManufacturer());
        return itemView;
    }
}

This is filling out the listview with the entire List (myCars) at once.

Problem :- I want to populate the listView with one item at a time when I click the button.I am not sure how should I modify my code.

create a button on main layout. Implement onclicklistener for the button and call populateListView() inside the onlick method.

Creat a new list. When you click a button add an item to a new list. call notifydatasetchanged(). When you click a button check whether the item is already added in the new list or not. Use newly created list in your adapter so that every time new item will be added.

单击按钮并调用carAdapter.notifyDataSetChanged()时,一次在列表中添加一项。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM