I am trying to update the ListView when the Data is update in the Android.
I use mLeDeviceListAdapter.addRSSIandAddress("20"); to set the value to addRSSIandAddress function.
And in the addRSSIandAddress , it receive the value and call mLeDeviceListAdapter.notifyDataSetChanged();. But the NullPointerException happened at the viewHolder.deviceRssi.setText("RSSI:" + String.valueOf(mrssi));
It seems the viewHolder.deviceRssi is null or the viewHolder is null.
The code of ListView and the Adapter is like the following:
private LeDeviceListAdapter mLeDeviceListAdapter;
private static ListView device_list;
mLeDeviceListAdapter = new LeDeviceListAdapter(getActivity());
device_list.setAdapter(mLeDeviceListAdapter);
public class LeDeviceListAdapter extends BaseAdapter{
private LayoutInflater mInflator;
ArrayList<BluetoothDevice> mLeDevices;
private String mrssi;
public LeDeviceListAdapter(Context context) {
// TODO Auto-generated constructor stub
mInflator = LayoutInflater.from(context);
}
private void addRSSIandAddress(String rssi) {
// TODO Auto-generated method stub
mrssi = rssi;
mLeDeviceListAdapter.notifyDataSetChanged();
}
#Override
public View getView(final int position, View view, ViewGroup parent) {
// TODO Auto-generated method stub
final ViewHolder viewHolder;
Log.d(TAG, "getView");
if(view == null){
view = mInflator.inflate(R.layout.device_list, null);
viewHolder = new ViewHolder();
viewHolder.deviceRssi = (TextView) view.findViewById(R.id.device_rssi);
}else {
viewHolder = (ViewHolder) view.getTag();
viewHolder.deviceRssi.setText("RSSI:" + String.valueOf(mrssi));
}
return view;
}
}
static class ViewHolder {
TextView deviceRssi;
}
How to solve this problem ? Thanks in advance.
As the others have said. I think you forgot to set
view.setTag(viewHolder).
Try Put this line here In your code
if(view == null)
{
view = mInflator.inflate(R.layout.device_list, null);
viewHolder = new ViewHolder();
viewHolder.deviceRssi = (TextView) view.findViewById(R.id.device_rssi);
view.setTag(viewHolder); //THE CODE
}
else
{
viewHolder = (ViewHolder) view.getTag();
viewHolder.deviceRssi.setText("RSSI:" + String.valueOf(mrssi));
}
return view;
You use:
viewHolder = (ViewHolder) view.getTag();
But do you setTag somewhere? I don't think so.
Hope it helps
Try with below code:
if(view == null){
view = mInflator.inflate(R.layout.device_list, null);
viewHolder = new ViewHolder();
viewHolder.deviceRssi = (TextView) view.findViewById(R.id.device_rssi);
view.setTag(viewHolder);
}else {
viewHolder = (ViewHolder) view.getTag();
}
viewHolder.deviceRssi.setText("RSSI:" + String.valueOf(mrssi));
Related
public class FoodTypeAdapter extends BaseAdapter {
private Context context;
public FoodTypeAdapter(Context context) {
this.context = context;
}
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder = null;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.food_type_grid,
new LinearLayout(context));
holder = new ViewHolder();
holder.btnAdd = (Button) convertView.findViewById(R.id.btnItemAdd);
holder.etQty = (EditText) convertView.findViewById(R.id.etfItemQty);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.btnAdd.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String quantity = holder.etQty.getText().toString();
System.out.println(quantity);
}
});
return convertView;
}
public int getCount() {
return mylist.size();
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
static class ViewHolder {
Button btnAdd;
EditText etQty;
}
}
In this code without make holder as final Object I can't access it from OnClickListener.
If I make it as final Object I can't initiate holder like holder = new ViewHolder();.
Now what can I do? Any help will be highly appreciable.
Thanks,
Guna
You also could have simply adjusted the initialization of holder which reduced your code complexity by the way:
final ViewHolder holder;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.food_type_grid,
new LinearLayout(context));
holder = new ViewHolder();
holder.btnAdd = (Button) convertView.findViewById(R.id.btnItemAdd);
holder.etQty = (EditText) convertView.findViewById(R.id.etfItemQty);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
You can work around it by adding a temporary final variable before the anonymous class, and use that variable instead:
final ViewHolder finalHolder = holder; // <- added
holder.btnAdd.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String quantity = finalHolder.etQty.getText().toString();
// ^^^^^^^^^^^
System.out.println(quantity);
}
});
Or, you could make holder a member variable.
I have arraylist.Size of arraylist is 156.when I am running for loop it's running 156 times and it's adding all values into another arraylist.But when I am setting this arraylist into adapter adapter taking 156 items but when I am setting adapter into listview that time Listview taking only 98 items only.
When I am debugging I seen arralist size 156.and arraylist deviding into sets like
0-99
100-199
200-210
see below image :
Here my adapter taking only 0-99 items.How I can add 100-199 set Items into Listview.
Edit #1:
Adapter class :
public class AllserversListAdapter extends BaseAdapter implements OnClickListener {
Activity mActivity;
ArrayList<AllServers> mAllServersArray;
private static LayoutInflater mInflater = null;
ArrayList<MyFavorites> mFavoriteServers;
public AllserversListAdapter(Activity activity,
ArrayList<AllServers> allServers,
ArrayList<MyFavorites> favoriteServers) {
// TODO Auto-generated constructor stub
mActivity = activity;
mAllServersArray = allServers;
mFavoriteServers = favoriteServers;
mInflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return mAllServersArray.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
ViewHolder holder;
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View vi = convertView;
if (vi == null) {
/****** Inflate list items layout file for each row ( Defined below ) *******/
vi = mInflater.inflate(R.layout.servers_list_items, null);
/****** View Holder Object to contain list items file elements ******/
holder = new ViewHolder();
holder.serverName = (TextView) vi
.findViewById(R.id.server_items_country_name_textview);
holder.speed = (TextView) vi
.findViewById(R.id.server_items_speed_textview);
holder.speedTitleTextView = (TextView) vi
.findViewById(R.id.speed_title_textview);
holder.flag = (ImageView) vi.findViewById(R.id.server_flag_image);
holder.star = (ImageView) vi.findViewById(R.id.server_items_star);
holder.bodyLayout = (LinearLayout) vi
.findViewById(R.id.servere_items_boady_layout);
holder.speedBackgroundLayout = (LinearLayout) vi
.findViewById(R.id.speed_backround_layout);
holder.itemsParentLayout = (LinearLayout) vi
.findViewById(R.id.server_list_items_parentlayout);
holder.itemsParentLayout.setBackgroundColor(mActivity
.getResources().getColor(R.color.light_gray));
holder.bodyLayout.setBackgroundColor(mActivity.getResources()
.getColor(R.color.dark_gray_black));
holder.star.setOnClickListener(this);
/************ Set holder with LayoutInflater ************/
vi.setTag(holder);
holder.star.setTag(position);
holder.flag.setTag(position);
} else {
holder = (ViewHolder) vi.getTag();
}
String ip = mAllServersArray.get(position).getIp();
String country = mAllServersArray.get(position).getCountry();
String region = mAllServersArray.get(position).getRegion();
String speed = mAllServersArray.get(position).getSpeed();
String flagCountryCode = mAllServersArray.get(position)
.getCountrycode();
int ipSpeed = Integer.valueOf(speed);
holder.speedTitleTextView.setVisibility(View.GONE);
holder.speed.setText("TimeOut");
holder.speed.setTypeface(Typeface.DEFAULT);
holder.speed.setPadding(26, 0, 26, 0);
holder.speedBackgroundLayout.setBackgroundColor(mActivity
.getResources().getColor(R.color.time_out_color));
holder.serverName.setText(country + "\n" + region + "\n" + ip);
return vi;
}
}
Adding items into arraylist :
Any one give me suggest.
Try this way,hope this will help you to solve your problem.
public class AllserversListAdapter extends BaseAdapter{
Context context;
ArrayList<AllServers> allServers;
ArrayList<MyFavorites> favoriteServers;
public AllserversListAdapter(Context context,ArrayList<AllServers> allServers,ArrayList<MyFavorites> favoriteServers) {
this.context = context;
this.allServers = allServers;
this.favoriteServers = favoriteServers;
}
#Override
public int getCount() {
return mAllServersArray.size();
}
#Override
public Object getItem(int position) {
return mAllServersArray.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(R.layout.servers_list_items, null);
holder.serverName = (TextView) convertView.findViewById(R.id.server_items_country_name_textview);
holder.speed = (TextView) convertView.findViewById(R.id.server_items_speed_textview);
holder.speedTitleTextView = (TextView) convertView.findViewById(R.id.speed_title_textview);
holder.flag = (ImageView) convertView.findViewById(R.id.server_flag_image);
holder.star = (ImageView) convertView.findViewById(R.id.server_items_star);
holder.bodyLayout = (LinearLayout) convertView.findViewById(R.id.servere_items_boady_layout);
holder.speedBackgroundLayout = (LinearLayout) convertView.findViewById(R.id.speed_backround_layout);
holder.itemsParentLayout = (LinearLayout) convertView.findViewById(R.id.server_list_items_parentlayout);
holder.itemsParentLayout.setBackgroundColor(mActivity.getResources().getColor(R.color.light_gray));
holder.bodyLayout.setBackgroundColor(mActivity.getResources().getColor(R.color.dark_gray_black));
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
String ip = mAllServersArray.get(position).getIp();
holder.speedTitleTextView.setVisibility(View.GONE);
holder.speed.setText("TimeOut");
holder.speed.setTypeface(Typeface.DEFAULT);
holder.speed.setPadding(26, 0, 26, 0);
holder.speedBackgroundLayout.setBackgroundColor(mActivity
.getResources().getColor(R.color.time_out_color));
holder.serverName.setText(mAllServersArray.get(position).getCountry() + "\n" + mAllServersArray.get(position).getRegion() + "\n" + ip);
holder.flag.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// try use (position) value to check which list item click.
}
});
holder.star.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// try use (position) value to check which list item click.
}
});
return convertView;
}
class ViewHolder {
TextView serverName;
TextView speed;
TextView speedTitleTextView;
ImageView flag;
ImageView star;
LinearLayout bodyLayout;
LinearLayout speedBackgroundLayout;
LinearLayout itemsParentLayout;
}
}
In my display adapter ptrelevant I visualize data from a database. I wish the M value of the column relative to the TextView turn, was colored red. How can I do this?
public DisplayAdapter(Context c, ArrayList<String> id,ArrayList<String> ore,ArrayList<String> turno) {
this.mContext = c;
this.id = id;
this.turnoName = turno;
this.oreName = ore;
}
public int getCount() {
// TODO Auto-generated method stub
return id.size();
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public View getView(int pos, View child, ViewGroup parent) {
Holder mHolder;
LayoutInflater layoutInflater;
if (child == null) {
layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
child = layoutInflater.inflate(R.layout.row, null);
mHolder = new Holder();
mHolder.txt_turno = (TextView) child.findViewById(R.id.txt_turno);
mHolder.txt_ore = (TextView) child.findViewById(R.id.txt_ore);
mHolder.txt_id = (TextView) child.findViewById(R.id.txt_id);
child.setTag(mHolder);
} else {
mHolder = (Holder) child.getTag();
}
mHolder.txt_turno.setText(turnoName.get(pos));
mHolder.txt_ore.setText(oreName.get(pos));
return child;
}
public class Holder {
TextView txt_turno;
TextView txt_ore;
TextView txt_id;
}
}
Weird...that works for me:
try this :
mHolder.txt_turno.setText(turnoName.get(pos));
mHolder.txt_ore.setText(oreName.get(pos));
mHolder.txt_id.setText(dataName.get(pos));
String Turno = turnoName.get(pos);
mHolder.txt_turno.setTextColor(Color.WHITE);
if (Turno != null){
if (TextUtils.equals(Turno,"M"))
mHolder.txt_turno.setTextColor(Color.RED);
if (TextUtils.equals(Turno,"F"))
mHolder.txt_turno.setTextColor(Color.YELLOW);
}
return child;
}
hope that help you
Try adding the text in HTML format. Im pretty sure this will change the color. Your code should look like:
public View getView(int pos, View child, ViewGroup parent) {
Holder mHolder;
LayoutInflater layoutInflater;
if (child == null) {
layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
child = layoutInflater.inflate(R.layout.row, null);
mHolder = new Holder();
mHolder.txt_turno = (TextView) child.findViewById(R.id.txt_turno);
mHolder.txt_ore = (TextView) child.findViewById(R.id.txt_ore);
mHolder.txt_id = (TextView) child.findViewById(R.id.txt_id);
child.setTag(mHolder);
} else {
mHolder = (Holder) child.getTag();
}
if (turnoName.get(pos).toString().equalsIgnoreCase("M")){
mHolder.txt_turno.setText(Html.fromHtml("<font color='#FF0000'>"+turnoName.get(pos)+"</font>"));
}else if(turnoName.get(pos).toString().equalsIgnoreCase("F")){
mHolder.txt_turno.setText(Html.fromHtml("<font color='#FFFF00'>"+turnoName.get(pos)+"</font>"));
}else{
mHolder.txt_turno.setText(Html.fromHtml("<font color='#FFFFFF'>"+turnoName.get(pos)+"</font>"));
}
mHolder.txt_ore.setText(oreName.get(pos));
return child;
}
Change your getView with the code posted above.
Hope it helps!
Hi Im trying to create a CustomFilter for my BaseAdapter and am not having much luck .. i query my database for an array of the ID, Name and Sex. and on the listview i wish to search the list i have implemented the textview and have had no luck with the code.
here is my CustomAdapter
public class MyCustomBaseAdapter extends BaseAdapter {
private static ArrayList<Custom> AList;
private LayoutInflater inflat;
public MyCustomBaseAdapter(Context context, ArrayList<Custom> results) {
AList = results;
inflat = LayoutInflater.from(context);
}
public int getCount() {
return AList.size();
}
public Object getItem(int position) {
return AList.get(position);
}
public long getItemId(int position) {
Long.parseLong(AList.get(position).getID());
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = inflat.inflate(R.layout.suspect_list, null);
holder = new ViewHolder();
holder.txtName = (TextView) convertView.findViewById(R.id.suspect_name);
holder.txtSex = (TextView) convertView.findViewById(R.id.suspect_sex);
holder.txtId = (TextView) convertView.findViewById(R.id.suspect_id);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.txtName.setText(AList.get(position).getName());
holder.txtSex.setText(AList.get(position).getSex());
holder.txtId.setText(AList.get(position).getID());
return convertView;
}
static class ViewHolder {
TextView txtName;
TextView txtSex;
TextView txtId;
}
}
I used Custom Filter in MultiSelectionList. Here is the Code.
Please download the demo, run and try to get understand and use that code as per your needs with modification as per your requirement.
Thanks.
Is it possible to get an item view based on its position in the adapter and not in the visible views in the ListView?
I am aware of functions like getChildAt() and getItemIdAtPosition() however they provide information based on the visible views inside ListView. I am also aware that Android recycles views which means that I can only work with the visible views in the ListView.
My objective is to have a universal identifier for each item since I am using CursorAdapter so I don't have to calculate the item's position relative to the visible items.
Here's how I accomplished this. Within my (custom) adapter class:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
if (convertView == null) {
LayoutInflater inflater = context.getLayoutInflater();
view = inflater.inflate(textViewResourceId, parent, false);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.name = (TextView) view.findViewById(R.id.name);
viewHolder.button = (ImageButton) view.findViewById(R.id.button);
viewHolder.button.setOnClickListener
(new View.OnClickListener() {
#Override
public void onClick(View view) {
int position = (int) viewHolder.button.getTag();
Log.d(TAG, "Position is: " +position);
}
});
view.setTag(viewHolder);
viewHolder.button.setTag(items.get(position));
} else {
view = convertView;
((ViewHolder) view.getTag()).button.setTag(items.get(position));
}
ViewHolder holder = (ViewHolder) view.getTag();
return view;
}
Essentially the trick is to set and retrieve the position index via the setTag and getTag methods. The items variable refers to the ArrayList containing my custom (adapter) objects.
Also see this tutorial for in-depth examples. Let me know if you need me to clarify anything.
See below code:
public static class ViewHolder
{
public TextView nm;
public TextView tnm;
public TextView tr;
public TextView re;
public TextView membercount;
public TextView membernm;
public TextView email;
public TextView phone;
public ImageView ii;
}
class ImageAdapter extends ArrayAdapter<CoordinatorData>
{
private ArrayList<CoordinatorData> items;
public FoodDriveImageLoader imageLoader;
public ImageAdapter(Context context, int textViewResourceId,ArrayList<CoordinatorData> items)
{
super(context, textViewResourceId, items);
this.items = items;
}
public View getView(int position, View convertView, ViewGroup parent)
{
View v = convertView;
ViewHolder holder = null;
if (v == null)
{
try
{
holder=new ViewHolder();
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader = new FoodDriveImageLoader(FoodDriveModule.this);
v = vi.inflate(R.layout.virtual_food_drive_row, null);
//System.out.println("layout is null.......");
holder.nm = (TextView) v.findViewById(R.id.name);
holder.tnm = (TextView) v.findViewById(R.id.teamname);
holder.tr = (TextView) v.findViewById(R.id.target);
holder.re = (TextView) v.findViewById(R.id.received);
holder.membercount = new TextView(FoodDriveModule.this);
holder.membernm = new TextView(FoodDriveModule.this);
holder.email = new TextView(FoodDriveModule.this);
holder.phone = new TextView(FoodDriveModule.this);
holder.ii = (ImageView) v.findViewById(R.id.icon);
v.setTag(holder);
}
catch(Exception e)
{
System.out.println("Excption Caught"+e);
}
}
else
{
holder=(ViewHolder)v.getTag();
}
CoordinatorData co = items.get(position);
holder.nm.setText(co.getName());
holder.tnm.setText(co.getTeamName());
holder.tr.setText(co.getTarget());
holder.re.setText(co.getReceived());
holder.ii.setTag(co.getImage());
imageLoader.DisplayImage(co.getImage(), FoodDriveModule.this , holder.ii);
if (co != null)
{
}
return v;
}
}
A better option is to identify using the data returned by the CursorAdapter rather than visible views.
For example if your data is in a Array , each data item has a unique index.
Here, Its very short described code, you can simple re-use viewholder pattern to increase listview performance
Write below code in your getview() method
ViewHolder holder = new ViewHolder();
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.skateparklist, null);
holder = new ViewHolder();
holder.headlineView = (TextView) convertView
.findViewById(R.id.textView1);
holder.DistanceView = (TextView) convertView
.findViewById(R.id.textView2);
holder.imgview = (NetworkImageView) convertView
.findViewById(R.id.imgSkatepark);
convertView.setTag(holder); //PLEASE PASS HOLDER AS OBJECT PARAM , CAUSE YOU CAN NOY PASS POSITION IT WILL BE CONFLICT Holder and Integer can not cast
//BECAUSE WE NEED TO REUSE CREATED HOLDER
} else {
holder = (ViewHolder) convertView.getTag();
}
// your controls/UI setup
holder.DistanceView.setText(strDistance);
......
return convertview
Listview lv = (ListView) findViewById(R.id.previewlist);
final BaseAdapter adapter = new PreviewAdapter(this, name, age);
confirm.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
View view = null;
String value;
for (int i = 0; i < adapter.getCount(); i++) {
view = adapter.getView(i, view, lv);
Textview et = (TextView) view.findViewById(R.id.passfare);
value=et.getText().toString();
Toast.makeText(getApplicationContext(), value,
Toast.LENGTH_SHORT).show();
}
}
});