简体   繁体   English

在另一个imageview和textview中设置列表视图项目的图像和文本

[英]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? 在自定义列表视图中,如何在另一个图像视图和texview中替换它们?

I need to change that imageview and textview everytime with the clicked item's image and text. 我需要每次使用单击项的图像和文本更改imageview和textview。

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 . 不太了解如何在此处使用onitemselect。 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 您可以使用listview OnItemClickListener来获取当前项目的位置,它将返回被单击项目的位置,然后您可以将该位置用作所需的操作

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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