2

i create a listView which i can put images and text in the listView and to add rows to the listView dynamically.

however i dont understand how can i implement that i will can change specific row item layout in the listView?

for example, in the Start of activity i will initialize the listView with 5 rows, and i when a trigger happens (after some minutes) i want to change for example the forth listView item (only change the layout of the forth entry, all other entries remain with the original layout)

thanks alot

private  ListView lv;
private List<String> text = new ArrayList<String>();
private List<Bitmap> listImages;
private String freindsId;

ArrayList<String> text_sort = new ArrayList<String>();
ArrayList<Bitmap> image_sort = new ArrayList<Bitmap>();


private MyCustomAdapter adapter;
  adapter =new MyCustomAdapter(text,listImages);
  lv.setAdapter(adapter);

class MyCustomAdapter extends BaseAdapter{
          public   List<String> text_array = new ArrayList<String>();
          public   List<Bitmap> image_array = new ArrayList<Bitmap>();
          public int getCount(){
               return text_array.size();
          }

          MyCustomAdapter(List<String> text, List<Bitmap> image)
          {
           text_array = text;
           image_array = image;
          }
          public long getItemId(int position){
               return position;
          }
          public String getItem(int position){
               return null;
          }
          public View getView(final int position, View convertView, ViewGroup parent) {
            LayoutInflater inflate = getLayoutInflater();
            View v = inflate.inflate(R.layout.listview, parent, false);
            final ImageView image = (ImageView) v.findViewById(R.id.ImageView01);
            TextView txtUnderImage =(TextView) v.findViewById(R.id.TextView01);

             if(listImages.get(position) != null) {
                         Bitmap bitmap = image_array.get(position);
                        image.setImageBitmap(bitmap);
                        ByteArrayOutputStream stream = new ByteArrayOutputStream();
                        bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
                        final byte[] byteArray = stream.toByteArray();

                         image.setOnClickListener(new View.OnClickListener() {

                            public void onClick(View v) {
                                // Switching to Register screen
                             Intent i = new Intent(getApplicationContext(), itemSaleActivity.class);
                             i.putExtra("picture", byteArray);
                            startActivity(i);


                                }
                        });
                         txtUnderImage.setText(text_array.get(position));
                         image.setVisibility(View.VISIBLE);
              } else {
                         image.setVisibility(View.GONE);
                         image.setImageBitmap(null);
                          image.setVisibility(View.VISIBLE);
          }
         return v;

        }
         public void addObject(String text, Bitmap bitmap) {
                text_array.add(text);
                image_array.add(bitmap);
            notifyDataSetChanged();
         } 

         public void addFirst(String text, Bitmap bitmap) {

                image_array.add(0,bitmap);
                text_array.add(0,text);

                notifyDataSetChanged();
             } 
         public void removeObject(String text,Bitmap bitmap) {

                int indexToRemove = image_array.indexOf(bitmap);
                image_array.remove(indexToRemove);
                text_array.remove(indexToRemove);
                notifyDataSetChanged();
         }
         public void deleteAll() {
             image_array.clear();
             text_array.clear();
             notifyDataSetChanged();
         }
    }
Adir Rahamim
  • 453
  • 1
  • 12
  • 31

4 Answers4

0

Try getting your View of particular position like this

getChildAt(position) you'll get your view,

And Change the layout of your row and call notifyDataSetChanged().

If you are trying to heighlight the selected row, use this Inflate ListView row from OnClickListener in Android?

When an Event Trigger Change Data of Your Adapter and call notifyDataSetChanged() on the Adapater object.

Community
  • 1
  • 1
Pragnani
  • 20,075
  • 6
  • 49
  • 74
  • hi, thanks for answer, however i didnt understand you. how can i change the layout of specific row in the listView? – Adir Rahamim Mar 19 '13 at 17:44
  • thanks alot for answer, do i need to call to notifyDataSetChanged() - i think that notifyDataSetChanged method its only for the data stored in the adapter...is there any method to call when the listView change? – Adir Rahamim Mar 19 '13 at 17:49
  • In order to refresh your list with the hightlighted row you need to call notifyDataSetChanged() otherwise it won't effect, untill the list refreshes – Pragnani Mar 19 '13 at 17:53
  • thanks, when i get the View with getChildAt method, how can i change the layout of the row? – Adir Rahamim Mar 19 '13 at 17:58
  • @AdirRahamim you'll get a view using getchildAt(position), and you can perform any operations on the view, like chaning the background, size, etc.. – Pragnani Mar 19 '13 at 18:01
  • hi, in the "View" class i found method to change the background or text, nothing about change the layout..what method should i use in the View class inorder to change to another layout? – Adir Rahamim Mar 19 '13 at 18:06
  • @AdirRahamim Changing layout in the sense..? – Pragnani Mar 19 '13 at 18:37
  • @ Pragnani, yes, i want to change to another layout, i mean to set to another layout in R.layout.another_layout...please help me :) – Adir Rahamim Mar 19 '13 at 18:40
  • @AdirRahamim what are you trying to achieve..? – Pragnani Mar 19 '13 at 18:42
  • @ Pragnani, i am trying to implement that when user swipe on listView item, it will change to layouy with options to delete or update the listView and a picture in the layout – Adir Rahamim Mar 19 '13 at 18:46
  • @AdirRahamim Keep them in a original layout, and set their visibility Gone, When an event triggers set them Visible...This is the way to implement that – Pragnani Mar 19 '13 at 18:48
  • @ Pragnani, how can i arrange them in the listView, they will be on another imageViews... is it possible? – Adir Rahamim Mar 19 '13 at 18:51
  • @AdirRahamim Sorry brother, "they will be on another imageViews", I didn't get this. is your imageview's are in different layout you mean to ask.? – Pragnani Mar 19 '13 at 18:54
  • @ Pragnani, i mean that as you said i need to implement two different layouts on one layout and to set to visible only the Objects that i want...so how can i arrange two different layout on one layout? i think that i got confused...sorry – Adir Rahamim Mar 19 '13 at 19:00
  • @AdirRahamim You can group your layout in a LinearLayout layout or any layout group of your choice, And then set the visibility of of layout group instead of all elements in the layout – Pragnani Mar 19 '13 at 19:05
0

Use the getChildAt() method to access whichever row you want to change. For example listView.getChildAt(4)

flup
  • 26,937
  • 7
  • 52
  • 74
  • thanks, and then how should i change the spefic row layout? do i need to call to notifyDataSetChanged() - i think that notifyDataSetChanged method its only for the data stored in the listView...is there any method to call when the listView change? – Adir Rahamim Mar 19 '13 at 17:45
  • getChildAt will return whatever is at that position in your listview. If its a textview or something similar, you can make the changes directly. If its another layout, you will have to get the specific child you want of that layout then. You don't need to call any extra methods on change – Alan Cross Mar 19 '13 at 17:49
  • hi, i want to change to another layout in the spesific row item...how should i implement it with getChildAt() method? thanks alot – Adir Rahamim Mar 19 '13 at 17:53
  • please help me to understand how can i change listView row item to another layout? i still dont understand how can i implement it? thanks alot – Adir Rahamim Mar 19 '13 at 18:18
0

Make changes to the fourth row item. Call notifyDataSetChanged() on your adapter.

notifyDataSetChanged() Notifies the attached observers that the underlying data has been changed and any View reflecting the data set should refresh itself.

notifyDataSetChanged() refreshes your listview.

Raghunandan
  • 132,755
  • 26
  • 225
  • 256
0

Ways described here are going to look through whole list, while you only need to update views that are visible, rest changes will be taken care while they will be 'redrawn'.

So look at the answers provided at THIS LINK, it provides a solution where you only need to identify your view's position and you can update the view only if it is visible at that time.

to Quote that answer -

int iFirst = getFirstVisiblePosition();
int iLast = getLastVisiblePosition();

if ( indexToChange >= numberOfRowsInSection() ) {
    Log.i( "MyApp", "Invalid index. Row Count = " + numberOfRowsInSection() );
}
else {      
    if ( ( index >= iFirst ) && ( index <= iLast ) ) {
        // get the view at the position being updated - need to adjust index based on first in the list
        View vw = getChildAt( sysvar_index - iFirst );
        if ( null != vw ) {
            // get the text view for the view
            TextView tv = (TextView) vw.findViewById(com.android.myapp.R.id.scrollingListRowTextView );
            if ( tv != null ) {
                // update the text, invalidation seems to be automatic
                tv.setText( "Item = " + myAppGetItem( index ) + ". Index = " + index + ". First = " + iFirst + ". Last = " + iLast );
            }
        }
    }
} 
Community
  • 1
  • 1
Darpan
  • 5,623
  • 3
  • 48
  • 80