简体   繁体   中英

How to change image in custom ListView item - Android

We are trying to change the image from a custom ListView item. We have tried everything to the point where the project wouldn't compile anymore. We started over and got to the point where we want to set the image src from a ListView item to something else when the ListView item is tapped.

This is our entire code:

package com.example.namename.listview;

import android.app.Activity;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;


public class MainActivity extends Activity {

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

        String[] foods = {"Bacon","Ham","Tuna","Candy","Meatball","Potato"};
        ListAdapter buckysAdapter = new CustomAdapter(this, foods);
        ListView buckysListView = (ListView) findViewById(R.id.buckysListView);
        buckysListView.setAdapter(buckysAdapter);

        buckysListView.setOnItemClickListener(
                new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        String food = String.valueOf(parent.getItemAtPosition(position));
                        Toast.makeText(MainActivity.this, food, Toast.LENGTH_SHORT).show();
                    }
                }
        );

    }
}


package com.example.namename.listview;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class CustomAdapter extends ArrayAdapter<String> {

    public CustomAdapter (Context context, String[] foods) {
        super(context, R.layout.custom_row, foods);
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater buckysInflater = LayoutInflater.from(getContext());
        View customView = buckysInflater.inflate(R.layout.custom_row, parent, false);

        String singleFoodItem = getItem(position);
        TextView buckysText = (TextView) customView.findViewById(R.id.buckysText);
        ImageView buckysImage = (ImageView) customView.findViewById(R.id.buckysImage);

        buckysText.setText(singleFoodItem);
        buckysImage.setImageResource(R.drawable.deselected);
        return customView;
    }
}


-------------------------------------------------


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/custom_row">

    <ImageView
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:id="@+id/buckysImage"
        android:src="@drawable/deselected"
        android:layout_margin="5dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Large Text"
        android:id="@+id/buckysText"
        android:layout_margin="5dp" />
</LinearLayout>




<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <ListView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/buckysListView"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

want to set the image src from a ListView item to something else when the ListView item is tapped

Use view parameter of onItemClick method to change ImageView of selected row:

 @Override
  public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
   ImageView selectedImage = (ImageView) view.findViewById(R.id.buckysImage);    
   selectedImage.setImageResource(R.drawable.selected);                 
   }

You can write a customView click listener method and then u can change the image as u wanted when ListView item is tapped.

customView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                       buckysImage.setImageResource("Your Image Resource");
            }
        });
    public View getView(int position, View convertView, ViewGroup parent) {

    LayoutInflater buckysInflater = LayoutInflater.from(getContext());
    View customView = buckysInflater.inflate(R.layout.custom_row, parent, false);

    String singleFoodItem = getItem(position);
    TextView buckysText = (TextView) customView.findViewById(R.id.buckysText);
    ImageView buckysImage = (ImageView) customView.findViewById(R.id.buckysImage);

    buckysText.setText(singleFoodItem);
    buckysImage.setImageResource(R.drawable.deselected);

    // set a listener to buckysImage
    buckysImage.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

            buckysImage.setImageResource(R.id.newImage);
        }
    });

    return customView;
}

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