custom ImageViews disapearing from ListAdapter - java

Ok, my problem is similar to this Background image in ListView entries spontaneously disappearing
but this issue is resolved with using setImageResource on a normal ImageView but I want an animation to exist on all elements of a list, and this can not be done without a custom ImageView class that allows the animation to run. Does anyone know why these images are disappearing after scrolling in the list view and maybe how this can be fixed? Thanks a lot!
Here is my ListAdapter:
package com.mmgworldwide.layout.listadapters;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import com.mmgworldwide.R;
import com.mmgworldwide.customviews.SpinLoadingAnim;
import com.mmgworldwide.data.FWREvent;
import com.mmgworldwide.data.ImageThreadLoader;
import com.mmgworldwide.data.ImageThreadLoader.ImageLoadedListener;
import com.mmgworldwide.layout.ListDivider;
import com.mmgworldwide.data.Personality;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.text.SpannableString;
import android.text.style.UnderlineSpan;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class PersonalityListAdapter extends ArrayAdapter<Object>{
private Context parentContext;
public ArrayList<Object> items_list_ref;
private ImageThreadLoader imageLoader = new ImageThreadLoader();
public PersonalityListAdapter(Context context, int resource, int textViewResourceId, ArrayList<Object> items_list) {
super(context, resource, textViewResourceId, items_list);
items_list_ref = items_list;
parentContext = context;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
Object node = (Object) items_list_ref.get(position);
LayoutInflater inflater = (LayoutInflater) parentContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(node instanceof ListDivider){
//make new div bar
View div = inflater.inflate(R.layout.list_bar, parent, false);
TextView div_label = (TextView) div.findViewById(R.id.div_label);
div_label.setText(((ListDivider) node).label.toUpperCase());
return div;
}
View row = inflater.inflate(R.layout.picture_list, parent, false);
TextView name_txt = (TextView) row.findViewById(R.id.name_txt);
TextView title_txt = (TextView) row.findViewById(R.id.title_txt);
final ImageView portrait_img = (ImageView) row.findViewById(R.id.portrait);
final ImageView load_cnt = (ImageView) row.findViewById(R.id.portrait_loadDisplay);
name_txt.setText(((Personality) node).getName());
SpannableString title_underlined = new SpannableString(((Personality) node).getTitle());
title_underlined .setSpan(new UnderlineSpan(), 0, title_underlined.length(), 0);
title_txt.setText(title_underlined);
load_cnt.setVisibility(View.VISIBLE);
return row;
}
Here is my custom ImageView class
package com.mmgworldwide.customviews;
import android.R;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.AnimationDrawable;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.ImageView;
public class SpinLoadingAnim extends ImageView{
AnimationDrawable animation;
private final Integer FRAME_SPEED = 20;
public SpinLoadingAnim(Context context, AttributeSet attrs){
super(context, attrs);
//setBackgroundColor(Color.GREEN);
animation = new AnimationDrawable();
animation.addFrame(context.getResources().getDrawable(com.mmgworldwide.R.drawable.spinner_00000), FRAME_SPEED);
animation.addFrame(context.getResources().getDrawable(com.mmgworldwide.R.drawable.spinner_00001), FRAME_SPEED);
animation.addFrame(context.getResources().getDrawable(com.mmgworldwide.R.drawable.spinner_00002), FRAME_SPEED);
animation.addFrame(context.getResources().getDrawable(com.mmgworldwide.R.drawable.spinner_00003), FRAME_SPEED);
animation.setOneShot(false);
this.setBackgroundDrawable(animation);
this.post(new Starter());
}
class Starter implements Runnable {
public void run() {
animation.start();
}
}
And here is my layout 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="wrap_content"
android:background="#FFFFFF"
android:gravity="center_vertical"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp">
<ImageView android:layout_width="60dp" android:layout_height="60dp" android:id="#+id/portrait" android:layout_centerVertical="true"></ImageView>
<com.mmgworldwide.customviews.SpinLoadingAnim
android:id="#+id/portrait_loadDisplay"
android:layout_width="20dp" android:layout_height="20dp"
android:background="#939598"
android:layout_centerVertical="true" android:layout_marginLeft="20dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true" />
<ImageView android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_centerVertical="true" android:id="#+id/arrow" android:src="#drawable/list_arrow" android:layout_alignParentRight="true"></ImageView>
<LinearLayout android:layout_toRightOf="#id/portrait" android:layout_toLeftOf="#id/arrow" android:layout_centerVertical="true" android:orientation="vertical" android:layout_height="wrap_content" android:layout_width="wrap_content" android:baselineAligned="true" android:paddingLeft="15dp" android:paddingRight="15dp">
<TextView android:text="CHEF NAME" android:id="#+id/name_txt" android:layout_width="wrap_content" android:textColor="#000000" android:textStyle="bold"
android:layout_height="wrap_content" android:paddingBottom="2dp" android:textSize="15sp"></TextView>
<TextView android:text="Chef Title" android:id="#+id/title_txt" android:layout_width="wrap_content" android:textColor="#000000"
android:layout_height="wrap_content" android:textSize="13sp"></TextView>
</LinearLayout>
</RelativeLayout>
Every 5th 'preloader' graphic is still disappearing, but thanks Michele this has helped a lot, and made things cleaner.
package com.mmgworldwide.layout;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import com.mmgworldwide.R;
import com.mmgworldwide.data.DataConnection;
import com.mmgworldwide.data.ImageThreadLoader;
import com.mmgworldwide.data.Personality;
//http://code.google.com/p/and-conference/source/browse/trunk/src/com/totsp/conference/PresentationList.java
public class Personalities extends Activity{
private ListView listView;
private ArrayAdapter<Object> adapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.personalities);
listView = (ListView) findViewById(R.id.personalitylistview);
listView.setDrawingCacheEnabled(false);
addListAdapter();
setFooterView("more");
}
private void addListAdapter(){
ArrayList<Object> adaptList = makeAdaptList();
adapter = new PersonalityListAdapter(this, 0, adaptList);
listView.setAdapter(adapter);
}
private ArrayList<Object> makeAdaptList(){
ArrayList<Object> adaptList = new ArrayList<Object>();
String currentDivDate = " ";
for(Personality aEvent : DataConnection.fwrInfo.getPersonalityList()){
adaptList.add(aEvent);
}
return adaptList;
}
private void setFooterView(String highlight){
Footer footer = (Footer) findViewById(R.id.footer);
footer.setHighlight(highlight);
}
//***********************************************************************************************************************************************
//ViewHolder
//***********************************************************************************************************************************************
static class ViewHolder {
private TextView tView1;
private TextView tView2;
private ImageView iView1;
}
//***********************************************************************************************************************************************
//PersonalityListAdapter
//***********************************************************************************************************************************************
private class PersonalityListAdapter extends ArrayAdapter<Object>{
LayoutInflater vi = (LayoutInflater) Personalities.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
private Context parentContext;
public ArrayList<Object> items_list_ref;
private ImageThreadLoader imageLoader = new ImageThreadLoader();
public PersonalityListAdapter(final Context context, final int resId, final ArrayList<Object> presentations) {
super(context, resId, presentations);
this.items_list_ref = presentations;
parentContext = context;
}
#Override
public int getCount() {
return items_list_ref.size();
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
//Log.d("TROY", "adding view");
View v = convertView;
if (v == null) {
//Log.d("TROY", "inflation");
v = vi.inflate(R.layout.picture_list, parent, false);
ViewHolder viewHolder = new ViewHolder();
viewHolder.tView1 = (TextView) v.findViewById(R.id.name_txt);
viewHolder.tView2 = (TextView) v.findViewById(R.id.title_txt);
viewHolder.iView1 = (ImageView) v.findViewById(R.id.portrait);
v.setTag(viewHolder);
}
Object p = items_list_ref.get(position);
if (p != null) {
//Log.d("TROY", "setting stuff");
ViewHolder viewHolder = (ViewHolder) v.getTag();
viewHolder.tView1.setText("hi");
}
return v;
}
}
}
Here is a screen shot of what's going on (why is the 5th preload graphic not showing?):
edit ------
Ok, I added the getCount and getItemId overrides but still have the same issues, I figured I would start a project from scratch and try again with just a listView, same issue, I have put up a test project to demonstrate this frustrating issue. In this test I did not even add any data to the adapters, and the animation only works for some of them, just like with my master project.
Thank you so much for your patience and help this far Michele, you have been very awesome.
http://faceonfire.com/temp/ListTester.zip

ListViews are highly optimized Datastructures. You should use ViewHolders to obey the rules of Lists and make the life of the system a bit easier.
When you Scroll a ListView the System caches the Items in Holders for performance.
Read How to load the Listview "smoothly" in android
you can also try to set
yourListView.setDrawingCacheEnabled(false);
edit: ok than try to set your animation with xml
http://developer.android.com/reference/android/graphics/drawable/AnimationDrawable.html
like:
<!-- Animation frames are wheel0.png -- wheel5.png files inside the
res/drawable/ folder -->
<animation-list android:id="selected" android:oneshot="false">
<item android:drawable="#drawable/wheel0" android:duration="50" />
<item android:drawable="#drawable/wheel1" android:duration="50" />
<item android:drawable="#drawable/wheel2" android:duration="50" />
<item android:drawable="#drawable/wheel3" android:duration="50" />
<item android:drawable="#drawable/wheel4" android:duration="50" />
<item android:drawable="#drawable/wheel5" android:duration="50" />
</animation-list>
edit2: please try to implement getCount and getItemId, theres something wrong with the boundarys of your List, maybe android cant find out on with position the list is after scrolling. Oh and can you please test it with random data, not the same item all the way (perhaps some weird hash (default getItemId Implementation?) that is always the same with the same item content).
for example like this:
#Override
public int getCount() {
return <yourdatastructure>.getSize();
}
#Override
public long getItemId(int position) {
return position;
}

Related

Picasso not displaying images in GridView

I've read several posts having similar problems yet I still can't make the fix.
The app I'm trying to build is supposed to retrieve images from a link and display them. This is done through Picasso and is supposed to be displayed in GridView format.
Here is my MovieAdapter.java:
package com.example.android.popularmovies.utilities;
import android.app.Activity;
import android.content.Context;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import com.example.android.popularmovies.R;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
/**
* Created by Jonathan on 6/18/2017.
*/
public class MovieAdapter extends ArrayAdapter<String>{
Context mContext;
int mLayoutResourceId;
ArrayList<String> mData = new ArrayList<>();
public MovieAdapter(Context context, int layoutResourceId,
ArrayList<String> data) {
super(context, layoutResourceId, data);
mLayoutResourceId = layoutResourceId;
mContext = context;
mData = data;
}
public void setData(ArrayList<String> mGridData) {
mData=mGridData;
notifyDataSetChanged();
}
//TODO code is working. Posters arent displayed :(
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
convertView = inflater.inflate(mLayoutResourceId, parent, false);
ImageView image = (ImageView) convertView.findViewById(R.id.iv_moviePoster);
convertView.setTag(image);
try {
String poster = NetworkUtils.posterBuilder(mData.get(position));
Picasso.with(mContext).
load(poster).
into(image);
}
catch(NullPointerException e) { e.printStackTrace(); }
return image;
}
#Override
public int getCount() {
return mData.size();
}
#Nullable
#Override
public String getItem(int position) {
return mData.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
}
When running the code, nothing is displayed. Please help and thank you
Update
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView
android:id="#+id/iv_moviePoster"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gv_movieData"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:columnWidth="100dp"
android:numColumns="2"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center" />
<ProgressBar
android:id="#+id/pb_loadingBar"
android:layout_width="42dp"
android:layout_height="42dp"
android:layout_gravity="center"
android:visibility="invisible"/>
</LinearLayout>
Permissions:
<uses-permission android:name="android.permission.INTERNET"/>

How can i make Objects from my Arraylist appear correctly in a Listview?

This is my first time asking a question here. Apologies if i phrase it incorrectly and please excuse my inexperience in programming.
I have been using this website to create a Listview that displays both the "name" and "level" attributes of a "Character"-object. However, for some reason the name and level never appear, as seen here. I want to solve that.
This is my MainActivity:
package com.example.laptoproeland.mydndproject;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import java.util.ArrayList;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<Character> arrayOfCharacters = new ArrayList<>();
arrayOfCharacters.add(new Character("Styx", 6));
arrayOfCharacters.add(new Character("Gulthrak", 1));
arrayOfCharacters.add(new Character("Edmund", 4));
arrayOfCharacters.add(new Character("K'naan", 3));
arrayOfCharacters.add(new Character("Helga", 1));
CharactersAdapter adapter = new CharactersAdapter(this, arrayOfCharacters);
ListView listView = (ListView) findViewById(R.id.listViewMain);
listView.setAdapter(adapter);
}
}
Here is my adapter:
package com.example.laptoproeland.mydndproject;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.ArrayList;
public class CharactersAdapter extends ArrayAdapter<Character> {
public CharactersAdapter(Context context, ArrayList<Character> arrayOfCharacters) {
super(context, 0, arrayOfCharacters);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
Character character = getItem(position);
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.character_item, parent, false);
}
TextView tvName = (TextView) convertView.findViewById(R.id.tvName);
TextView tvLevel = (TextView) convertView.findViewById(R.id.tvLevel);
tvName.setText(character.name);
tvLevel.setText(String.valueOf(character.level));
return convertView;
}
}
This is my character_item:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/name"
android:textSize="30sp"
android:textStyle="normal|bold" />
<TextView
android:id="#+id/tvLevel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/level"
android:layout_marginStart="10dp"
android:textSize="30sp" />
</LinearLayout>
If any other information or code is required to solve this, please tell me. Thank you.

RecyclerView childView always with the same id

MY PROBLEM
All views inside my RecyclerView, I am talking about elements(TextView and ImageView ....) are displayed well, normal but when i want to list all items inside the recyclerView with this:
for(int i=0;i<recyclerView.getChildCount();i++){
ids=ids+"-"+recyclerView.getChildAt(i).getId();
}
when i display ids always show me the same id which is the RelativeLayout id and it supose to display the id of the elements inside the RecyclerView as the TextView, ImageView and more
MY RESEARCH
i was following a video from slidenerd of
RecyclerView.OnItemTouchListener
and for him is working fine i dont know why for my is not working i was reading a lot about my problem to find the exact problem because:
First thing i did change the ids names for all my layouts to be different
Move the position of my code
Confirm that the data sent by MotionEvent e.getX() and y.getY() was correct and even thought i change the methods for getRaw
Read about RecyclerView class and find that there is not issue in this method findChildVIewUnder because all things that do are mathematicals
Read the id number of each important resource in R file an then convert it to dec to compare the prompt
I am close to the frustation
MY CODE
teacher_item
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/problem"
android:layout_width="328dp"
android:layout_height="130dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="10dp"
android:clickable="true"
android:background="#drawable/borders">
<ImageView
android:layout_width="90dp"
android:layout_height="100dp"
android:id="#+id/teacher_item_picture"
style="#style/TeacherItemImage" />
<RatingBar
android:layout_width="80dp"
android:layout_height="wrap_content"
android:id="#+id/teacher_item_ratingbar"
style="#style/TeacherItemRatingBar" />
<TextView
android:id="#+id/teacher_item_name"
android:layout_width="180dp"
android:layout_height="16.5dp"
style="#style/TeacherItemName" />
<TextView
android:id="#+id/teacher_item_courses"
android:layout_width="180dp"
android:layout_height="33dp"
style="#style/TeacherItemCourses" />
<TextView
android:id="#+id/teacher_item_faculties"
android:layout_width="180dp"
android:layout_height="33dp"
style="#style/TeacherItemFaculties" />
</RelativeLayout>
recyclerview.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:layout_width="wrap_content"
android:id="#+id/generic_listView"
android:layout_height="wrap_content"
style="#style/GenericListItem">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
teacherListFragment.javaimportant code
teacherListAdapter=new TeacherListAdapter(getActivity(),getData());
teacherListAdapter.setListener(this);
recyclerView.addItemDecoration(new RecyclerViewDivider(getActivity()));
recyclerView.setAdapter(teacherListAdapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.addOnItemTouchListener(new RecyclerViewClickListener(getActivity(), recyclerView, new RecyclerViewClickListener.OnItemClickListener() {
#Override
public void onItemClick(View view, int position) {
Log.d("D", view.getId() + "-" + position);
}
}));
getData() returns an List, this code is inside onCreateView
TeacherListAdatper
package wan.wanmarcos.views.adapters;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;
import com.squareup.picasso.Picasso;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import wan.wanmarcos.R;
import wan.wanmarcos.managers.ItemAdapterListener;
import wan.wanmarcos.managers.ViewHolderSetters;
import wan.wanmarcos.models.Teacher;
import wan.wanmarcos.utils.Constants;
import wan.wanmarcos.views.widgets.CircleTransform;
public class TeacherListAdapter extends RecyclerView.Adapter<TeacherListAdapter.TeacherHolder> {
private LayoutInflater inflater;
private List<Teacher> teachers= Collections.emptyList();
private ItemAdapterListener itemAdapterListener;
public TeacherListAdapter(Context context,List<Teacher> teachers){
inflater= LayoutInflater.from(context);
this.teachers=teachers;
}
#Override
public TeacherHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view=inflater.inflate(Constants.TEACHER_NEW_ITEM, parent, false);
TeacherHolder teacherCustomer = new TeacherHolder(view);
return teacherCustomer;
}
#Override
public void onBindViewHolder(TeacherHolder holder, int position) {
Teacher teacher=teachers.get(position);
holder.setElements(teacher);
}
public void setListener(ItemAdapterListener listener){
this.itemAdapterListener =listener;
}
#Override
public int getItemCount() {
return teachers.size();
}
public class TeacherHolder extends RecyclerView.ViewHolder implements ViewHolderSetters<Teacher>,View.OnClickListener{
private TextView teacherName;
private RatingBar teacherRating;
private ImageView teacherImage;
private TextView teacherCourses;
private TextView teacherAssumptions;
private Teacher current;
public TeacherHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
teacherName= (TextView) itemView.findViewById(R.id.teacher_item_name);
teacherRating=(RatingBar)itemView.findViewById(R.id.teacher_item_ratingbar);
teacherImage=(ImageView)itemView.findViewById(R.id.teacher_item_picture);
teacherCourses=(TextView)itemView.findViewById(R.id.teacher_item_courses);
teacherAssumptions=(TextView)itemView.findViewById(R.id.teacher_item_faculties);
}
#Override
public void setElements(Teacher elements) {
current=elements;
teacherName.setText(elements.getName());
teacherRating.setRating(elements.getRaiting());
teacherCourses.setText(itemView.getResources().getString(R.string.teacher_capabilities)+ elements.getFaculties());
teacherAssumptions.setText(itemView.getResources().getString(R.string.teacher_ped) + elements.getDescription());
Picasso.with(itemView.getContext()).load(elements.getImageUrl()).transform(new CircleTransform()).into(teacherImage);
}
#Override
public void onClick(View v) {
//itemAdapterListener.itemClicked(v,current);
}
}
}
WHAT I WANT IS: why child items of my RecyclerVIew are the layouts containers?
I need someone to help me Thanks

Create a ListView with Text and Favorite Icon based on SQLite Database Data

I want to create a list where the listview will display a textview and an icon for each row. The diagram should be as follows:
Other than that, The data is retrieved from the database. The attribute of "favorite" will be checked first and if true, then the image in listview will be assigned with favorite_icon.png. Else, no icon need to be assigned.
I have been search for the related answer and tutorial, but all of them is too different from what I want and I cannot understand it very much. Hope somebody here can help me. Thank you in advance.
Here is what I got after I have done my Homework
First we set up a custom layout for listview and also for the row of the listview we will use later.
Here is the custom_listview_main.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">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/customListView" />
</LinearLayout>
Here is the custom_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"
android:gravity="center_vertical"
android:minHeight="64dp">
<ImageView
android:id="#+id/clv_imageView"
android:layout_width="32dp"
android:layout_height="32dp"
android:contentDescription="#string/empty"
android:layout_alignParentRight="true"
android:layout_marginLeft="9dp"
android:layout_alignParentTop="true"/>
<TextView
android:id="#+id/clv_textView"
android:layout_width="97dp"
android:layout_height="32dp"
android:layout_alignParentLeft="true"
android:gravity="center_vertical"
android:text="#string/tv_definition"
android:textIsSelectable="true" />
</RelativeLayout>
Then we need to customized how our Custom ArrayAdaptor will look and do.
But before that, make sure you have put your icon in the drawable folder.
Here is my MyPerformanceArrayAdapter.java
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class MyPerformanceArrayAdapter extends ArrayAdapter<DefinitionObject>{
private List<DefinitionObject> entries;
private Activity activity;
public MyPerformanceArrayAdapter(Activity a, int textViewResourceId, List<DefinitionObject> entries) {
super(a, textViewResourceId, entries);
this.entries = entries;
this.activity = a;
}
public static class ViewHolder{
public TextView item1;
public ImageView item2;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
ViewHolder holder;
if (v == null) {
LayoutInflater vi =
(LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.custom_listview_row, null);
holder = new ViewHolder();
holder.item1 = (TextView) v.findViewById(R.id.clv_textView);
holder.item2 = (ImageView) v.findViewById(R.id.clv_imageView);
v.setTag(holder);
}
else
holder=(ViewHolder)v.getTag();
final DefinitionObject custom = entries.get(position);
if (custom != null) {
holder.item1.setText(custom.getWord());
if(custom.getFav().equalsIgnoreCase("0"))
{
holder.item2.setImageResource(R.drawable.fav);
holder.item2.setVisibility(holder.item2.INVISIBLE);
}
else
{
holder.item2.setImageResource(R.drawable.fav2);
}
}
return v;
}
}
And lastly, here is my Activity to View the ListView using the CustomArrayAdaptor.
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ListView;
public class TempCLV extends Activity {
private MySQLiteDefinitionHelper db;
String tblName = "";
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_listview_main);
Intent msjIntent = getIntent();
tblName = msjIntent.getStringExtra(WordDefinitionHomeActivity.TABLENAME2);
refresh();
}
public void refresh()
{
db = new MySQLiteDefinitionHelper(this);
final List<DefinitionObject> values = db.getAllWords(tblName);
ListView mylist = (ListView)findViewById(R.id.customListView);
MyPerformanceArrayAdapter adapter = new MyPerformanceArrayAdapter(this, R.id.customListView, values);
mylist.setAdapter(adapter);
}
}

How can i show running app icon in listview using ActivityManager and PackageManager?

I'm trying to making a app that..
-shows currently running app icons in listview
-click item on listview that shows running app icons to switch
But i don't know how to do these, and i already googled a lot
but i found that i should use ActivityManager.RunningTaskinfo and Packagemanager to do these
Cloud you help me to do these?
How can i show running app icons in listview and give listview click event(such as OnItemClick)that switch to app which is clicked on listview?
[LeftSidePanel.java]
package kr.hybdms.sidepanel;
import java.util.ArrayList;
import java.util.List;
import kr.hybdms.sidepanel.PanelArrayAdapter;
import kr.hybdms.sidepanel.R;
import kr.hybdms.sidepanel.util.SystemUiHider;
import android.app.Activity;
import android.content.pm.PackageInfo;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;
/**
* An example full-screen activity that shows and hides the system UI (i.e.
* status bar and navigation/system bar) with user interaction.
*
* #see SystemUiHider
*/
public class LeftSidePanel extends Activity implements
OnItemClickListener {
ListView listView;
List<PanelItemDetail> rowItems;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_left_side_panel);
List<PackageInfo> packs = getPackageManager().getInstalledPackages(10);
Drawable[] images = new Drawable[packs.size()];
for(int i=0;i<packs.size();i++) {
PackageInfo p = packs.get(i);
images[i]= p.applicationInfo.loadIcon(getPackageManager());
}
rowItems = new ArrayList<PanelItemDetail>();
listView = (ListView) findViewById(R.id.panelcontents);
PanelArrayAdapter adapter = new PanelArrayAdapter(this,
R.layout.panelrow, rowItems);
listView.setAdapter(adapter);
listView.setOnItemClickListener(this);
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Toast toast = Toast.makeText(getApplicationContext(),
"Item " + (position + 1) + ": " + rowItems.get(position),
Toast.LENGTH_SHORT);
toast.setGravity(Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0);
toast.show();
}
}
[PanelArrayAdapter.java]
package kr.hybdms.sidepanel;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
public class PanelArrayAdapter extends ArrayAdapter<PanelItemDetail> {
Context context;
public PanelArrayAdapter(Context context, int resourceId,
List<PanelItemDetail> items) {
super(context, resourceId, items);
this.context = context;
}
/*private view holder class*/
private class ViewHolder {
ImageView imageView;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
PanelItemDetail rowItem = getItem(position);
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.panelrow, null);
holder = new ViewHolder();
holder.imageView = (ImageView) convertView.findViewById(R.id.appicon);
convertView.setTag(holder);
} else
holder = (ViewHolder) convertView.getTag();
holder.imageView.setImageDrawable(rowItem.getImageId());
return convertView;
}
}
[PanelItemDetail.java]
package kr.hybdms.sidepanel;
import android.graphics.drawable.Drawable;
public class PanelItemDetail {
private Drawable imageId;
public PanelItemDetail(Drawable images) {
this.imageId = images;
}
public Drawable getImageId() {
return imageId;
}
public void setImageId(Drawable imageId) {
this.imageId = imageId;
}
}
[panelrow.xml]
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="#+id/appicon"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_margin="10dp" />
</RelativeLayout>
[activity_left_side_panel.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"
tools:context=".LeftSidePanel" >
<ImageView android:id="#+id/transparentbackground"
android:src="#drawable/detector"
android:adjustViewBounds="true"
android:gravity="center_vertical"
android:scaleType="fitXY"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_x="0dp"
android:layout_y="0dp"
android:layout_marginLeft="100dp"/>
<ImageView
android:id="#+id/panelbackground"
android:layout_width="100dp"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:scaleType="fitXY"
android:src="#drawable/panelbg" />
<ListView
android:id="#+id/panelcontents"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignRight="#+id/panelbackground"
android:layout_centerVertical="true"
android:listSelector="#drawable/panel_item_bg"
android:divider="#000000" >
</ListView>
</RelativeLayout>
Try going through this tutorial it may help you
here!

Categories

Resources