1

I m trying to add the value from textview dynamically to listview.For that I have created another layout to take the values from user. but I'm not able to do that. I have tried many things. Kindly help.. Here is my code. 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);

        }
    }
  • so what you are trying to achieve is take values of name and price from AddLayout class and show it in a listview in your MainActivity?? – sumit Jun 05 '17 at 09:22
  • Yes!! but I'm not able to deal with the strings – chetan ingle Jun 05 '17 at 09:38
  • in Android there are several issues in this simple task. persistence, lifecycle, sharing data between activity. 1st look at this https://stackoverflow.com/questions/4878159/whats-the-best-way-to-share-data-between-activities – burgyna Jun 05 '17 at 09:52
  • so one more question you are going to get one value at a time from user and then on button click you are going to show the list?? – sumit Jun 05 '17 at 10:26
  • yes, for now I'm doing that!! may be later I'll try to add multiple values at once. – chetan ingle Jun 05 '17 at 10:40
  • In the updated program,first as default it is showing two values in the listview, "name" and "price" and then when I add the values from editview it crashes – chetan ingle Jun 05 '17 at 10:47
  • thats because you have written a wrong code tell me one more more thing do you want to retain the previous values in the list or you just want to show the only the new name and price user will enter?? – sumit Jun 05 '17 at 10:58
  • It should have the previous value as well. The values should get add one after another as the user add it from Addlayout – chetan ingle Jun 05 '17 at 11:03

1 Answers1

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>

Hope it helps!!!

sumit
  • 1,047
  • 1
  • 10
  • 15