如何将textview中的值添加到列表视图中

时间:2017-06-05 09:15:07

标签: android arrays string listview textview

我试图将textview中的值动态添加到listview。为此我创建了另一个布局来从用户获取值。但我无法做到这一点。我尝试了很多东西。请帮助..这是我的代码。 package com.example.chetan.assignment;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ListAdapter;
import android.widget.ListView;

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


public class MainActivity extends AppCompatActivity {
    private static int count = 0;


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


        // String[] name= {"Apple","Banana","Mango","PineApple"};
        //String[] price = {"Rs.40","Rs.60","Rs.70","Rs.80"};

        List<String> names = new ArrayList<String>();
        //String[] names = {"names"};
        List<String> prices = new ArrayList<String>();
        //String[] prices = {"prices"};

        String name = getIntent().getStringExtra("name");
        String price = getIntent().getStringExtra("price");
        if(count==0) {
            names.add(0, "name");
            prices.add(0, "price");
        }

        if (count>0) {
            names.add(count, name);
            prices.add(count,price);
        }
        count++;

        ListAdapter MyAdapter = new MyAdapter(names, prices, this);
        ListView theList = (ListView) findViewById(R.id.listView);

        theList.setAdapter(MyAdapter);
    }
    public void buttonClick(View view) {
        Intent i = new Intent(this, Addlayout.class);
        startActivity(i);
    }
}


        package com.example.chetan.assignment;




    package com.example.chetan.assignment;

    import android.content.Context;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ArrayAdapter;
    import android.widget.BaseAdapter;
    import android.widget.TextView;

    class MyAdapter extends BaseAdapter {

        String[] name;
        String[] price;
        Context context;
        private static LayoutInflater myInflater = null;

        public MyAdapter(String[] name, String[] price, Context context) {
            this.name = name;
            this.price = price;
            this.context = context;
            myInflater  = ( LayoutInflater )context.
                    getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }
        @Override
        public int getCount() {
            return name.length;
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }
        public class Holder
        {
            TextView nametext;
            TextView pricetext;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            Holder myholder = new Holder();
            View myViews = myInflater.inflate(R.layout.myview,parent,false);
            myholder.nametext= (TextView) myViews.findViewById(R.id.textView);
            myholder.pricetext= (TextView) myViews.findViewById(R.id.textView2);
            myholder.nametext.setText(name[position]);
            myholder.pricetext.setText(price[position]);

            return myViews;

        }

    }
    package com.example.chetan.assignment;

    import android.content.Intent;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.text.Editable;
    import android.view.View;
    import android.widget.EditText;
    import android.widget.Toast;

    public class Addlayout extends AppCompatActivity {


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

       public void Onclickbutton(View view){
            EditText nametext = (EditText)findViewById(R.id.editText);
            EditText pricetext = (EditText)findViewById(R.id.editText2);

            String namedata = nametext.getText().toString();
            String pricedata = pricetext.getText().toString();

            Intent intent = new Intent(this,MainActivity.class);
            intent.putExtra("name",namedata);
            intent.putExtra("price",pricedata);

           startActivity(intent);

        }
    }

1 个答案:

答案 0 :(得分:0)

    Here you can do something like this:
    Instead of simple listview I have used RecyclerView as it has better performance.

    //First create a Data class for name and price

    public class Data implements Serializable {

        private String name,price;

        public Data(){}

        public Data(String name,String price){
            this.name = name;
            this.price = price;
        }

        public String getPrice() {
            return price;
        }

        public void setPrice(String price) {
            this.price = price;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }


    //your Addlayout class 

    public class Addlayout extends AppCompatActivity {

        List<Data> dataList = new ArrayList<>();
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_addlayout);

        }

        public void Onclickbutton(View view){
            EditText nameText = (EditText)findViewById(R.id.editText);
            EditText priceText = (EditText)findViewById(R.id.editText2);

            String nameData = nameText.getText().toString();
            String priceData = priceText.getText().toString();
            Data data = new Data(nameData, priceData);
            dataList.add(data);
            Intent intent = new Intent(this,MainActivity.class);
            intent.putExtra("List", (Serializable) dataList);
            startActivity(intent);

        }
    }

    //add layout xml
    //change layout according to you

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/activity_addlayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="com.example.mmc.testproject.Addlayout">

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:inputType="textPersonName"
            android:ems="10"
            android:hint="name"
            android:layout_alignParentTop="true"
            android:layout_alignParentStart="true"
            android:layout_marginStart="13dp"
            android:id="@+id/editText" />

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:inputType="textPersonName"
            android:ems="10"
            android:hint="price"
            android:layout_below="@+id/editText"
            android:layout_alignEnd="@+id/editText"
            android:layout_marginTop="18dp"
            android:id="@+id/editText2" />

        <Button
            android:text="Button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/editText2"
            android:layout_alignStart="@+id/editText2"
            android:layout_marginStart="43dp"
            android:layout_marginTop="36dp"
            android:onClick="Onclickbutton"
            android:id="@+id/button" />
    </RelativeLayout>


    //Then your Listview Adapter

    public class ListViewAdaptor extends RecyclerView.Adapter<ListViewAdaptor.MyViewHolder> {
        private List<Data> mDataList;

        public class MyViewHolder extends RecyclerView.ViewHolder{
            public TextView name,price;

            public MyViewHolder(View view){
                super(view);
                name = (TextView) view.findViewById(R.id.name);
                price= (TextView) view.findViewById(R.id.price);
            }
        }

        public ListViewAdaptor(List<Data> dataList){
            this.mDataList = dataList;
        }


        @Override
        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View itemView = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.list_view_item, parent, false);

            return new MyViewHolder(itemView);
        }

        @Override
        public void onBindViewHolder(MyViewHolder holder, int position) {
            Data data = mDataList.get(position);
            holder.name.setText(data.getName());
            holder.price.setText(data.getPrice());
        }

        @Override
        public int getItemCount() {
            return mDataList.size();
        }
    }


    //your Listview layout to show name and price

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

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/name"
            android:text="name"
            android:gravity="center"
            android:textSize="26sp"
            android:layout_weight="1"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/price"
            android:text="price"
            android:gravity="center"
            android:textSize="26sp"
            android:layout_weight="1"/>

    </LinearLayout>


    //And finally the main Activity

    public class MainActivity extends AppCompatActivity {


        private RecyclerView mRecyclerView;
        private ListViewAdaptor mAdapter;
        private List<Data> mDataList;

        private static final String TAG = "MainActivity";
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
            Intent i = getIntent();
            mDataList = (List<Data>) i.getSerializableExtra("List");
            Log.e(TAG,"Datalist size : "+mDataList.size());
            mAdapter = new ListViewAdaptor(mDataList);
            RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
            mRecyclerView.setLayoutManager(mLayoutManager);
            mRecyclerView.setItemAnimator(new DefaultItemAnimator());
            mRecyclerView.setAdapter(mAdapter);
        }

    }

//and main Activity.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.mmc.testproject.MainActivity">

    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/recycler_view">

    </android.support.v7.widget.RecyclerView>

</RelativeLayout>

希望它有所帮助!!!