I get stucked and I need help. I'm trying to use set and get Tag but i can't get how it works for this action:
The custom Adapter
@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.itemstartsession, null);
holder = new ViewHolder();
holder.image = (WebView)convertView.findViewById(R.id.img_session);
//holder.image.setTag(position);
holder.code = (TextView)convertView.findViewById(R.id.code_item_session_text);
//holder.code.setTag(position);
holder.share=(ImageButton)convertView.findViewById(R.id.share_item_session_button);
holder.share.setTag(position);
convertView.setTag(holder);
// Check if my setTag is ok for button and get the reference to get
//text from textview and the referece to webview, then I gonna load a url
} else {
holder=(ViewHolder)convertView.getTag();
}
StoreDataForBA storeItem= (StoreDataForBA) getItem(position);
holder.image.loadUrl(storeItem.getImage());
holder.code.setText(storeItem.getCode());
return convertView;
}
This is my getter and setter for data, very easy
public StoreDataForBA( String image, String code) {
this.setImage(image);
this.setCode(code);
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
My principal Activity snipped
public void shareOnClickHandler(View v) {
// plz here i need the code to get the text from textview and also get the
// reference of the webview, so i can do something like
// StoreDataForBA data = (StoreDataForBA)v.getTag();
// image2.loadUrl("http://image2")..... I'm not sure, thank you
}
Attaching the Adapter to a ListView // Construct the data source ArrayList<User> arrayOfUsers = new ArrayList<User>(); // Create the adapter to convert the array to views UsersAdapter adapter = new UsersAdapter(this, arrayOfUsers); // Attach the adapter to a ListView ListView listView = (ListView) findViewById(R. id.
Go to app > res > layout > right-click > New > Layout Resource File and create a new layout file and name this file as item_view. xml and make the root element as a LinearLayout. This will contain a TextView that is used to display the array objects as output.
You are very close to your answer. Just Follow the changes and complete your answer
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if ((convertView == null) || (convertView.getTag() == null)) {
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.list_item, null);
holder = new ViewHolder();
} else {
holder = (ViewHolder) convertView.getTag();
}
convertView.setTag(holder);
return convertView;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With