Skip to content Skip to sidebar Skip to footer

Change Layout Of Selected List Item In Android

I need to create a ListView and show more detailed layout only for the selected row in order to show more information to the costumer. What I tried is below: newsListView.setAdapte

Solution 1:

What you are doing is not even gonna change the View of the ListView Item, you are inflating an unknown view.

solution:

create a method in your adapter for setting the position of the selected item:

publicvoidselectedItem(int position){
   this.position = position; //position must be a global variable
}

in your getView inflate the view when the position is equals to the click item position

@Overridepublic View getView(int position, View convertView, ViewGroup parent) {

    ViewHolderholder=null;

    LayoutInflatermInflater= (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.listitem_news, null);
        holder = newViewHolder();

        holder.title = (TextView) convertView.findViewById(R.id.title);
        holder.image = (ImageView) convertView.findViewById(R.id.imageView1);            

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

    NewsDatanews= items.get(position);

    holder.title.setText(news.getTitle());
    newDownloadImageTask(holder.image).execute(news.getImgUrl());

    if(this.position == position)
    {
        view2 = mInflater.inflate(R.layout.listitem_news_selected, null);
        TextViewinfo= (TextView) view2.findViewById(R.id.info);

        info.setText(news.getInfo());

        return view2;
    }


    return convertView;
}

Using it in your onItemClick

NewsListAdapteradapter=newNewsListAdapter(this, news);
newsListView.setAdapter(adapter );
newsListView.setOnItemClickListener(newOnItemClickListener() {
    @OverridepublicvoidonItemClick(AdapterView<?> parent, View view, int position, long id) {

          adapter.selectedItem(position);
          adapter.notifyDataSetChange();
    }
});

always have a reference/object to your adapter so you can refresh your listView upon clicking on it.

Post a Comment for "Change Layout Of Selected List Item In Android"