part 29 : Custom ArrayList Adapter, mContext Instance Of

 CustomListView.java : 

package first.learn.androidlearningcourse.Listview_Simple;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.os.Bundle;
import android.widget.ListView;

import java.util.ArrayList;

import first.learn.androidlearningcourse.R;

public class CustomListview extends AppCompatActivity {
ListView listView2;
Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_custom_listview);

mContext = this;


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



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

listView2.setAdapter(customAdapter);




}
}


CheckInstance.java

package first.learn.androidlearningcourse.Listview_Simple;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.os.Bundle;
import android.widget.ListView;

import java.util.ArrayList;

import first.learn.androidlearningcourse.R;

public class CheckInstance extends AppCompatActivity {

ListView listView2;
Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_custom_listview);

mContext = this;


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

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

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

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



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

listView2.setAdapter(customAdapter);




}
}




activity_custom_listview. 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">



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


custom_list_item : 

<?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">

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

<TextView
android:padding="10dp"
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="10dp"
android:text="blah blah"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tvTwo"/>



</LinearLayout>

</LinearLayout>


CustomAdapter.java : 


package first.learn.androidlearningcourse.Listview_Simple;

import android.app.AlertDialog;
import android.content.Context;
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 java.util.List;

import first.learn.androidlearningcourse.R;

public class CustomAdapter extends ArrayAdapter<String> {


ArrayList<String> list ;
Context mContext;

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

this.list = 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);

if(list.size()>position){

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

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

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("You Clicked: ")
.setMessage(ar[1])
.setPositiveButton("Ok", null)
.create().show();
}

if(mContext instanceof CheckInstance) {

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

}
});


}



return view ;
}



}












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