Open top menu


In this Blog explain how to insert multiple value zero Index in array.

Add a multiple values zero index in ArrayList . First you can create Item class and create a  private type variable inside class as per your need  and generate getter/setter for Example:-op[


public class MultipleValues  {

   
private  String Value_one;
   
private  String Value_two;
   
private  String Value_three;
   
private  String Value_four;
   
private  String Value_five;
   
// Create class constructor................................
   
public MultipleValues(){}
   
// Create class Parameter type constructor................................
   
public MultipleValues(String Value_one,String Value_two, String Value_three,
        StringValue_four,String Value_five){
       
this.Value_one=Value_one;
       
this.Value_two=Value_two;
       
this.Value_three=Value_three;
       
this.Value_four=Value_four;
       
this.Value_five=Value_five;
    }

   
public String getValue_one() {
       
return Value_one;
    }

   
public void setValue_one(String value_one) {
       
Value_one = value_one;
    }

   
public String getValue_two() {
       
return Value_two;
    }

   
public void setValue_two(String value_two) {
       
Value_two = value_two;
    }

   
public String getValue_three() {
       
return Value_three;
    }

   
public void setValue_three(String value_three) {
       
Value_three = value_three;
    }

   
public String getValue_four() {
       
return Value_four;
    }

   
public void setValue_four(String value_four) {
       
Value_four = value_four;
    }

   
public String getValue_five() {
       
return Value_five;
    }

   
public void setValue_five(String value_five) {
       
Value_five = value_five;
    }
}

...................................................................................................................................................................



activity_main.xml
...................................................................................................................................................................
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin">
    <TextView        android:id="@+id/textView2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Android Beginner point"        android:textStyle="bold"        android:textColor="#066001"        android:textSize="24dp"/>
    <Button
        android:id="@+id/button1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginTop="54dp"        android:text="GetValue 1" />
    <TextView        android:id="@+id/textView1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginEnd="163dp"        android:text=""        android:textStyle="bold"        android:textColor="@color/colorPrimary"        android:textSize="24dp"/>

    <Button
        android:id="@+id/button5"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginTop="54dp"        android:text="GetValue 5"       />
    <TextView        android:id="@+id/textView5"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginEnd="163dp"        android:text=""        android:textStyle="bold"        android:textColor="@color/colorAccent"        android:textSize="24dp"/>
</LinearLayout>
...................................................................................................................................................................



MainActivity.Java

In this class explain how to add multiple value in array and how to get particular string value from array.
...................................................................................................................................................................
public class MainActivity extends AppCompatActivity  implements View.OnClickListener{

    ArrayList<MultipleValues> arrayList;
    TextView text1,text5;
    Button button1,button5;
    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        text1=(TextView)findViewById(R.id.textView1);
        text5=(TextView)findViewById(R.id.textView5);
        button1=(Button)findViewById(R.id.button1);
        button1.setOnClickListener(this);
        button5=(Button)findViewById(R.id.button5);
        button5.setOnClickListener(this);
        arrayList = new ArrayList<MultipleValues>();

        
//add multiple value one by one.......................       
       MultipleValues values=new MultipleValues();

        values.setValue_one("your data 1");
        values.setValue_two("your data 2");
        values.setValue_three("your data 3");
        values.setValue_four("your data 4");
        values.setValue_five("your data 5");

        arrayList.add(values);// add value 0 index        
       // or        
        arrayList.set(0,values); // replaced zero index

You can add multiple value at time.
arrayList.add(new MultipleValues("your data 1","your data 2","your data 3",
 "your data 4","your data 5")); 




  }
    @Override    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button1:
                MultipleValues valuefromarray=arrayList.get(0);
                String str1=valuefromarray.getValue_one();
                text1.setText(str1);

                break;

            case R.id.button5:
                MultipleValues valuefromarray5=arrayList.get(0);
                String str5=valuefromarray5.getValue_five();
                text5.setText(str5);
                break;
        }
    }
}

 ................................................................................................................................................................


0 comments