Create the New Project
Ex. listviewProject
activity_main.xml
Paste the Below Code
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/listview"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<TextView
android:id="@+id/NoData"
android:text="No Data"
android:layout_gravity="center"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
and Create the New XML File in android studio
right click on layout folder then click New then click Layout Resource File
Paste the below code
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/Clicked"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/b1"
android:text="Loading..."
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.constraint.ConstraintLayout></per>
Then Creating the 4 class file in below
1) Tutorial
2) TutorialAdapter
3) TutorialData
4) TutorialHolder
1) Tutorial File Paste the Below Code
<pre>
import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
public class Tutorial{
Context c;
ListView listView;
Activity a;
TutorialAdapter chettingAdapter;
TextView CNoData;
public Tutorial(Context c, ListView listView, Activity a, TextView CNoData) {
this.c = c;
this.listView = listView;
this.a = a;
this.CNoData = CNoData;
}
public void getUpdate(ArrayList<TutorialData> chettingDatas)
{
if(chettingDatas.size()>0)
{
CNoData.setVisibility(View.GONE);
listView.setVisibility(View.VISIBLE);
chettingAdapter = new TutorialAdapter(c,a,chettingDatas);
listView.setAdapter((ListAdapter)chettingAdapter);
}
else
{
CNoData.setVisibility(View.VISIBLE);
CNoData.setText("No Data");
listView.setVisibility(View.GONE);
}
}
}
</per>
2) TutorialAdapter File Paste the Below Code
<pre>
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Toast;
import java.util.ArrayList;
public class TutorialAdapter extends BaseAdapter {
Context c;
Activity a;
ArrayList<TutorialData> chettingDatas;
LayoutInflater layoutInflater;
public TutorialAdapter(Context c, Activity a, ArrayList<TutorialData> chettingDatas) {
this.c = c;
this.a = a;
this.chettingDatas = chettingDatas;
}
@Override
public int getCount() {
return chettingDatas.size();
}
@Override
public Object getItem(int position) {
return chettingDatas.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (layoutInflater == null) {
layoutInflater = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
if (convertView == null)
{
convertView= layoutInflater.inflate(R.layout.layout_tutorial,parent,false);
}
TutorialHolder Holder = new TutorialHolder(convertView);
Holder.b1.setText(chettingDatas.get(position).getData());
Holder.b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(c, "Clicked : "+chettingDatas.get(position).getData(), Toast.LENGTH_SHORT).show();
}
});
return convertView;
}
}
3) TutorialData File Paste the Below Code
public class TutorialData {
private String data;
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
}
4) TutorialHolder File Paste the Below Code
public class TutorialHolder {
Button b1;
TutorialHolder(View v)
{
b1 = (Button)v.findViewById(R.id.b1);
}
}
Then the Last Code is MainActivity.java paste the below code
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listview = (ListView)findViewById(R.id.listview);
TextView NoData = (TextView)findViewById(R.id.NoData);
ArrayList<TutorialData> chettingDatas= new ArrayList<>();
TutorialData T = new TutorialData();
T.setData("Hello");
chettingDatas.add(T);
TutorialData T1 = new TutorialData();
T1.setData("Bro");
chettingDatas.add(T1);
TutorialData T2 = new TutorialData();
T2.setData("Hi");
chettingDatas.add(T2);
TutorialData T3 = new TutorialData();
T3.setData("Buy");
chettingDatas.add(T3);
Tutorial Tu = new Tutorial(MainActivity.this,listview,MainActivity.this,NoData);
Tu.getUpdate(chettingDatas);
}
}
Below Comment Box to i am sending the tested apk file send you can check the working...
Good Buy...