I have a question, currently I am using CursorTreeAdapter however I want to change it to use any other data not just cursors, but to be honest im not sure how would I do that, I think implement some other adapter and then override needed methods? But could any one show me some way? I'm confused right now and don't know to what direction I should go.
Thanks for help.
Abstract classes you can extend: BaseAdapter ArrayAdapter.
You can see this example:
public class FeedAdapter extends BaseAdapter {
private ArrayList<FeedItem> items;
private LayoutInflater layoutInflater;
FeedAdapter(Context context, ArrayList<FeedItem> list, int bgColor){
items = list;
layoutInflater = LayoutInflater.from(context);
}
#Override
public int getCount() {
return items.size();
}
#Override
public FeedItem getItem(int index) {
return items.get(index);
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row;
if (null == convertView) {
row = layoutInflater.inflate(R.layout.romanblack_feed_item, null);
} else {
row = convertView;
}
TextView title = (TextView) row.findViewById(R.id.romanblack_rss_title);
TextView pubdate = (TextView) row.findViewById(R.id.romanblack_rss_pubdate);
String titleString = items.get(position).getTitle();
title.setText(titleString);
if(items.get(position).getTextColor() != Color.TRANSPARENT){
title.setTextColor(items.get(position).getTextColor());
}else{
title.setTextColor(Color.DKGRAY);
}
pubdate.setText(items.get(position).getPubdate());
return row;
}
}
and layout file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/romanblack_feed_item"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FFF">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/romanblack_rss_bg_feed"
android:orientation="vertical">
<TextView
android:text="Title"
android:id="#+id/romanblack_rss_title"
android:textSize="14sp"
android:textColor="#222"
android:layout_margin="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:text="2011-01-01"
android:id="#+id/romanblack_rss_pubdate"
android:textSize="10sp"
android:textColor="#666"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
Related
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
I need to customize spinner in Android.
I have my simple Adapter:
public class MonthsSpinnerAdapter extends BaseAdapter {
private Activity context;
private String [] itemList;
public MonthsSpinnerAdapter(Activity context,String [] monthArray) {
this.context=context;
this.itemList=monthArray;
}
#Override
public int getCount() {
return 0;
}
#Override
public Object getItem(int position) {
return itemList[position];
}
#Override
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View row = inflater.inflate(R.layout.spinner_item, parent,
false);
RobotoTextView make = (RobotoTextView) row.findViewById(R.id.spinner_title);
make.setText(itemList[position]);
Log.d("SpinnerA", "getView");
return row;
}
public View getDropDownView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View row = inflater.inflate(R.layout.spinner_dropdown_item, parent,
false);
RobotoTextView month = (RobotoTextView) row.findViewById(R.id.spinner_month);
month.setText(itemList[position]);
RobotoTextView year = (RobotoTextView) row.findViewById(R.id.spinner_year);
year.setText(context.getString(R.string.year));
Log.d("SpinnerA", "getDropDownView");
return row;
}
}
But when I added it to my spinner and run the up, I don't see anything in spinner.
MonthsSpinnerAdapter adapter = new MonthsSpinnerAdapter(GeneralActivity.this
, getResources().getStringArray(R.array.month_list));
spinner.setAdapter(adapter);
I think it will be work nice, but I dont know where I have mistake. This is example what I see, when run app:
And at this picture, what I need:
And this my xml files if you need:
<com.devspark.robototextview.widget.RobotoTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:typeface="roboto_medium"
android:padding="5dp"
android:textColor="#fff"
android:id="#+id/spinner_title"
android:text="month"
android:background="#color/primary_color"
xmlns:android="http://schemas.android.com/apk/res/android" />
And DropDown layout:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<com.devspark.robototextview.widget.RobotoTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:typeface="roboto_medium"
android:layout_marginTop="16dp"
android:layout_marginLeft="12dp"
android:id="#+id/spinner_month"
android:text="month"
android:textColor="#545454"
android:background="#fafafa" />
<com.devspark.robototextview.widget.RobotoTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:typeface="roboto_regular"
android:layout_marginTop="16dp"
android:layout_marginRight="12dp"
android:layout_alignParentRight="true"
android:text="year"
android:id="#+id/spinner_year"
/>
</RelativeLayout>
Help me please with my problem
Try to return itemList.length in getCount function of your adapter.
#Override
public int getCount() {
return itemList.length;
}
I'm new in android..
I would like to create the different views in Listview using Custom Adapter.
Please suggest me how I can do this.
In first row there will be a single Item and in second row there will be two item and so on..
Please Check the attached screen..
Thanks in advance
Define a custom layout of how you want the list row like this
<?xml version="1.0" encoding="utf-8"?>
<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="#dimen/headerHeightCustRow"
android:background="#FFFFFF"
tools:ignore="HardcodedText" >
<TextView
android:id="#+id/txtName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="2dip"
android:layout_marginLeft="#dimen/viewSpace1"
android:text="Varun Mad"
android:textSize="#dimen/logout_textSize"
android:textStyle="bold"
android:textColor="#color/orange_normal" />
<TextView
android:id="#+id/txtCustId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="#dimen/viewSpace3"
android:layout_marginTop="#dimen/viewSpace3"
android:layout_marginRight="#dimen/viewSpace3"
android:text="10549"
android:layout_centerVertical="true"
android:textColor="#color/orange_normal"
android:textSize="15sp" />
<TextView
android:id="#+id/txtPhone"
android:layout_below="#id/txtName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/viewSpace1"
android:text="(886)677-8855"
android:textColor="#color/orange_normal"
android:textSize="#dimen/vsText" />
<TextView
android:id="#+id/txtEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/viewSpace1"
android:text="ggg#gmail.com"
android:textSize="#dimen/vsText"
android:layout_below="#id/txtPhone"
android:textColor="#color/orange_normal" />
<View
android:layout_width="fill_parent"
android:layout_height="0.5dip"
android:layout_alignParentBottom="true"
android:background="#color/greyDark" />
here is the adapter class
public class SearchCustomerAdapter extends BaseAdapter {
Context ctx;
LayoutInflater lInflater;
ArrayList<SearchCustomerItem> objects;
public SearchCustomerAdapter(Context context, ArrayList<SearchCustomerItem> products) {
ctx = context;
objects = products;
lInflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#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) {
View view = convertView;
if (view == null) {
view = lInflater.inflate(R.layout.search_cust_row, parent, false);
}
SearchCustomerItem p = getProduct(position);
((TextView) view.findViewById(R.id.txtName)).setText(p.customerName);
if (p.customerEmail.compareTo("anyType{}") == 0) {
((TextView) view.findViewById(R.id.txtEmail)).setText("");
} else {
((TextView) view.findViewById(R.id.txtEmail)).setText(p.customerEmail);
}
((TextView) view.findViewById(R.id.txtPhone)).setText(p.customerPhone);
((TextView) view.findViewById(R.id.txtCustId)).setText(p.customerId);
return view;
}
SearchCustomerItem getProduct(int position) {
return ((SearchCustomerItem) getItem(position));
}
#SuppressWarnings("unused")
ArrayList<SearchCustomerItem> getBox() {
ArrayList<SearchCustomerItem> box = new ArrayList<SearchCustomerItem>();
for (SearchCustomerItem p : objects) {
// if (p.box)
// box.add(p);
}
return box;
}
}
and the ITem Class
public class SearchCustomerItem {
public String customerName;
public String customerPhone;
public String customerEmail;
public String customerId;
public SearchCustomerItem(String _cName, String _cPhone, String _cEmail, String _cCustId) {
customerName = _cName;
customerPhone = _cPhone;
customerEmail = _cEmail;
customerId = _cCustId;
}
}
you can initialize the adapter,
add the items in Arraylist and show in the list by using
SearchCustomerAdapter boxAdapter;
ArrayList<SearchCustomerItem> products = new ArrayList<SearchCustomerItem>();
to add items in arraylist
products.add(new SearchCustomerItem("CustomerName","PhoneNo","Cust_EmailId","Cust_Id"));
//Add as many items you want
than
boxAdapter = new SearchCustomerAdapter(SearchActivity.this, products);
list.setAdapter(boxAdapter);
This link might help you. link
You should do it step by step.
Extend Base Adapter class by you Custom Adapter class
Override getView and other related methods
set the adapter to list view adapter.
Good day to all.
I am currently working on a customized ListView which is not inflating (is not becoming visible) inside the activity.
Below are the xml and ListView adapter classes.
main.xml
<LinearLayout
android:id="#+id/llListView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="2dp"
android:paddingBottom="2dp"
android:layout_below="#+id/rlFirstRow">
<ListView
android:id="#+id/lvErrorsReport"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/border"/>
</LinearLayout>
listview_row.xml
<?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">
<LinearLayout
android:id="#+id/llError"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentTop="true">
<TextView
android:id="#+id/txtErrorInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textStyle="bold"
android:textSize="20dp"/>
</LinearLayout>
<LinearLayout
android:id="#+id/llStatus"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="#+id/llError">
<TextView
android:id="#+id/txtStatus"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Status: "/>
<TextView
android:id="#+id/txtStatusInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textStyle="italic"/>
</LinearLayout>
<LinearLayout
android:id="#+id/llMaterials"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="#+id/llStatus">
<TextView
android:id="#+id/txtMaterials"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Materials: "/>
<TextView
android:id="#+id/txtMaterialsInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textStyle="italic"/>
</LinearLayout>
<LinearLayout
android:id="#+id/llButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="#+id/llMaterials">
<Button
android:id="#+id/btnShowMaterials"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Materials"
android:visibility="gone"/>
</LinearLayout>
</RelativeLayout>
BaseAdapter class
public class RepairReportListViewAdapter extends BaseAdapter
{
ArrayList<String> errors;
ArrayList<String> statuses;
ArrayList<Boolean> materials;
Context mContext;
LayoutInflater inflater;
public RepairReportListViewAdapter(Context context, ArrayList<String> err, ArrayList<String> stat, ArrayList<Boolean> mat)
{
this.mContext = context;
this.errors = err;
this.statuses = stat;
this.materials = mat;
inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount()
{
return 0;
}
#Override
public Object getItem(int position)
{
return null;
}
#Override
public long getItemId(int position)
{
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder holder;
if(convertView == null)
{
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.repairreport_listview, null);
holder.error = (TextView)convertView.findViewById(R.id.txtErrorInfo);
holder.status = (TextView)convertView.findViewById(R.id.txtStatusInfo);
holder.material = (TextView)convertView.findViewById(R.id.txtMaterialsInfo);
holder.showMaterials = (Button)convertView.findViewById(R.id.btnShowMaterials);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder)convertView.getTag();
}
holder.error.setText(this.errors.get(position).toString());
holder.status.setText(this.statuses.get(position).toString());
if(this.materials.get(position) == true)
{
holder.material.setText("Materials where used to fix this error. Click on the button below to view the materials.");
holder.showMaterials.setVisibility(Button.VISIBLE);
holder.showMaterials.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
//LATER
}
});
}
else
{
holder.material.setText("No materials where used to fix this error.");
}
return convertView;
}
static class ViewHolder
{
TextView error;
TextView status;
TextView material;
Button showMaterials;
}
}
I've already tried changing the layouts as suggested here but the ListView is still not revealed.
Any ideas why it is not showing? Sorry for the code dump but I really cannot figure out what the heck is going on.
P.S. I did set the adapter to the ListView in the main activity.
adapter = new RepairReportListViewAdapter(this, errorNames, statuses, materialsInUse);
lvErrors.setAdapter(adapter);
your ListView does not inflate nothing because getCount is returning 0
change
#Override
public int getCount()
{
return 0;
}
with
#Override
public int getCount()
{
return (stat == null) ? 0 : stat.size();
}
Also, instead of keeping three differents ArrayList, you can create a class that holds all the info you need and an ArrayList of this class
Hi Please any one Help me out of this..
i am using listview adapter for displaying image list with check box, that allows to select multiple image form list.. code works good for images list not more than screen but if image is more than screen (for scrolling) it throws null pointer exception.. following is my code.
(apterMultiChoice.java)
public class PhotoAdapterMultiChoice extends BaseAdapter
{
public ViewHolder holder;
private Activity activity;
private ArrayList<String> data;
private ArrayList<String> text;
private ArrayList<CheckBox> chkBox;
private static LayoutInflater inflater=null;
public ImageLoader imageLoader;
public PhotoAdapterMultiChoice(Activity a, ArrayList<String> title,ArrayList<String> URL,ArrayList<CheckBox> chkBoxPass)
{
activity = (Activity) a;
data=URL;
text=title;
this.chkBox = chkBoxPass;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader=new ImageLoader(activity.getApplicationContext());
}
public int getCount()
{
return data.size();
}
public boolean getSelect(int position)
{
return this.chkBox.get(position).isChecked();
}
public int getCountSelect()
{
return chkBox.size();
}
public Object getItem(int position)
{
return this.chkBox.get(position).isChecked();
}
public long getItemId(int position)
{
return position;
}
public static class ViewHolder
{
public TextView text;
public ImageView image;
public CheckBox checkBox;
}
public View getView(int position, View convertView, ViewGroup parent)
{
System.out.println(this.chkBox.size() +" ---- "+data.size());
if(this.chkBox.size() < data.size())
{
View vi=convertView;
if(convertView==null)
{
System.out.println("convertView is null");
vi = inflater.inflate(R.layout.photoitemlistmultichoice, null);
holder=new ViewHolder();
holder.text=(TextView)vi.findViewById(R.id.text);
holder.image=(ImageView)vi.findViewById(R.id.image);
holder.checkBox=(CheckBox) vi.findViewById(R.id.check);
//this.chkBox.add((CheckBox) vi.findViewById(R.id.check));
vi.setTag(holder);
}
else
{
System.out.println("convertView is Not null");
this.chkBox.add((CheckBox) vi.findViewById(R.id.check));
holder=(ViewHolder)vi.getTag();
}
System.out.println("Position : "+position);
holder.text.setText(text.get(position));
holder.image.setTag(data.get(position));
//holder.checkBox.setTag(chkBox.get(position));
//this.chkBox.set(position, chkBox.get(position));
//this.chkBox.add(holder.checkBox);
imageLoader.DisplayImage(data.get(position), activity, holder.image,"Photo");
return vi;
}
return null;
}
}
How i call adapter
PhotoAdapter adapter = new PhotoAdapter(this, imageNameList, thumbURLList);
listPhoto.setAdapter(adapter); //listPhoto is my listView
(photolist.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="#+id/lblAlbumName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<ListView
android:id="#+id/listPhoto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"/>
</LinearLayout>
(photoitemlist.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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_width="50dip"
android:layout_height="50dip" android:src="#drawable/default_photo_icon" android:scaleType="centerCrop"/>
<TextView
android:id="#+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="left|center_vertical"
android:textSize="20dip"
android:layout_marginLeft="10dip"
/>
</LinearLayout>
please help..
thanks...
The error is, as you said. When you scroll it poops out. So your creating your views correctly but not recycling them correctly.
this.chkBox.add((CheckBox) vi.findViewById(R.id.check));
Looks like what's causing your error. You don't need to find the Id again as you already have a holder for it.
holder.text.setText(text.get(position));
holder.image.setTag(data.get(position));
You probably want that in your else statement