part 31 : LocalBroadCast Manager

 CustomListview.java : 

package first.learn.androidlearningcourse.Listview_Simple;

import androidx.appcompat.app.AppCompatActivity;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
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, btnListSize;

ArrayList<String> list1 ;
@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);

btnListSize = (Button)findViewById(R.id.btnListSize);







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));

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();

btnListSize.setText("List Size : "+list1.size());

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));
}
});

}
});


btnListSize.setText("List Size : "+list1.size());
btnListSize.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

new AlertDialog.Builder(mContext)
.setTitle("List Size")
.setMessage("List Size : "+ list1.size())
.create().show();
}
});


}



BroadcastReceiver adapterReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {

String s = intent.getStringExtra("deleteOneData");

if(s!=null){

btnListSize.setText("List Size : "+list1.size());

String position = intent.getStringExtra("position");

int n = -1;

n = Integer.parseInt(position);

System.out.println("Deleted Position : "+ n);
}

}
};


@Override
protected void onResume() {
super.onResume();

LocalBroadcastManager.getInstance(mContext).registerReceiver(adapterReceiver, new IntentFilter("RowDeleteIntent"));

}


@Override
protected void onDestroy() {
super.onDestroy();
LocalBroadcastManager.getInstance(mContext).unregisterReceiver(adapterReceiver);

}
}


activiti_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"/>


<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="List Size"
android:id="@+id/btnListSize"/>



<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.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;

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);
ImageView btnDelete = (ImageView) view.findViewById(R.id.btnDelete);


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();
}
});

btnDelete.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();

Intent intent = new Intent("RowDeleteIntent");
intent.putExtra("deleteOneData", "deleteOneData");
intent.putExtra("position", String.valueOf(position) );
LocalBroadcastManager.getInstance(mContext).sendBroadcast(intent);


}
})
.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:weightSum="10"
android:orientation="horizontal"
>
<LinearLayout
android:layout_weight="8"
android:layout_width="0dp"
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:padding="2dp"
android:text="blah blah"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tvThree"/>



</LinearLayout>


<LinearLayout

android:gravity="center"
android:layout_gravity="center"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:orientation="vertical">

<ImageView
android:id="@+id/btnDelete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@android:drawable/ic_delete"/>

</LinearLayout>



</LinearLayout>





</LinearLayout>




No comments:

Post a Comment