Saturday, July 28, 2012

Creating a ListView populated with array.




First of all, create a Button which starts a new activity(a list view) and write following code on the click event of the button.

  Intent intent=new Intent(this, adapter_activity.class);
    startActivity(intent);

   
in the class adapter_activity write down :


package com.example.listviewadapter;
import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.app.NavUtils;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.view.View;
public class adapter_activity extends ListActivity{
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_adapter);
       
       String[] myStringArray={"Helllo","Wow","Its fun","blah","blah","blah"};
       
       setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,myStringArray));
    }
   
   
}

and in layout.xml 

 <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:drawSelectorOnTop="false"
        android:choiceMode="multipleChoice" >
    </ListView>
   
     <TextView
        android:id="@+id/selection"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#ff0000cc"
        android:textStyle="bold"/>


do not forget to add the activity in the manifest

No comments:

Post a Comment