part 30 : Add Element To String []

 customListview.java 

package first.learn.androidlearningcourse.Listview_Simple;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.Arrays;

import first.learn.androidlearningcourse.R;

public class CustomListview extends AppCompatActivity {
ListView listView2;
Context mContext;
EditText etName,
etAge,
etAddress;
Button btnAdd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_custom_listview);

mContext = this;

etName= (EditText)findViewById(R.id.etName);
etAge= (EditText)findViewById(R.id.etAge);
etAddress= (EditText)findViewById(R.id.etAddress);
btnAdd = (Button)findViewById(R.id.btnAdd);





listView2 = (ListView) findViewById(R.id.listView2);

ArrayList<String> list = new ArrayList<>();

for(int i=0; i<100; i++){

list.add("Position : "+i + "# Size Number : "+(i+1));
}


String [] array = {"Name : Noor Hossain#Age: 49#Address: Shibchar","Name : Ruhul Amin#Age: 27#Address: Munshi Ganj", "Name : Ahsanul Islam#Age: 38#Address: Rangpur" };


// ArrayList<String> list1 = new ArrayList<>(Arrays.asList(array));

ArrayList<String> list1 = new ArrayList<>();

for(int i = 0; i<array.length; i++){
list1.add(array[i]);
}





// CustomAdapter customAdapter = new CustomAdapter(mContext, R.layout.custom_list_item, list );


CustomAdapter customAdapter = new CustomAdapter(mContext, R.layout.custom_list_item, list1);
listView2.setAdapter(customAdapter);


btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = etName.getText().toString();
String age = etAge.getText().toString();
String address = etAddress.getText().toString();

list1.add("Name : "+name+"#Age : "+age+ "#Address : "+address);

//customAdapter.add("Name : "+name+"#Age : "+age+ "#Address : "+address);

customAdapter.notifyDataSetChanged();

new Handler().post(new Runnable() {
@Override
public void run() {
customAdapter.notifyDataSetChanged();
}
});

listView2.setSelection((list1.size()-1));

listView2.post(new Runnable() {
@Override
public void run() {
listView2.setSelection((list1.size()-1));
}
});

}
});


}
}


activity_custom_list_view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Listview_Simple.SimpleListview"
android:orientation="vertical"
android:layout_margin="16dp">

<EditText
android:hint="Name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/etName"
/>

<EditText
android:hint="age"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/etAge"
/>

<EditText
android:hint="Address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/etAddress"
/>

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add To List"
android:id="@+id/btnAdd"/>


<ListView
android:fastScrollAlwaysVisible="false"
android:scrollbars="none"
android:layout_marginTop="20dp"
android:id="@+id/listView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>



</LinearLayout>


CustomAdapter.java

package first.learn.androidlearningcourse.Listview_Simple;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.ArrayList;

import first.learn.androidlearningcourse.R;

public class CustomAdapter extends ArrayAdapter<String> {


ArrayList<String> mainlist = null ;
String [] mainArray = null ;
Context mContext;

public CustomAdapter(@NonNull Context context, int resource, @NonNull ArrayList<String> objects) {
super(context, resource, objects);

this.mainlist = objects;
this.mContext = context ;
}


public CustomAdapter(@NonNull Context context, int resource, @NonNull String [] objects) {
super(context, resource, objects);

this.mainArray = objects;
this.mContext = context ;
}



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

View view ;

if(convertView == null){
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.custom_list_item, null);
}else {

view = convertView;
}

TextView tvOne = (TextView)view.findViewById(R.id.tvOne);
TextView tvTwo = (TextView)view.findViewById(R.id.tvTwo);
TextView tvThree = (TextView)view.findViewById(R.id.tvThree);


if(mainlist!=null&&mainlist.size()>position){

tvThree.setVisibility(View.VISIBLE);
String s = mainlist.get(position);
String [] ar = s.split("#");

if(ar.length>2) {
tvOne.setText(ar[0]);
tvTwo.setText(ar[1]);
tvThree.setText(ar[2]);
}

tvOne.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(mContext, "You Clicked On : "+ ar[0], Toast.LENGTH_SHORT).show();
}
});

tvTwo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

if(mContext instanceof CustomListview) {

new AlertDialog.Builder(mContext)
.setTitle("Do You Really Want To Delete The Item ")
.setMessage(ar[1])
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {

mainlist.remove(position);
notifyDataSetChanged();

}
})
.create().show();
}

if(mContext instanceof CheckInstance) {

new AlertDialog.Builder(mContext)
.setTitle("You Clicked Instance Of: ")
.setMessage(ar[1])
.setPositiveButton("Cancel", null)
.create().show();
}

}
});


}



if(mainArray!=null&&mainArray.length>position){
tvThree.setVisibility(View.VISIBLE);

String s = mainArray[position];

String [] ar = s.split("#");

if(ar.length>2) {
tvOne.setText(ar[0]);
tvTwo.setText(ar[1]);
tvThree.setText(ar[2]);
}


}



return view ;
}



}


custom_list_item.xml


<?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"
android:layout_margin="15dp">




<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:orientation="vertical">

<TextView
android:padding="2dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tvOne"
android:textStyle="bold"
android:textSize="16dp"
android:text=";skdj;lkfa slk;dfja"
android:textColor="@color/purple_700"/>


<TextView
android:padding="2dp"
android:text="blah blah"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tvTwo"/>

<TextView
android:visibility="gone"
android:padding="2dp"
android:text="blah blah"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tvThree"/>



</LinearLayout>

</LinearLayout>





No comments:

Post a Comment

Featured Post

Lesson 1 : Android Studio : DownLoad And Install and First App

বিসমিল্লাহির রাহমানির রাহীম Lesson 1 : Android Studio : DownLoad And Install  and First App এই লেসনটির ভিডিও ইউটিউবে দেখতে এখানে ক্ল...

Popular Posts