Cannot resolve layout in adapter for custom list view - android

I am trying to make a custom ListView and a adapter for that list, I am inflating the list view layout but it is giving an error saying
Cannot find symbol variable lv_grpMsg
Can someone tell me how to correct this error?
Here is the adapter code:
public class GrpMsgAdapter extends BaseAdapter{
private Fragment fragment;
private ArrayList data;
private static LayoutInflater layoutInflater = null;
public Resources resources;
GrpMsgModel grpMsgModel = null;
int i = 0;
public GrpMsgAdapter(Fragment fragment, ArrayList data, Resources resources){
this.fragment = fragment;
this.data = data;
this.resources = resources;
layoutInflater = (LayoutInflater)this.fragment.getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
if(data.size() <= 0)
return 1;
return data.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
public static class ViewHolder{
public ImageView iv_grpMsgDp;
public TextView tv_grpMsgName, tv_grpMsgNoMem;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
ViewHolder viewHolder;
if(convertView == null) {
view = layoutInflater.inflate(android.R.layout.lv_grpmsg, null);
viewHolder = new ViewHolder();
viewHolder.iv_grpMsgDp = (ImageView) view.findViewById(R.id.iv_grpMsgDp);
viewHolder.tv_grpMsgName = (TextView) view.findViewById(R.id.tv_grpMsgName);
viewHolder.tv_grpMsgNoMem = (TextView) view.findViewById(R.id.tv_grpMsgNoMem);
view.setTag("holder");
}
else{
viewHolder = (ViewHolder)view.getTag();
}
if(data.size() <= 0){
viewHolder.tv_grpMsgName.setText("No data");
}
else{
grpMsgModel = null;
grpMsgModel = (GrpMsgModel)data.get(position);
viewHolder.tv_grpMsgName.setText(grpMsgModel.getGrpName());
viewHolder.tv_grpMsgNoMem.setText(grpMsgModel.getNoMem());
viewHolder.iv_grpMsgDp.setImageResource(resources.getIdentifier("com.example.nmss.coach" + grpMsgModel.getImageURL(), null, null));
}
return view;
}
}
My ListView layout code is:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/relGrpMsg">
<ImageView
android:id="#+id/iv_grpMsgDp"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginStart="18dp"
app:srcCompat="#mipmap/ic_launcher_round"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_marginTop="12dp" />
<TextView
android:id="#+id/tv_grpMsgName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Group name"
android:layout_alignTop="#+id/iv_grpMsgDp"
android:layout_toEndOf="#+id/iv_grpMsgDp"
android:layout_marginStart="80dp" />
<TextView
android:id="#+id/tv_grpMsgNoMem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/iv_grpMsgDp"
android:layout_alignStart="#+id/tv_grpMsgName"
android:layout_marginBottom="15dp"
android:text="No. of members" />
<CheckBox
android:id="#+id/cb_grpMsgSel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/tv_grpMsgNoMem"
android:layout_alignParentEnd="true"
android:layout_marginEnd="24dp" />
</RelativeLayout>

It should be R.layout.lv_grpmsg not android.R.layout.lv_grpmsg

use R.layout.lv_grpmsg instead ofandroid.R.layout.lv_grpmsg
and other mistake is view.setTag("holder");
correct one is view.setTag(viewHolder);

Related

ListView in android scroll very slow when they take a short place

Please, the listview with just 3 textview (there is no image) it was working very fine when it was taking the full screen in height and width.
and after i have been change it to take the half of screen in height, it became scrolling very slow.
layout for rows in listView
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="4dp"
android:layout_marginEnd="16dp"
android:layout_toStartOf="#id/timer_row">
<TextView
android:id="#+id/name_row"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="#dimen/main_title_size"
android:textColor="#color/white"
/>
<TextView
android:id="#+id/author_row"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="#dimen/main_timer_size"
android:textColor="#color/white"
android:layout_below="#id/name_row"
android:layout_marginTop="16dp"
android:layout_alignParentStart="true"/>
</RelativeLayout>
<TextView
android:id="#+id/timer_row"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="#dimen/main_timer_size"
android:layout_margin="16dp"
android:padding="8dp"
android:textAlignment="center"
android:textColor="#color/white"
android:gravity="center"
android:layout_centerVertical="true"
android:layout_alignParentEnd="true"/>
</RelativeLayout>
and the adapter with the viewholder
public class MusicAdapter extends BaseAdapter {
private ArrayList<Music> list;
private LayoutInflater layoutInflater;
MusicAdapter(Context context, ArrayList<Music> list) {
this.list = list;
layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return list.size();
}
#Override
public Object getItem(int position) {
return list.get(position);
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.music_row, parent, false);
viewHolder = new ViewHolder(convertView);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
Music music = (Music) getItem(position);
viewHolder.title.setText(music.getName());
viewHolder.author.setText(music.getAuthor());
viewHolder.timer.setText(music.getTimer());
return convertView;
}
static class ViewHolder{
TextView title,author,timer;
ViewHolder(View v)
{
this.title = v.findViewById(R.id.name_row);
this.author = v.findViewById(R.id.author_row);
this.timer = v.findViewById(R.id.timer_row);
}
}
}
You should try to use RecyclerView instead of ListView: https://developer.android.com/guide/topics/ui/layout/recyclerview

textview of listview in android

I have Listview that contains a Textview, I populate data from sqlite inside them , now I want to change the textview background color for each row based on the value in each one.
I couldn't do it , can anybody help me for how to get each textview from the listview ??
Edit :
Here is my code to populate the listview:
sql = new SqlCommands(getApplicationContext());
Fields = new String[]{SqlCommands.Group, SqlCommands.Subject, SqlCommands.Body, SqlCommands.DateTime};
int [] Dview = new int []{R.id.grop, R.id.sbj,R.id.bdy,R.id.DT};
Cursor c = sql.GetData();
Adpt = new SimpleCursorAdapter(getApplicationContext(),R.layout.items, c , Fields, Dview ,0 );
NotiLst.setAdapter(Adpt);
this is the XML file for the items :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:background="#drawable/grp_shap" >
<ImageView android:id="#+id/Prio"
android:layout_width="32dip"
android:layout_height="32dip"
/>
<TextView
android:id="#+id/grop"
android:layout_width="64sp"
android:layout_height="40sp"
android:layout_alignBottom="#+id/Prio"
android:layout_alignParentLeft="true"
android:text="S"
android:textSize="12sp" />
<TextView
android:id="#+id/DT"
android:layout_width="wrap_content"
android:layout_height="14sp"
android:layout_alignParentRight="true"
android:layout_alignTop="#id/title"
android:gravity="right"
android:text="Date Rec"
android:layout_marginRight="5dip"
android:textSize="12sp"
/>
<TextView
android:id="#+id/bdy"
android:layout_width="fill_parent"
android:layout_height="28sp"
android:layout_below="#+id/DT"
android:layout_toLeftOf="#+id/hi"
android:layout_toRightOf="#+id/Prio"
android:text="Body"
android:textSize="12sp" />
<TextView
android:id="#+id/sbj"
android:layout_width="fill_parent"
android:layout_height="12sp"
android:layout_alignLeft="#+id/bdy"
android:layout_alignParentTop="true"
android:layout_alignRight="#+id/bdy"
android:fontFamily="sans-serif"
android:text="Subject"
android:textSize="12sp" />
<ImageView
android:id="#+id/hi"
android:layout_width="20dip"
android:layout_height="26dip"
android:layout_alignLeft="#+id/DT"
android:layout_below="#+id/DT"
android:layout_marginLeft="15dp"
android:src="#drawable/highprio" />
</RelativeLayout>
and I have a listview on the activity_mail layout which populated whith this items.
I want to change the color for the textview with id grop based on the value inserted on it
You should use a custom adapter:
public class MyAdapter extends BaseAdapter {
private static class ViewHolder {
public final TextView mTv;
}
private final Context mContext;
private final List<String> mList; //you can change this to String array
public MyAdapter (Context context, List<String> list) {
this.mContext = context;
this.mList= list;
}
#Override
public int getCount() {
return mList.size();
}
#Override
public String getItem(int position) {
return mList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder h;
final String string = mList.get(position);
if (convertView == null) {
convertView = View.inflate(mContext, R.layout.item_listview, null);
h = new ViewHolder();
h.mTv= (TextView) convertView .findViewById(R.id.mTvRes);
convertView.setTag(h);
} else {
h = (ViewHolder) convertView.getTag();
}
h.mTv.setText....
h.mTv.setTextColor....
h.mTv.setBackground....
return convertView;
}
}
in your activity:
List<String> list = Arrays.asList(Fields);
MyAdapter adapter = new MyAdapter(this, list);
NotiLst.setAdapter(adapter);
I didn't checked it and you maybe need to do some changes but it should work.
If you have something like this:
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if(convertView == null) {
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(resource, parent, false);
viewHolder = new ViewHolder();
viewHolder.label = (TextView) convertView.findViewById(android.R.id.text1);
viewHolder.label.setBackground(getResources().getColor(color.white));
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.label.setText(activitiesList.get(position).getName());
return convertView;
}

Android GridView not showing items

I have made an app with a gridview by following slidenerd's tutorial. My problem is that I can see the view but it's empty. When I move my finger I can see the top and the button becoming blue.
Here is my code:
The Adapter:
public class GridViewAdapter extends BaseAdapter {
private ArrayList<SingleButton> buttons;
private MainActivity mainActivity;
public GridViewAdapter(MainActivity mainActivity) {
this.mainActivity = mainActivity;
this.buttons = new ArrayList<SingleButton>();
Resources res = mainActivity.getApplicationContext().getResources();
String[] temp = res.getStringArray(R.array.buttons);
int[] buttonImages = { R.drawable.pic1, R.drawable.pic2,
R.drawable.pic3, R.drawable.pic4, R.drawable.pic5,
R.drawable.pic6, R.drawable.pic7, R.drawable.pic8,
R.drawable.pic9 };
for (int count = 0; count < 9; ++count) {
this.buttons
.add(new SingleButton(buttonImages[count], temp[count]));
}
}
#Override
public int getCount() {
return this.buttons.size();
}
#Override
public Object getItem(int position) {
return this.buttons.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#SuppressLint("ViewHolder")
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ViewHolder holder = null;
if (row == null) {
LayoutInflater layoutInflater = (LayoutInflater) mainActivity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = layoutInflater.inflate(R.layout.single_button, parent, false);
holder = new ViewHolder(row);
row.setTag(holder);
} else {
holder = (ViewHolder) row.getTag();
}
holder.buttonImage.setImageResource(buttons.get(position).image);
holder.buttonText.setText(buttons.get(position).buttonText);
return row;
}
class ViewHolder {
ImageView buttonImage;
TextView buttonText;
public ViewHolder(View v) {
this.buttonImage = (ImageView) v.findViewById(R.id.imageViewButton);
this.buttonText = (TextView) v.findViewById(R.id.textViewButton);
}
}
class SingleButton {
int image;
String buttonText;
SingleButton(int image, String buttonText) {
this.image = image;
this.buttonText = buttonText;
}
}
single_button.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/imageViewButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
<TextView
android:id="#+id/textViewButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/imageViewButton"
android:layout_centerHorizontal="true"
android:maxLines="1" />
</RelativeLayout>
main activity layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/background"
tools:context="com.example.app.MainActivity" >
<ImageView
android:id="#+id/imageViewLogo"
android:layout_width="wrap_content"
android:layout_height="5dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:paddingBottom="5dp"
android:src="#drawable/company_logo" />
<GridView
android:id="#+id/gridViewButtons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/imageViewLogo"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:horizontalSpacing="10dp"
android:numColumns="3"
android:stretchMode="spacingWidth"
android:verticalSpacing="10dp" >
</GridView>
</RelativeLayout>

How to customize listview using baseadapter

I wanna create a customized ListView like this:
I think that I have to use BaseAdapter but I have no idea about it.
main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ListView>
</RelativeLayout>
custom.xml:
<?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" >
<LinearLayout
android:layout_width="255dp"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Video1"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#339966"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/detail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="video1"
android:textColor="#606060" />
</LinearLayout>
</LinearLayout>
<ImageView
android:id="#+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
</LinearLayout>
</LinearLayout>
main.java:
package com.example.sample;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends Activity {
ListView l1;
String[] t1={"video1","video2"};
String[] d1={"lesson1","lesson2"};
int[] i1 ={R.drawable.ic_launcher,R.drawable.ic_launcher};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
l1=(ListView)findViewById(R.id.list);
l1.setAdapter(new dataListAdapter(t1,d1,i1));
}
class dataListAdapter extends BaseAdapter {
String[] Title, Detail;
int[] imge;
dataListAdapter() {
Title = null;
Detail = null;
imge=null;
}
public dataListAdapter(String[] text, String[] text1,int[] text3) {
Title = text;
Detail = text1;
imge = text3;
}
public int getCount() {
// TODO Auto-generated method stub
return Title.length;
}
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View row;
row = inflater.inflate(R.layout.custom, parent, false);
TextView title, detail;
ImageView i1;
title = (TextView) row.findViewById(R.id.title);
detail = (TextView) row.findViewById(R.id.detail);
i1=(ImageView)row.findViewById(R.id.img);
title.setText(Title[position]);
detail.setText(Detail[position]);
i1.setImageResource(imge[position]);
return (row);
}
}
}
Try this.
public class ListElementAdapter extends BaseAdapter{
String[] data;
Context context;
LayoutInflater layoutInflater;
public ListElementAdapter(String[] data, Context context) {
super();
this.data = data;
this.context = context;
layoutInflater = LayoutInflater.from(context);
}
#Override
public int getCount() {
return data.length;
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView= layoutInflater.inflate(R.layout.item, null);
TextView txt=(TextView)convertView.findViewById(R.id.text);
txt.setText(data[position]);
return convertView;
}
}
Just call ListElementAdapter in your Main Activity and set Adapter to ListView.
Create your own BaseAdapter class and use it as following.
public class NotificationScreen extends Activity
{
#Override
protected void onCreate_Impl(Bundle savedInstanceState)
{
setContentView(R.layout.notification_screen);
ListView notificationList = (ListView) findViewById(R.id.notification_list);
NotiFicationListAdapter notiFicationListAdapter = new NotiFicationListAdapter();
notificationList.setAdapter(notiFicationListAdapter);
homeButton = (Button) findViewById(R.id.home_button);
}
}
Make your own BaseAdapter class and its separate xml file.
public class NotiFicationListAdapter extends BaseAdapter
{
private ArrayList<HashMap<String, String>> data;
private LayoutInflater inflater=null;
public NotiFicationListAdapter(ArrayList data)
{
this.data=data;
inflater =(LayoutInflater)baseActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount()
{
return data.size();
}
public Object getItem(int position)
{
return position;
}
public long getItemId(int position)
{
return position;
}
public View getView(int position, View convertView, ViewGroup parent)
{
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.notification_list_item, null);
ImageView compleatImageView=(ImageView)vi.findViewById(R.id.complet_image);
TextView name = (TextView)vi.findViewById(R.id.game_name); // name
TextView email_id = (TextView)vi.findViewById(R.id.e_mail_id); // email ID
TextView notification_message = (TextView)vi.findViewById(R.id.notification_message); // notification message
compleatImageView.setBackgroundResource(R.id.address_book);
name.setText(data.getIndex(position));
email_id.setText(data.getIndex(position));
notification_message.setTextdata.getIndex(position));
return vi;
}
}
BaseAdapter xml file.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/inner_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="10dp"
android:layout_weight="4"
android:background="#drawable/list_view_frame"
android:gravity="center_vertical"
android:padding="5dp" >
<TextView
android:id="#+id/game_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Game name"
android:textColor="#FFFFFF"
android:textSize="15dip"
android:textStyle="bold"
android:typeface="sans" />
<TextView
android:id="#+id/e_mail_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/game_name"
android:layout_marginTop="1dip"
android:text="E-Mail Id"
android:textColor="#FFFFFF"
android:textSize="10dip" />
<TextView
android:id="#+id/notification_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/game_name"
android:layout_toRightOf="#id/e_mail_id"
android:paddingLeft="5dp"
android:text="Notification message"
android:textColor="#FFFFFF"
android:textSize="10dip" />
<ImageView
android:id="#+id/complet_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginBottom="30dp"
android:layout_marginRight="10dp"
android:src="#drawable/complete_tag"
android:visibility="invisible" />
</RelativeLayout>
Change it accordingly and use.
I suggest using a custom Adapter, first create a Xml-file, for example layout/customlistview.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" >
<ImageView
android:id="#+id/image"
android:layout_alignParentRight="true"
android:paddingRight="4dp" />
<TextView
android:id="#+id/title"
android:layout_toLeftOf="#id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="23sp"
android:maxLines="1" />
<TextView
android:id="#+id/subtitle"
android:layout_toLeftOf="#id/image" android:layout_below="#id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
Assuming you have a custom class like this
public class CustomClass {
private long id;
private String title, subtitle, picture;
public CustomClass () {
}
public CustomClass (long id, String title, String subtitle, String picture) {
this.id = id;
this.title= title;
this.subtitle= subtitle;
this.picture= picture;
}
//add getters and setters
}
And a CustomAdapter.java uses the xml-layout
public class CustomAdapter extends ArrayAdapter {
private Context context;
private int resource;
private LayoutInflater inflater;
public CustomAdapter (Context context, List<CustomClass> values) { // or String[][] or whatever
super(context, R.layout.customlistviewitem, values);
this.context = context;
this.resource = R.layout.customlistview;
this.inflater = LayoutInflater.from(context);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = (RelativeLayout) inflater.inflate(resource, null);
CustomClass item = (CustomClass) getItem(position);
TextView textviewTitle = (TextView) convertView.findViewById(R.id.title);
TextView textviewSubtitle = (TextView) convertView.findViewById(R.id.subtitle);
ImageView imageview = (ImageView) convertView.findViewById(R.id.image);
//fill the textviews and imageview with the values
textviewTitle = item.getTtile();
textviewSubtitle = item.getSubtitle();
if (item.getAfbeelding() != null) {
int imageResource = context.getResources().getIdentifier("drawable/" + item.getImage(), null, context.getPackageName());
Drawable image = context.getResources().getDrawable(imageResource);
}
imageview.setImageDrawable(image);
return convertView;
}
}
Did you manage to do it? Feel free to ask if you want more info on something :)
EDIT: Changed the adapter to suit a List instead of just a List
private class ObjectAdapter extends BaseAdapter {
private Context context;
private List<Object>objects;
public ObjectAdapter(Context context, List<Object> objects) {
this.context = context;
this.objects = objects;
}
#Override
public int getCount() {
return objects.size();
}
#Override
public Object getItem(int position) {
return objects.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView==null){
holder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(android.R.layout.simple_list_item_1, parent, false);
holder.text = (TextView) convertView.findViewById(android.R.id.text1);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
holder.text.setText(getItem(position).toString()));
return convertView;
}
class ViewHolder {
TextView text;
}
}

ListView is showing gapes in list items Android

i am trying to implement listView through setListAdapter and Efficient Adapter. I want that when list is show then the background should not be repeat. My code is repeating the whole layout of list.xml due to which my list item are showing with so much gap.
Right now my list is working like that:
But i want this type of view:
Here is my editText.xml in which i type the word and a list View is opened.
<EditText
android:id="#+id/start_edit"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_gravity="top|left"
android:ems="10"
android:hint="Type to search"
android:paddingLeft="50dp" >
<requestFocus />
</EditText>
this layout is for list.xml :
<RelativeLayout
android:id="#+id/RelativeLayout_list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/blue_cellbar" >
the list.xml file is repeating the layout in Effecient adapter:
here is my code:
listAdapter = new EfficientAdapter2(this);
setListAdapter(listAdapter);
public static class viewHolder2 {
TextView word;
TextView meaning;
ImageView image;
ImageView image_color;
RelativeLayout cell;
}
private class EfficientAdapter2 extends BaseAdapter implements Filterable,OnItemClickListener {
private Context context;
LayoutInflater inflater;
public EfficientAdapter2(Context context) {
this.context = context;
inflater = LayoutInflater.from(context);
}
public int getCount() {
// if(SearchWordString.isEmpty()==false)
// {
return SearchWordString.size();
/// }
//return 0;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
viewHolder2 holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.list, null);
ViewToUse=parent;
holder = new viewHolder2();
// Log.i("View","is Null");
convertView.setTag(holder);
} else {
//Log.i("View","is not Null");
holder = (viewHolder2) convertView.getTag();
}
holder.cell = (RelativeLayout) convertView
.findViewById(R.id.RelativeLayout_list);
return convertView;
}
UPDATE
public class Start extends ListActivity implements OnTouchListener,
android.view.GestureDetector.OnGestureListener {
// ////////////////////////////////////////////////////
/************* INNER CLASS VIEWHOLDER ****************/
// ////////////////////////////////////////////////////
onCreate()
{
ListView list_to_use = getListView();
listAdapter = new EfficientAdapter2(this);
list_to_use.setAdapter(listAdapter);
list_to_use.setBackgroundColor(2);
viewFlipper = (ViewFlipper) findViewById(R.id.viewFlipper1);
search = (EditText) findViewById(R.id.start_edit);
search.addTextChangedListener(myTextWatcher);
}
public static class viewHolder2 {
TextView word;
TextView meaning;
ImageView image;
ImageView image_color;
RelativeLayout cell;
}
// ////////////////////////////////////////////////////
/*********** INNER CLASS EfficientAdapter ************/
// ////////////////////////////////////////////////////
private class EfficientAdapter2 extends BaseAdapter implements Filterable,OnItemClickListener {
private Context context;
LayoutInflater inflater;
public EfficientAdapter2(Context context) {
this.context = context;
inflater = LayoutInflater.from(context);
}
public int getCount() {
// if(SearchWordString.isEmpty()==false)
// {
return SearchWordString.size();
/// }
//return 0;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
viewHolder2 holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_search_item, null);
ViewToUse=parent;
holder = new viewHolder2();
// Log.i("View","is Null");
convertView.setTag(holder);
holder.word = (TextView) convertView.findViewById(R.id.title_list);
holder.meaning = (TextView) convertView
.findViewById(R.id.textView_meaning_list);
holder.image = (ImageView) convertView
.findViewById(R.id.image_list);
holder.image_color = (ImageView) convertView
.findViewById(R.id.imageView_color_list);
holder.cell = (RelativeLayout) convertView
.findViewById(R.id.RelativeLayout_list);
} else {
//Log.i("View","is not Null");
holder = (viewHolder2) convertView.getTag();
}
}
}
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<EditText
android:id="#+id/start_edit"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_gravity="top|left"
android:ems="10"
android:hint="Type to search"
android:paddingLeft="50dp" >
<requestFocus />
</EditText>
<ViewFlipper
android:id="#+id/viewFlipper1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="top|left"
android:layout_marginTop="50dp" >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/blue_home"
android:fastScrollEnabled="true"
android:smoothScrollbar="true"
android:divider="#drawable/blue_dic"
android:dividerHeight="250sp" >
</ListView>
</FrameLayout>
</ViewFlipper>
rows for listView:
<?xml version="1.0" encoding="utf-8"?>
<ImageView
android:id="#+id/image_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#drawable/abacus_thumbnail"
android:scaleType="centerCrop" />
<ImageView
android:id="#+id/imageView_color_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#drawable/blue_thumbnail" />
<TextView
android:id="#+id/title_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/image_list"
android:layout_marginLeft="20dp"
android:layout_toRightOf="#+id/image_list"
android:gravity="center"
android:text="Abacus"
android:textColor="#000000"
android:textSize="30sp"
android:textStyle="bold"
android:typeface="sans" />
<TextView
android:id="#+id/textView_meaning_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/title_list"
android:layout_below="#+id/title_list"
android:layout_marginTop="10dp"
android:text="TextView"
android:textColor="#000000"
android:textSize="25sp" />
I hope you have not provide android:dividerHeight attribute in your layout file under <ListView/>
If so please remove it.
this problem occurs when you have set the adapter class xml, parent root
match_content instead of wrap_content.
set the root content height wrap_content
As i can see in adapter layout there is no parent root like relative, linearlayout, Framelayout and ConstraintLayout etc.

Categories

Resources