简体   繁体   中英

Change specific row item layout in listView

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();
         }
    }

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.

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

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.

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 );
            }
        }
    }
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM