开发者

How to modify an element of a list view according to one condition

开发者 https://www.devze.com 2023-04-12 09:40 出处:网络
i have a listview that use a simple adapter. Basically each element consist of an image, 1 string field and a ratingbar.

i have a listview that use a simple adapter. Basically each element consist of an image, 1 string field and a ratingbar.

ListView element's layout is in the file element.xml instead the list view is inside main.xml

The value of the string field can be a number, the word FREE, or the word INSTALLED.

Just close to this field in the element.xml layout there is a label with ID = Price and text value = "Application price"

This is what i get now when i display my list: "Application price" 4 "Application price" FREE "Application price" 7 "Applicati开发者_开发知识库on price" INSTALLED "Application price" FREE

Basically what i would like to do is to check the value of the string field and if it is equal to INSTALLEd the label Price has to be become Invisible to get this result:

"Application price" 4 "Application price" FREE "Application price" 7 INSTALLED "Application price" FREE

Do you know how to do that......i red that could be right to extend the method getview of my simple adapter, but i don't know how.....it's not clear to me the how this method works and what his parameters stand for.....can you help me or suggest me the right thing to do?

public View getView(int position, View convertView, ViewGroup parent) 
    {
        View view = super.getView(position, convertView, parent);


        TextView costo = (TextView) view.findViewById(R.id.costo);
        TextView prezzo = (TextView) view.findViewById(R.id.appPrezzo);
        if (prezzo.getText().equals("Installed"))
        {
            costo.setVisibility(View.INVISIBLE);
            //view = inflater.inflate(R.layout.elem_applist, null);

        }
        else
        {
            costo.setVisibility(View.VISIBLE);
        }

      return view;

I did it in this way......i don't know why i can'y comment or vote for your answer Blessemn.....anyway it works, but i didn't use an inflater....Hope is correct also whithout it :)


In the getview method is called to return the view representing each row. It is called multiple time even when you scroll the list. The arguments you get

position -> The row number. convertview -> the view representing the row. At beginning its null. But once is rendered it wont be null. parent -> the parent view

In the getview method, you MUST return a view to represent the row.

Heres how a normal getView method looks like

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    convertView = mInflater.inflate(R.layout.analytics_row, null);
    TextView mNae = (TextView)convertView.findViewById(R.id.analytic_name);

    if(data.get(position).mystring == ""){
        mNae.setText("YES");
    } else {
        mNae.setText("No");
    }       
    return convertView;
}

Here we just get a view using the inflater and find the textview inside the view. We check for a condition and set the text accordingly and return the view.

There are way better tutorials out there. You should search for custom base adapter.

If it works for you then there is no problem in using it. As long as you return a valid view the list will work. Inflater is just used to create a view from an xml file.

But do notice paresh's answer. As you can see he uses a static class to hold references to a rows internal childs. When the list is first renderedm the getview method is only called the n times where n is the number of visible rows at the moment(not exact). Suppose the 5 rows are visible at a time, the adapter reuses an existing view and just updates the values in them.

So in his code he check it the convertview is null, if it is he inflates a new row otherwise he uses an existing row with updated values. I recomend you watch this video from Google IO


Try the below code to define custom listview. For example:

public static class ViewHolder
    {
        ImageView imgViewLogo;
        TextView txtViewTitle;
        TextView txtViewDescription;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        ViewHolder holder;
        if(convertView==null)
        {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.items, null);

            holder.imgViewLogo = (ImageView) convertView.findViewById(R.id.imgViewLogo);
            holder.txtViewTitle = (TextView) convertView.findViewById(R.id.txtViewTitle);
            holder.txtViewDescription = (TextView) convertView.findViewById(R.id.txtViewDescription);

            convertView.setTag(holder);
        }
        else
            holder=(ViewHolder)convertView.getTag();

        ItemBean bean = (ItemBean) itemList.get(position);

        holder.imgViewLogo.setImageResource(bean.getImage());
        holder.txtViewTitle.setText(bean.getTitle());
        holder.txtViewDescription.setText(bean.getDescription());

        return convertView;
    }
0

精彩评论

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

关注公众号