ListView

Mai jos aveti codul explicat in cadrul cursului.

  • clasa MainActivity, unde initializam lista, setam adaptorul si ne abonam la evenimentul de itemClick
public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
      
        ListView list = (ListView)findViewById(R.id.listView1);
        
        ArrayList<String> nume=new ArrayList<String>();
        nume.add("Iulia");
        nume.add("Mystery woman");
        nume.add("Alexandra");
        
        myAdapter adapter=new myAdapter(nume, this);
        
        list.setAdapter(adapter);
        
        
        list.setOnItemClickListener(new OnItemClickListener() {
           @Override
			public void onItemClick(AdapterView<?> adaptor, View arg1, int position,
					long id) {
				Toast.makeText(MainActivity.this, "Ai apasat-o pe "+ adaptor.getItemAtPosition(position), Toast.LENGTH_LONG).show()	;
			}
		});
        
    }}
  • clasa myAdapter, unde definim comportamentul adaptorului, prin suprascrierea celor 4 functii din interfata BaseAdapter.
class myAdapter extends BaseAdapter
{ArrayList<String> nume;
  Activity context;
  
  public myAdapter(ArrayList<String> _nume, Activity _context)
  {nume=_nume;
  context=_context;}
  
	@Override
	public int getCount() {
		
		return nume.size();
	}

	@Override
	public Object getItem(int arg0) {
		
		return nume.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
	
		View element;
		if (convertView==null){
		LayoutInflater inflater= context.getLayoutInflater();
		element= (View)inflater.inflate(R.layout.list_element, null);
		
		myTag tag=new myTag();
		tag.textView=(TextView)element.findViewById(R.id.textView1);
		tag.imageView=(ImageView) element.findViewById(R.id.imageView1);
		element.setTag(tag);
		
		}
		else element=convertView;
			myTag tag=(myTag)element.getTag();	
		
		tag.textView.setText(nume.get(position));
		
		if(nume.get(position).equalsIgnoreCase("alexandra")) tag.imageView.setImageResource(R.drawable.alexandra);
		else if (nume.get(position).equalsIgnoreCase("iulia")) tag.imageView.setImageResource(R.drawable.iulia);
		else tag.imageView.setImageResource(R.drawable.ic_launcher);
		
		return element;
	}}


class myTag
{TextView textView;
ImageView imageView;}
  • xml-ul in care este definit un element din lista
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="10dp"
    android:layout_height="10dp"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/imageView2"
        android:layout_marginLeft="30dp"
        android:layout_gravity="center"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:src="@android:drawable/ic_media_play" />

    <ImageView
         android:layout_marginLeft="30dp"
         android:layout_gravity="center"
         android:scaleType="fitCenter"
        android:id="@+id/imageView1"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:src="@drawable/ic_launcher" />

    <TextView
         android:layout_marginLeft="30dp"
        android:id="@+id/textView1"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

Exercitii

  1. Realizati o lista de masini, pentru care fiecare element din lista sa contina logoul brand-ului, numele brand-ului si numele masinii.
  • Daca brand-ul este Reno sau BMW afisati imaginea corespunzatoare brand-ului, iar pentru orice alt brand afisati o imagine default

  • Realizati in partea de jos a ecranului o interfata simpla pentru introducerea unui item nou.
  • In cazul unui long press pe un item, eliminati item-ul din lista
  • Realizati varianta cea mai eficienta de implementare a acestei liste
  • Pastrati acest cod, veti reveni la el mai tarziu.

Bonus

Gasiti o metoda de a restaura lista atunci cand aplicatia este inchisa si redeschisa.

programare_android/curs/curs3.txt · Ultima modificare: 2013/08/01 17:30 de către stefan.alexandru
CC Attribution-Noncommercial-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0