开发者

working with LazyList problem

开发者 https://www.devze.com 2023-04-02 06:30 出处:网络
i have implemented sample application by using lazy list concept.i would like to assign the all string array values to text view from LazyAdapter(extends BaseAdapter)class

i have implemented sample application by using lazy list concept.i would like to assign the all string array values to text view from LazyAdapter(extends BaseAdapter)class

i have used this class as follows

 public class LazyAdapter extends BaseAdapter {

private Activity activity;
private String[] data;
private static LayoutInflater inflater=null;
public ImageLoader imageLoader;



public LazyAdapter(Activity a, String[] d) {
    activity = a;
    data=d;
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    imageLoader=new ImageLoader(activity.getApplicationContext());
}

public int getCount() {
    return data.length;
}

public Object getItem(int position) {
    return position;
}

public long getItemId(int position) {
    return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
    View vi=convertView;


    if(convertView==null)
        vi = inflater.inflate(R.layout.item, null);



         String texts[]={"hai","how are you","hello","oyeeee"};

        //i would like to set this texts[] values to TextView

        for(int i=0;i< texts.size();i++)
        { 
         TextView text=(TextView)vi.findViewById(R.id.text);;
         ImageView image=(ImageView)vi.findViewById(R.id.image);
         text.setText("item "+texts[i]);
         imageLoader.DisplayImage(data[position], activity, image);


           }
    return vi;

       }
   }

here i am able display only oyeee.how can i view all values as a list

here how can i display all string array values in a list like in lazy list we are displaying item开发者_StackOverflow中文版0,item1,item2,....etc.,


The problem is because you have used a for-loop for each item that is being populated. And in this case since for each view your for loop is executed, obviously it is the last element in the array, which of no surprise "oyeeee" gets printed in all the lines.

Now what you have to do is, you can provide the array globally somewhere in your activity, and change the code within getView() like this,

//Global declaration of array

 String texts[]={"hai","how are you","hello","oyeeee"};
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;


if(convertView==null)
    vi = inflater.inflate(R.layout.item, null);


     TextView text=(TextView)vi.findViewById(R.id.text);
     ImageView image=(ImageView)vi.findViewById(R.id.image);
     text.setText("item "+texts[postion]);
     imageLoader.DisplayImage(data[position], activity, image);


       }
return vi;

   }

}

This is all it takes.


Remove GetView Code & Write Following Code in GetView Function.

String texts[]={"hai","how are you","hello","oyeeee"};
View vi = convertView;
        if (convertView == null) {
            vi = inflater.inflate(R.layout.item, null);
            image = (ImageView) vi.findViewById(R.id.imgview);
            txtviewtitle=(TextView)vi.findViewById(R.id.txtviewtitle);
        }
        txtviewtitle.setText(texts[position]);
        imageLoader.DisplayImage(data[position], activity, image);

        return vi;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号