Android Open Source - firstcodeandroid Main Activity






From Project

Back to project page firstcodeandroid.

License

The source code is released under:

MIT License

If you think the Android project firstcodeandroid listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.example.listviewtest;
/*ww  w  .j  av a2 s  .  c o m*/
//import android.R;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends Activity {

  private String[] data = { "Apple", "Banana", "Orange", "Watermelon",
      "Pear", "Grape", "Pineapple", "Strawberry", "Cherry", "Mango" };

  private List<Fruit> fruitList=new ArrayList<Fruit>();
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initFruits();
//    ArrayAdapter<String> adapter = new ArrayAdapter<String>(
//        MainActivity.this, android.R.layout.simple_list_item_1, data);
    FruitAdapter adapter=new FruitAdapter(MainActivity.this, R.layout.fruit_item, fruitList);
    ListView listView = (ListView) this.findViewById(R.id.list_view);
    listView.setAdapter(adapter); 
    
    // make the ListView to be Clickable
    listView.setOnItemClickListener(new OnItemClickListener(){

      @Override
      public void onItemClick(AdapterView<?> parent, View view, int position,
          long id) {
        // TODO Auto-generated method stub
        Fruit fruit = fruitList.get(position); // the position is equal to List index ?
        Toast.makeText(MainActivity.this, fruit.getName(), Toast.LENGTH_SHORT).show();
      }
      
    });
  }

  //TODO for every fruit's pic here, should use their own, in here just use ic_launcher 
  private void initFruits() {
    Fruit Banana = new Fruit("Banana", R.drawable.ic_launcher);
    fruitList.add(Banana);
    Fruit Orange = new Fruit("Orange", R.drawable.ic_launcher);
    fruitList.add(Orange);
    Fruit Watermelon = new Fruit("Watermelon", R.drawable.ic_launcher);
    fruitList.add(Watermelon);
    Fruit Pear = new Fruit("Pear", R.drawable.ic_launcher);
    fruitList.add(Pear);
    Fruit Grape = new Fruit("Grape", R.drawable.ic_launcher);
    fruitList.add(Grape);
    Fruit Pineapple = new Fruit("Pineapple", R.drawable.ic_launcher);
    fruitList.add(Pineapple);
    Fruit Strawberry = new Fruit("Strawberry", R.drawable.ic_launcher);
    fruitList.add(Strawberry);
    Fruit Cherry = new Fruit("Cherry", R.drawable.ic_launcher);
    fruitList.add(Cherry);
    Fruit Mango = new Fruit("Mango", R.drawable.ic_launcher);
    fruitList.add(Mango);
    Fruit apple = new Fruit("Apple", R.drawable.ic_launcher);
    fruitList.add(apple);
  }
}




Java Source Code List

com.example.activitylifecycletest.DialogActivity.java
com.example.activitylifecycletest.MainActivity.java
com.example.activitylifecycletest.NormalActivity.java
com.example.activitytest.FirstActivity.java
com.example.listviewtest.FruitAdapter.java
com.example.listviewtest.Fruit.java
com.example.listviewtest.MainActivity.java
com.jikexueyuan.counttime.MainActivity.java
com.jikexueyuan.getmyphonenumber.GetNumber.java
com.jikexueyuan.getmyphonenumber.MainActivity.java
com.jikexueyuan.getmyphonenumber.MyAdapter.java
com.jikexueyuan.getmyphonenumber.PhoneInfo.java
com.vjia.bookcollector.MainActivity.java
com.vjia.coolweather.MainActivity.java
com.vjia.coolweather.activity.ChooseAreaActivity.java
com.vjia.coolweather.activity.WeatherActivity.java
com.vjia.coolweather.db.CoolWeatherDB.java
com.vjia.coolweather.db.CoolWeatherOpenHelper.java
com.vjia.coolweather.model.City.java
com.vjia.coolweather.model.County.java
com.vjia.coolweather.model.Province.java
com.vjia.coolweather.util.HttpCallbackListener.java
com.vjia.coolweather.util.HttpUtil.java
com.vjia.coolweather.util.Utility.java
com.vjia.helloandroid.FirstActivity.java
com.vjia.helloandroid.HelloAndroidActivity.java
com.vjia.hellonote.AddContent.java
com.vjia.hellonote.MainActivity.java
com.vjia.hellonote.MyAdapter.java
com.vjia.hellonote.NotesDB.java
com.vjia.hellonote.SelectAct.java
com.vjia.jokeking.GetJoke.java
com.vjia.jokeking.HttpCallbackListener.java
com.vjia.jokeking.HttpUtil.java
com.vjia.jokeking.Joke.java
com.vjia.jokeking.MainActivity.java
com.vjia.jokeking.MyAdapter.java
com.vjia.locationtest.MainActivity.java