在通知中显示listView中的图像

时间:2016-01-01 11:18:42

标签: java android listview android-listview

我正在处理此listView示例http://www.cs.sfu.ca/CourseCentral/276/bfraser/androidExampleCode/AndroidComplexListViewDemo.zip 并且我想在点击时在列表视图中显示图像

我还要显示项目名称。

public class MainActivity extends Activity {
private List<Car> myCars = new ArrayList<Car>();

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

    populateCarList();
    populateListView();
    registerClickCallback();
}
private void populateCarList() {
    myCars.add(new Car("Ford", 1940, R.drawable.help, "Needing work"));
    myCars.add(new Car("Toyota", 1994, R.drawable.heart, "Lovable"));
    myCars.add(new Car("Honda", 1999, R.drawable.fish, "Wet"));
    myCars.add(new Car("Porche", 2005, R.drawable.lightning, "Fast!"));
    myCars.add(new Car("Jeep", 200, R.drawable.star, "Awesome"));
    myCars.add(new Car("Rust-Bucket", 2010, R.drawable.bug, "Not *very* good"));
    myCars.add(new Car("Moon Lander", 1971, R.drawable.up, "Out of this world"));
}

private void populateListView() {
    ArrayAdapter<Car> adapter = new MyListAdapter();
    ListView list = (ListView) findViewById(R.id.carsListView);
    list.setAdapter(adapter);
}

private void registerClickCallback() {
    ListView list = (ListView) findViewById(R.id.carsListView);
    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View viewClicked,
                int position, long id) {

            Car clickedCar = myCars.get(position);
            String message = "You clicked position " + position
                            + " Which is car make " + clickedCar.getMake();
            Toast.makeText(MainActivity.this, message, Toast.LENGTH_LONG).show();
        }
    });
}

private class MyListAdapter extends ArrayAdapter<Car> {
    public MyListAdapter() {
        super(MainActivity.this, R.layout.item_view, myCars);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // Make sure we have a view to work with (may have been given null)
        View itemView = convertView;
        if (itemView == null) {
            itemView = getLayoutInflater().inflate(R.layout.item_view, parent, false);
        }

        // Find the car to work with.
        Car currentCar = myCars.get(position);

        // Fill the view
        ImageView imageView = (ImageView)itemView.findViewById(R.id.item_icon);
        imageView.setImageResource(currentCar.getIconID());

        // Make:
        TextView makeText = (TextView) itemView.findViewById(R.id.item_txtMake);
        makeText.setText(currentCar.getMake());

        // Year:
        TextView yearText = (TextView) itemView.findViewById(R.id.item_txtYear);
        yearText.setText("" + currentCar.getYear());

        // Condition:
        TextView condionText = (TextView) itemView.findViewById(R.id.item_txtCondition);
        condionText.setText(currentCar.getCondition());

        return itemView;
    }               
}

public class Car {
private String make;
private int year;
private int iconID;
private String condition;

public Car(String make, int year, int iconID, String condition) {
    super();
    this.make = make;
    this.year = year;
    this.iconID = iconID;
    this.condition = condition;
}

public String getMake() {
    return make;
}
public int getYear() {
    return year;
}
public int getIconID() {
    return iconID;
}
public String getCondition() {
    return condition;
}

我想要什么

What I want

0 个答案:

没有答案