access non final variable from anonymous inner class - java

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.

Related

Refresh ListView when item in adapter is removed

I have created a ListView of items that each one of them looks as follows:
In simplified code, It looks like this:
public class MyBAdapter extends ArrayAdapter {
private static class ViewHolder {
public ImageButton ib_Delete;
public ToggleButton tb_Status;
}
public MyBAdapter(Context context, ArrayList<MyB> aB) {
super(context, 0, aB);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final MyB b = getItem(position);
ViewHolder viewHolder;
if (convertView == null) {
viewHolder = new ViewHolder();
LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate( R.layout.item_b_myb, parent, false);
viewHolder.ib_DeleteB = convertView.findViewById( R.id.ib_DeleteB );
viewHolder.tb_Status = convertView.findViewById( R.id.tb_Status );
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
return convertView;
}
}
Now, I had like that once a user clicks on the ToggleButton of on the x button, that it will remove this item from the list and that the view will be updated on this screen.
How can I do it?
I saw that for this case notifyDataSetChanged() won't work.
Basically what I'm trying to do is to refresh the list without calling again the activity.
Thank you
You can try like this:
public static class MyBAdapter extends ArrayAdapter {
private ArrayList<String> aB;
private class ViewHolder {
public ImageButton ib_Delete;
public ToggleButton tb_Status;
public TextView text;
}
public MyBAdapter(Context context, ArrayList<String> aB) {
super(context,0, aB);
this.aB = aB;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
viewHolder = new ViewHolder();
LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate( R.layout.item_b_myb, parent, false);
viewHolder.ib_Delete = convertView.findViewById( R.id.ib_DeleteB );
viewHolder.tb_Status = convertView.findViewById( R.id.tb_Status );
viewHolder.text = convertView.findViewById( R.id.text );
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
viewHolder.tb_Status.setChecked(false);
}
viewHolder.text.setText(aB.get(position));
viewHolder.ib_Delete.setTag(position);
viewHolder.ib_Delete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(viewHolder.tb_Status.isChecked()){
delete((Integer)view.getTag());
}
}
});
return convertView;
}
private void delete(int position) {
aB.remove(position);
notifyDataSetChanged();
}
#Override
public int getCount() {
return aB.size();
}
}

Why the NullPointerException happened when call notifyDataSetChanged?

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));

My simple custom listview show wrong items, not working as intended. Can't see what is wrong

I am trying to create something like this: https://i.stack.imgur.com/l8ZOc.png
However, i ran into a problem. When i create the list with my adapter, it is supposed to be a list of 8 items. However, it just shows the first 4 of these items in a random order two times. Do you see what is wrong with my code?
public class MyActivity extends Activity{
String headers[];
String image_urls[];
List<MyMenuItem> menuItems;
ListView mylistview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
menuItems = new ArrayList<MyMenuItem>();
headers = getResources().getStringArray(R.array.header_names);
image_urls = getResources().getStringArray(R.array.image_urls);
for (int i = 0; i < headers.length; i++) {
MyMenuItem item = new MyMenuItem(headers[i], image_urls[i]);
menuItems.add(item);
}
mylistview = (ListView) findViewById(R.id.list);
MenuAdapter adapter = new MenuAdapter(this, menuItems);
mylistview.setAdapter(adapter);
mylistview.setOnItemClickListener(this);
}
public class MyMenuItem {
private String item_header;
private String item_image_url;
public MyMenuItem(String item_header, String item_image_url){
this.item_header=item_header;
this.item_image_url=item_image_url;
}
public String getItem_header(){
return item_header;
}
public void setItem_header(String item_header){
this.item_header=item_header;
}
public String getItem_image_url(){
return item_image_url;
}
public void setItem_image_url(String item_image_url){
this.item_image_url=item_image_url;
}
}
public class MenuAdapter extends BaseAdapter{
Context context;
List<MyMenuItem> menuItems;
MenuAdapter(Context context, List<MyMenuItem> rowItems) {
this.context = context;
this.menuItems = rowItems;
}
#Override
public int getCount() {
return menuItems.size();
}
#Override
public Object getItem(int position) {
return menuItems.get(position);
}
#Override
public long getItemId(int position) {
return menuItems.indexOf(getItem(position));
}
private class ViewHolder {
ImageView ivMenu;
TextView tvMenuHeader;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.menu_item, null);
holder = new ViewHolder();
holder.tvMenuHeader = (TextView) convertView.findViewById(R.id.tvMenuHeader);
holder.ivMenu = (ImageView) convertView.findViewById(R.id.ivMenuItem);
MyMenuItem row_pos = menuItems.get(position);
Picasso.with(context)
.load(row_pos.getItem_image_url())
.placeholder(R.drawable.empty)
.error(R.drawable.error)
.into(holder.ivMenu);
holder.tvMenuHeader.setText(row_pos.getItem_header());
Log.e("Test", "headers:" + row_pos.getItem_header());
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
return convertView;
}
}
You are setting the data only when the convertView is null.So when it is not null,list items are inflated with data of previous list items. getView should be something like this
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.menu_item, null);
holder = new ViewHolder();
holder.tvMenuHeader = (TextView) convertView.findViewById(R.id.tvMenuHeader);
holder.ivMenu = (ImageView) convertView.findViewById(R.id.ivMenuItem);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
MyMenuItem row_pos = menuItems.get(position);
Picasso.with(context)
.load(row_pos.getItem_image_url())
.placeholder(R.drawable.empty)
.error(R.drawable.error)
.into(holder.ivMenu);
holder.tvMenuHeader.setText(row_pos.getItem_header());
Log.e("Test", "headers:" + row_pos.getItem_header());
return convertView;
}
I think this is the solution
else {
holder = (ViewHolder) convertView.getTag();
holder.tvMenuHeader.setText(row_pos.getItem_header());
}
I also encountered this problem earlier and this one works for me.

Custom Adapter No Id Value

Hi im trying to populate a listview from a baseadapter and when selected on a listview the wrong id is propagated.
here is my adapter
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) {
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;
}
}
Any Help you can provide would be great
More code can be availble upon request
Your getItemId() should return id.
So it should be like this->
public long getItemId(int position) {
return AList.get(position).getID();
}
Assuming that ID is long, if it is not then use -> Long.parseLong(AList.get(position).getID());

How to get view for an item in listview in android?

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();
}
}
});

Categories