After creating simple ListView, android also provides facilities to customize our ListView.
As the simple ListView, custom ListView also uses Adapter classes which added the content from data source (such as string array, array, database etc). Adapter bridges data between an AdapterViews and other Views
MainActivity.java
package com.example.basleenkaur.lvcustom; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.ListView; import java.util.ArrayList; public class MainActivity extends AppCompatActivity { ListView l1; ArrayList alplaces; ArrayList<Integer> image; second1 ad; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); l1=(ListView) findViewById(R.id.listview); alplaces=new ArrayList(); image=new ArrayList(); alplaces.add("Mysore"); //alplaces.add("chd"); // alplaces.add("Banglore"); //alplaces.add("Ldh"); image.add(R.drawable.download); ad=new second1(getApplicationContext(),alplaces,image); l1.setAdapter(ad); } }
Adapater.java
package com.example.basleenkaur.lvcustom; import android.content.Context; import android.support.annotation.NonNull; import android.support.annotation.Nullable; 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 java.util.ArrayList; /** * Created by basleenkaur on 2018-07-25. */ public class second1 extends ArrayAdapter { ArrayList alplace1; ArrayList<Integer> images; Context cntx; public second1(@NonNull Context context, ArrayList alplace,ArrayList<Integer> imag) { super(context, R.layout.layout,alplace); alplace1 =alplace; images= imag; cntx=context; } @NonNull @Override public View getView(int position, @Nullable View convertView, @Nullable ViewGroup parent) { LayoutInflater inflater=(LayoutInflater) cntx.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View v=inflater.inflate(R.layout.layout,null); TextView tv=(TextView)v.findViewById(R.id.textView); tv.setText(alplace1.get(position).toString()); ImageView immgv=(ImageView)v.findViewById(R.id.imageView); immgv.setImageResource(images.get(position)); return v; } }
Comments
Post a Comment