开发者

Displaying List View on Button Click in Android

I am creating an android application in which there are two activites. One activity contains a button. On the button click, i want to switch to other activity which displays a list view containing few options.

Two switch between the screens or activities , i am using the following code

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

Since my Activity2 class extends the 'ListActivity', this code doesn't seem to work. On my button click, i want to display a list view containing some data.

Any help would be appreciated

@Siddharth i seem to be doing almost the same thing Here is my actual code

From Activity 1

public void onClick(View v) {

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

        }

In Activity 2

public class CategoryList extends ListActivity {

public TextView selection;
public String[] categories;
ArrayList<String> type_of_category;


protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.category_list);
    getCategories();    

}

    public void getCategories() {

        DBHelper dbhelper = new DBHelper(this);
        type_of_category = new ArrayList<String>();

        type_of_category = dbhelper.getTypesOfQuotes();
        String[] items = new String[100];
        for(int i=0;i<type_of_type_of_category.size();i++)
        {
            items[i] = type_of_type_of_category.get(i);
        }

        setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,items));

    }

}

Here is my XML File

   <LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vert开发者_Go百科ical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:text="@string/hello" />

</LinearLayout>

In My 2nd Activity, i get the error in this line

 setContentView(R.layout.category_list);


Depending on whether your data in the second activity is from a database or static data, this code should work for you. I am assuming from your post that you dont need to send data from the 1st activity to the 2nd activity. This is actual code from my application which is database driven. If you are not using a database, parts of this code can be changed to use that instead of a database. It should get you started:

From the 1st Activity:

public void onClickListContacts(View target)
{
    Intent listContacts = new Intent(this, com.dzinesunlimited.quotetogo.ContactsList.class);
    startActivity(listContacts);
}

The 2nd activity:

public class ContactsList extends ListActivity {
DBAdapter dbContactList;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.contacts_list);

    dbContactList = new DBAdapter(this);

    // Calls the method to display the ListView of all Contacts
    fillData();
}    

private void fillData() {
    // Pull the data from the database
    String[] fields = new String[]    {    dbContactList.TABLE_CON_NAME    };
    int[] views = new int[]    {    android.R.id.text1    };

    Cursor c = dbContactList.getAllContacts();
    startManagingCursor(c);

    // Set the ListView
    ListAdapter prjName = new SimpleCursorAdapter(this,
            android.R.layout.simple_list_item_1, c, fields, views);
    setListAdapter(prjName);

    dbContactList.close();

}

For a static list, you can also refer to this tutorial: http://www.vogella.de/articles/Android/article.html#lists

Hope this helps.

PS: It would be great help if you could post code of the 2nd activity which has problems.

Add this to your XML and see if that helps:

<ListView 
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1"    >
</ListView>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜