I have two button inside mylist.xml which is inserted over listview in main.xml using arrayadapter.It is showing me the data in application but only click functionality is not working. Not be able to click list and both buttons.
activity_main.xml
( In this file I have just made listview using xml in which my custom list "mylist.xml" is going to inserted. )
<?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=".MainActivity">
<ListView
android:id="@+id/lv"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
mylist.xml
( In this file all the content of listview lv which is there on mainactivity.These file need to be inserted in that listview. )
<?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">
<TextView
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginTop="10dp"
android:id="@+id/s"
android:gravity="center_horizontal"
android:textSize="20dp"
android:textStyle="bold"/>
<TextView
android:layout_width="match_parent"
android:layout_height="25dp"
android:id="@+id/c"
android:textSize="15dp"
android:textStyle="bold"/>
<TextView
android:layout_width="match_parent"
android:layout_height="55dp"
android:id="@+id/d"
android:textSize="15dp"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="left">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/b1"
android:text="UPDATE"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/b2"
android:text="DELETE"/>
</LinearLayout>
</LinearLayout>
MainActivity Class
public class MainActivity extends AppCompatActivity
{
String S1[]={"hello","1","2"},C1[]={"ello2","1","2"},D1[]={"hello3","3","4"};
Button b1,b2;
ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv=findViewById(R.id.lv);
b1=findViewById(R.id.b1);
b2=findViewById(R.id.b2);
// display data
CustomAdapter myAdapter=new CustomAdapter(this,S1,C1,D1);
lv.setAdapter(myAdapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this,"ello"+position,Toast.LENGTH_LONG).show();
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"ello",Toast.LENGTH_LONG).show();
}
});
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"ello",Toast.LENGTH_LONG).show();
}
});
}
});
}
}
CustomAdapter class
public class CustomAdapter extends ArrayAdapter<String>
{
private final Activity context;
private final String Subject1[],Chapter1[],Details1[];
public CustomAdapter(Activity context,String Subject1[], String Chapter1[], String Details1[])
{
super(context,R.layout.mylist,Subject1);
this.context=context;
this.Subject1=Subject1;
this.Chapter1=Chapter1;
this.Details1=Details1;
}
public View getView(int position, View view, ViewGroup parent)
{
LayoutInflater inflater=context.getLayoutInflater();
view=inflater.inflate(R.layout.mylist, null);
TextView s =view.findViewById(R.id.s);
TextView c= view.findViewById(R.id.c);
TextView d= view.findViewById(R.id.d);
s.setText(Subject1[position]);
c.setText(Chapter1[position]);
d.setText(Details1[position]);
return view;
};
}