Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package com.example.mehade.customlistview;
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.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
public class ListViewAdapter extends BaseAdapter {
//variables
Context mContext;
List<Model> modellist;
LayoutInflater inflater;
ArrayList<Model> arrayList;
public ListViewAdapter(Context mContext, List<Model> modellist) {
this.mContext = mContext;
this.modellist = modellist;
inflater = LayoutInflater.from(mContext);
arrayList = new ArrayList<Model>();
arrayList.addAll(modellist);
}
public class ViewHolder {
TextView mTitleTv, mDescTv;
ImageView mIconIv;
}
@Override
public int getCount() {
return modellist.size();
}
@Override
public Object getItem(int i) {
return modellist.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder holder;
if (view == null) {
holder = new ViewHolder();
view = inflater.inflate(R.layout.row, null);
holder.mTitleTv = view.findViewById(R.id.mainTitle);
holder.mDescTv = view.findViewById(R.id.mainDesc);
holder.mIconIv = view.findViewById(R.id.mainIcon);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
holder.mTitleTv.setText(modellist.get(i).getTitle());
holder.mDescTv.setText(modellist.get(i).getDesc());
holder.mIconIv.setImageResource(modellist.get(i).getIcon());
return view;
}
} // ListViewAdapter Class
package com.example.mehade.customlistview;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
//variables
ArrayList<Model> arrayList = new ArrayList<Model>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] title = new String[]{"OS","Camera","Battery", "Cpu", "Display","Ram", "Memory", "Sensor"};
String[] description = new String[]{"OS detail...","Camera detail...","Battery detail...", "Cpu detail...", "Display detail...","Ram detail...", "Memory detail...", "Sensor detail..."};
int[] icon = new int[]{
R.drawable.os,
R.drawable.camera,
R.drawable.battery,
R.drawable.cpu,
R.drawable.display,
R.drawable.ram,
R.drawable.memory,
R.drawable.sensor
};
for (int i = 0; i < title.length; i++) {
Model model = new Model(title[i], description[i], icon[i]);
//bind all strings/data in an array
arrayList.add(model);
//pass results to listViewAdapter class
ListViewAdapter adapter = new ListViewAdapter(this, arrayList);
//bind the adapter to the listview
ListView listView = findViewById(R.id.listView);
listView.setAdapter(adapter);
} //for loop
} //onCreate Method
} //MainActivity Class
package com.example.mehade.customlistview;
public class Model {
String title;
String desc;
int icon;
//constructor
public Model(String title, String desc, int icon) {
this.title = title;
this.desc = desc;
this.icon = icon;
} //Model constructor
//getters
public String getTitle() {
return this.title;
}
public String getDesc() {
return this.desc;
}
public int getIcon() {
return this.icon;
}
} //Model Class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.