如何在活动中单击按钮并将项目添加到列表视图而无需编辑文本

时间:2019-05-16 21:46:39

标签: android listview

我需要单击产品活动中的按钮,然后将名称和价格添加到片段上的列表视图中,我只想纠正代码中想要的文本,不需要编辑文本。

这是活动中带有按钮的代码:

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

        BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);


        butao_add = (Button) findViewById(R.id.adicionar_carrinho);

        View.OnClickListener adicionarL = new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        };



    }

片段中具有listview的代码:

package diogo_pedro.inovetech2.android.inove_tech;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;


/**
 * A simple {@link Fragment} subclass.
 */
public class CarrinhoFrag extends Fragment {

    ListView lv2;
    String [] st = new String[] {"Ola", "Eu", "Sou", "O", "Pedro"};



    public CarrinhoFrag() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v =inflater.inflate(R.layout.fragment_carrinho, container, false);



        lv2 = (ListView) v.findViewById(R.id.listCompras_id);

        ArrayAdapter ad = new ArrayAdapter(this.getActivity(), android.R.layout.simple_list_item_multiple_choice, st);

        lv2.setAdapter(ad);


        return v;
    }


}

0 个答案:

没有答案