Sunday, March 2, 2014

BaseAdapter optimization problem


Learn BaseAdapter optimization problems, but also to optimize the feeling that it would like to write their own test methods, the picture is not always fully loaded only one, someone help me find or tell me conertView bottom in the end how it happened, how it it can not be replaced
 
public class baseAdapterActivity1 extends Activity {
LayoutInflater li;
ViewHolder vh = new ViewHolder ();
ListView lv;
int [] pp = {R.drawable.b, R.drawable.bg4, R.drawable.m, R.drawable.m2, R.drawable.m3, R.drawable.m8, R.drawable.p, R. drawable.psb};
@ Override
protected void onCreate (Bundle savedInstanceState) {
/ / TODO Auto-generated method stub
super.onCreate (savedInstanceState);
setContentView (R.layout.baselist);
lv = (ListView) findViewById (R.id.b_lv);
li = getLayoutInflater ();
vh.cont = li.inflate (R.layout.b_item, null);
vh.tv = (TextView) vh.cont.findViewById (R.id.b_tv);
vh.iv = (ImageView) vh.cont.findViewById (R.id.b_iv);
lv.setAdapter (new BaseAdapter () {

@ Override
public View getView (int position, View convertView, ViewGroup parent) {
/ / TODO Auto-generated method stub
View v = vh.cont;
vh.tv.setText ("image" + (position +1));
vh.iv.setImageResource (pp [position]);
System.out.println (position);
return v;
}

@ Override
public long getItemId (int position) {
/ / TODO Auto-generated method stub
return position;
}

@ Override
public Object getItem (int position) {
/ / TODO Auto-generated method stub
return null;
}

@ Override
public int getCount () {
/ / TODO Auto-generated method stub
return pp.length;
}
});
lv.setOnItemClickListener (new OnItemClickListener () {

@ Override
public void onItemClick (AdapterView arg0, View arg1, int arg2,
long arg3) {
/ / TODO Auto-generated method stub
Intent in = new Intent ("look");
in.putExtra ("pp", pp);
in.putExtra ("p", arg2);
startActivity (in);
}
});

}
static class ViewHolder {
View cont;
TextView tv;
ImageView iv;
}
}
<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
BaseAdapter prototype:
 
lv = (ListView) findViewById (R.id.b_lv);
li = getLayoutInflater ();

/ / Vh.tv = (TextView) vh.cont.findViewById (R.id.b_tv);
/ / Vh.iv = (ImageView) vh.cont.findViewById (R.id.b_iv);
lv.setAdapter (new BaseAdapter () {

@ Override
public View getView (int position, View convertView, ViewGroup parent) {
/ / TODO Auto-generated method stub
View v = convertView;
if (v == null) {
v = li.inflate (R.layout.b_item, null);
vh.tv = (TextView) v.findViewById (R.id.b_tv);
vh.iv = (ImageView) v.findViewById (R.id.b_iv);
v.setTag (vh);
}
else
vh = (ViewHolder) v.getTag ();
vh.tv.setText ("image" + (position +1));
vh.iv.setImageResource (pp [position]);
System.out.println (position);
return v;
}

Reply:
Nobody can give you to solve the next
Reply:
I understand View convertView refers listview inside each row view, is defined. You need to do is modify the convertview basis, rather than replace him.

if (convertView == null) {
convertView = li.inflate (R.layout.b_item, null);
vh.tv = (TextView) v.findViewById (R.id.b_tv);

vh = new ViewHolder ();
vh.tv = (TextView) v.findViewById (R.id.b_tv);
vh.iv = (ImageView) v.findViewById (R.id.b_iv);
convertView.setTag (holder);
convertView.setTag (holder);
} Else {
vh = (ViewHolder) convertView.getTag ();
}
vh.tv.setText ("image" + (position +1));
vh.iv.setImageResource (pp [position]);
return convertView;
}

/ / Separate class ViewHolder
private class ViewHolder {
private TextView tv;
private ImageView iv;
}
Reply:
reference to the third floor leehu1987 reply:
I understand View convertView refers listview inside each row view, is defined. You need to do is modify the convertview basis, rather than replace him.

if (convertView == null) {
convertView = li.inflate (R.layout.b_item, null);
vh.tv = (TextView) v.findViewById (R.id.b_tv);

vh = new ViewHolder ();
vh.tv = (TextView) v.findViewById (R.id.b_tv);
vh.iv = (ImageView) v.findViewById (R.id.b_iv);
convertView.setTag (holder);
convertView.setTag (holder);
} Else {
vh = (ViewHolder) convertView.getTag ();
}
vh.tv.setText ("image" + (position +1));
vh.iv.setImageResource (pp [position]);
return convertView;
}

/ / Separate class ViewHolder
private class ViewHolder {
private TextView tv;
private ImageView iv;
}

I also understand that it is defined, I wanted to see what principle it is not a simple last used View, and after replacing multiple use can be defined once, I would like to find more. . Thank you for your answer

No comments:

Post a Comment