[:en]Android: ListView[:]

[:en]ListView

1. activity_main.xml

 

    


  



2. row_layout.xml





    
    


    
    

3. MainActivity.java

package com.example.cambridge.listview;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;

public class MainActivity extends AppCompatActivity {
    // Start listview
    ListView listView;
    int[] movie_poster_resource = {R.drawable.p1,
            R.drawable.p2,
            R.drawable.p3,
            R.drawable.p4,
            R.drawable.p5,
            R.drawable.p6,
            R.drawable.p7,
            R.drawable.p8,
    };
    String[] movie_titles;
    String[] movie_ratings;
    MovieAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // Listview
        listView = (ListView)findViewById(R.id.list_view);
        movie_ratings = getResources().getStringArray(R.array.movie_ratings);
        movie_titles  = getResources().getStringArray(R.array.movie_titles);
        int i=0;
        adapter = new MovieAdapter(getApplicationContext(),R.layout.row_layout);
        listView.setAdapter(adapter);

        for(String titles: movie_titles){
            MovieDataProvider dataProvider = new MovieDataProvider(movie_poster_resource[i],
                    titles,movie_ratings[i]);
            adapter.add(dataProvider);
            i++;
        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

4. MovieAdapter.java

package com.example.cambridge.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;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by cambridge on 9/2/16.
 */
public class MovieAdapter extends ArrayAdapter {
    List list = new ArrayList();

    public MovieAdapter(Context context, int resource) {
        super(context, resource);
    }

    static class DataHandler {
        ImageView Poster;
        TextView title;
        TextView rating;
    }


    @Override
    public void add(Object object) {
        super.add(object);
        list.add(object);

    }

    @Override
    public int getCount() {
        return this.list.size();
    }

    @Override
    public Object getItem(int position) {
        return this.list.get(position);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
       View row;
        row = convertView;
        DataHandler handler;
        if(convertView == null) {
          LayoutInflater inflater = (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
          row = inflater.inflate(R.layout.row_layout,parent,false);
          handler = new DataHandler();
          handler.Poster = (ImageView)row.findViewById(R.id.movie_poster);
          handler.title = (TextView)row.findViewById(R.id.movie_title);
            handler.rating = (TextView)row.findViewById(R.id.movie_rating);
          row.setTag(handler);
        }else{
            handler = (DataHandler)row.getTag();
        }
        MovieDataProvider dataProvider;
        dataProvider = (MovieDataProvider) this.getItem(position);
        handler.Poster.setImageResource(dataProvider.getMovie_poster_resource());
        handler.title.setText(dataProvider.getMovie_title());
        handler.rating.setText(dataProvider.getMovie_rating());
        return row;
    }
}

5. MovieDataProvider.java

package com.example.cambridge.listview;

/**
 * Created by cambridge on 9/2/16.
 */
public class MovieDataProvider {
    private  int movie_poster_resource;
    private  String movie_title;
    private String movie_rating;

    public String getMovie_rating() {
        return movie_rating;
    }

    public MovieDataProvider(int movie_poster_resource,String movie_title,String movie_rating){
        this.setMovie_poster_resource(movie_poster_resource);
        this.setMovie_title(movie_title);
        this.setMovie_rating(movie_rating);
    }

    public void setMovie_rating(String movie_rating) {
        this.movie_rating = movie_rating;
    }


    public int getMovie_poster_resource() {
        return movie_poster_resource;
    }

    public void setMovie_poster_resource(int movie_poster_resource) {
        this.movie_poster_resource = movie_poster_resource;
    }

    public String getMovie_title() {
        return movie_title;
    }

    public void setMovie_title(String movie_title) {
        this.movie_title = movie_title;
    }


}

6. strings.xml

Top 8 movies

    Hello world!
    Settings
    
    One
    Two
    Three
    Four
    Five
    Six
    Seven
    Eight
    
    
        R1
        R2
        R3
        R4
        R5
        R6
        R7
        R8
    



+++++++++++++++++++++++++++++++++++++++++++++++++++

Or Simple ListView with onItemClickListener:

SimpleLV

1. activity_main.xml


    




2. MainActivity.java

(this,android.R.layout.simple_list_item_1,myAdapter);
        list_view.setAdapter(adapter);

        // Step 2. Add onItemClickListener
        list_view.setOnItemClickListener(this);

    }

    // Step 3. Add this onItemClick method. That's it.
    @Override
    public void onItemClick(AdapterView arg0, View view, int position, long arg3){  
        //Toast.makeText(getApplicationContext(),"Position:"+position,Toast.LENGTH_LONG).show();

      // Get item name of the list
       String item_of_list = items[position];
       Toast.makeText(this, item_of_list, Toast.LENGTH_LONG).show();

        
        if (item_of_list.equals("a1")){
            startActivity(new Intent(this, Register.class));
        }
        if (item_of_list.equals("a2")){
            startActivity(new Intent(this, Register.class));
        }
    }

}

[:]

Print Friendly, PDF & Email
Scroll to Top