Basic Android Lists

  1. Single item - ArrayAdapter

    txtNote = (EditText)findViewById(R.id.txtNote);
    btnAdd = (ImageButton)findViewById(R.id.btnAdd);
    lstNotes = (ListView)findViewById(R.id.lstNotes);
    adapter = new ArrayAdapter(AddNewListOfNotes.this,R.layout.listnotes);
    lstNotes.setAdapter(adapter);
    btnAdd.setOnClickListener(new View.OnClickListener() {
    	public void onClick(View v) {
    		adapter.add(txtNote.getText().toString());
    	}
    });

    http://stackoverflow.com/questions/4645854/add-data-from-edittext-to-listview-in-android

  2. on click listeners
    lstNotes.setOnItemClickListener(new OnItemClickListener(){
    	public void onItemClick(AdapterView parent, View view, int position, long id) {
    		Toast.makeText(getApplicationContext(), ((TextView) view).getText(), Toast.LENGTH_SHORT).show();
    	}
    });

    http://www.mkyong.com/android/android-listview-example/

  3. single item – array adapter with complex layout – reference id of textview
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:background="#FFFFFF"  >
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >
            <EditText
                android:id="@+id/txtNote"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1.06"
                android:ems="10" >
                <requestFocus />
            </EditText>
            <ImageButton
                android:id="@+id/btnAdd"
                android:layout_width="47dp"
                android:layout_height="fill_parent"
                android:src="@drawable/ic_menu_add" />
        </LinearLayout>
        <ListView
            android:id="@+id/lstNotes"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >
        </ListView>
    </LinearLayout>
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="horizontal" >
        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ok" />
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical" >
            <TextView
                android:id="@+id/txtHeading"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Large Text"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:textColor="#090909" />
            <TextView
                android:id="@+id/txtSubHeading"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Small Text"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:textColor="#090909" />
        </LinearLayout>
    </LinearLayout>
    package com.example.helloandroid;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.ArrayAdapter;
    import android.widget.EditText;
    import android.widget.ImageButton;
    import android.widget.TextView;
    import android.widget.Toast;
    import android.widget.ListView;
    import android.widget.AdapterView.OnItemClickListener;
    
    public class AddNewListOfNotes extends Activity {
    	private EditText txtNote;
    	private ImageButton btnAdd;
    	private ListView lstNotes;
    	private ArrayAdapter adapter;
    	@Override
    	public void onCreate(Bundle savedInstanceState) {
    	    super.onCreate(savedInstanceState);
    
            setContentView(R.layout.addnewlistofnotes);
    		txtNote = (EditText)findViewById(R.id.txtNote);
    		btnAdd = (ImageButton)findViewById(R.id.btnAdd);
    		lstNotes = (ListView)findViewById(R.id.lstNotes);
    		adapter = new ArrayAdapter(AddNewListOfNotes.this,R.layout.listnotes,R.id.txtHeading);
    		lstNotes.setAdapter(adapter);
    
    		btnAdd.setOnClickListener(new View.OnClickListener() {
    
    			public void onClick(View v) {
    				adapter.add(txtNote.getText().toString());
    				txtNote.setText("");
    			}
    		});
    		lstNotes.setOnItemClickListener(new OnItemClickListener(){
    
    			public void onItemClick(AdapterView parent, View view, int position,
    					long id) {
    				Toast.makeText(getApplicationContext(),
    						((TextView)view.findViewById(R.id.txtHeading)).getText(), Toast.LENGTH_SHORT).show();
    
    			}
    		});
    	}
    }


    android-listview-example

  4. binding more items

    - thanks to vogella.com_tutorial_twolistitems and adapter.notifyDataSetChanged(); from many crazy links which made me pretty scared if I had to code that much!!! but thank god custom adapter was not needed, a simple adapter was enough!

    public class AddNewListOfNotes extends Activity {
    	private EditText txtNote;
    	private ImageButton btnAdd;
    	private ListView lstNotes;
    	private SimpleAdapter adapter;
    	private int count = 1;
    	private ArrayList<Map<String, String>> list = new ArrayList<Map<String, String>>();;
    	@Override
    	public void onCreate(Bundle savedInstanceState) {
    	    super.onCreate(savedInstanceState);
    
            setContentView(R.layout.addnewlistofnotes);
    		txtNote = (EditText)findViewById(R.id.txtNote);
    		btnAdd = (ImageButton)findViewById(R.id.btnAdd);
    		lstNotes = (ListView)findViewById(R.id.lstNotes);
    		String[] from = { "heading", "subheading" };
    		int[] to = { R.id.txtHeading, R.id.txtSubHeading };
    		adapter = new SimpleAdapter (this, list, R.layout.listnotes, from, to);
    		lstNotes.setAdapter(adapter);
    		
    		btnAdd.setOnClickListener(new View.OnClickListener() {			
    			public void onClick(View v) {
    				HashMap<String, String> item = new HashMap<String, String>();
    				item.put("heading", txtNote.getText().toString());
    				item.put("subheading", Integer.toString(count));
    				list.add(item);
    				adapter.notifyDataSetChanged();
    				count++;
    				txtNote.setText("");
    			}
    		});
    		lstNotes.setOnItemClickListener(new OnItemClickListener(){		
    			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    				Toast.makeText(getApplicationContext(),
    						((TextView)view.findViewById(R.id.txtHeading)).getText(), Toast.LENGTH_SHORT).show();
    				
    			}			
    		});        
    	}
    }


    another

  5. other

    ImageView imageView=(ImageView) view.findViewById(R.id.imageView1);
    if((String) imageView.getTag() != "x"){
        imageView.setImageResource(R.drawable.x);
    	imageView.setTag("x");
    }
    else{
        imageView.setImageResource(R.drawable.ok);
    	imageView.setTag("");
    }

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>