简体   繁体   中英

Set image and text of list view item in another imageview and textview

I have an array of images and string like this:

String[] stations = new String[] {
            "GHOTKI 91 Radio FM","UmerKot 91.4 Radio","TMK 100.20 Radio"
};

public static int [] images ={R.drawable.ghotkifmlogo,R.drawable.umerkotfmlogo,R.drawable.tmkfmlogo};

In an custom listview how can I replace these in another Image view and texview?

I need to change that imageview and textview everytime with the clicked item's image and text.

Here's my Custom Adapter:

public class TrackAdapter extends  BaseAdapter{
    String [] description;
    Context context;
    int [] imageId;

    public TrackAdapter(Context c, String[] d, int[] prgmImages) {

        description=d;
        context= c;
        imageId=prgmImages;

    }

    @Override
    public int getCount() {
        return description.length;
    }

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

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

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        if(convertView == null){
            convertView = LayoutInflater.from(context).inflate(R.layout.cardviewlayout,null);
        }
        //get the textview and set its text
        TextView tv=(TextView) convertView.findViewById(R.id.tracktitle);
        //get the img view and set its img icon
        ImageView im=(ImageView) convertView.findViewById(R.id.trackimage);

        tv.setText(description[position]);
        im.setImageResource(imageId[position]);

        return convertView;
    }

Here's Main Activity:

private TextView mSelectedTrackTitle;
    private ImageView mSelectedTrackImage;
    private MediaPlayer mMediaPlayer;
    private ImageView mPlayerControl;
    ListView lv_tracks;
    TrackAdapter track_adapter;

    String[] stations = new String[] {
            "GHOTKI 91 Radio FM","UmerKot 91.4 Radio","TMK 100.20 Radio"
    };
     public static int [] images ={R.drawable.ghotkifmlogo,R.drawable.umerkotfmlogo,R.drawable.tmkfmlogo};



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mMediaPlayer = new MediaPlayer();



        //get a reference to our ListView so we can associate it with our custom ArrayAdapter
        lv_tracks = (ListView) findViewById(R.id.track_list);
        //create a new CustomAdapter
        track_adapter =new TrackAdapter(this,stations,images);
        lv_tracks.setAdapter(track_adapter);//connect the ListView with myCustomAdapter

        //Want to set selected image and title here in these
        mSelectedTrackTitle = (TextView)findViewById(R.id.selected_track_title);
        mSelectedTrackImage =(ImageView)findViewById(R.id.selected_track_image);
lv_tracks.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
??
}

Do not really understand what to do with onitemselect here . in order to change image and text.

you can get current item position by using listview OnItemClickListener it will returns position of an Item clicked then you can use this position as your desired operation

   lv_tracks.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                    public void onItemClick(AdapterView<?> parent, View view,
                                            int position, long id) {
        mSelectedTrackTitle.setText(stations[position]);
    mSelectedTrackImage.setImageDrawable(ContextCompat.getDrawable(this,images [position])); 
   // or 
        mSelectedTrackImage.setImageResource(images [position]);
        }

Try something like this

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    mSelectedTrackTitle.setText( stations[position] );
    mSelectedTrackImage.setImageResource( images[positions] );
}

It will get the station name and image from the clicked position in your arrays, and update your views.

You can achieve it like this:

lv_tracks.setOnItemClickListener(new AdapterView.OnItemClickListener() {
       @Override
       public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
          mSelectedTrackTitle.setText(stations[position]);
          mSelectedTrackImage.setImageResource(images[position]);
       } 

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