显示在按钮单击Android的列表视图 [英] Displaying List View on Button Click in Android

查看:171
本文介绍了显示在按钮单击Android的列表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建在其中有两个activites一个Android应用程序。一个活动包含一个按钮。点击按钮,我想切换到显示一个包含几个选项的列表视图其他活动。

屏幕或活动之间切换两个,我用下面的code

 意向意图=新意图(Activity1.this,Activity2.class);
               startActivity(意向);

由于我的活性2类扩展ListActivity',这code似乎没有工作。在我按一下按钮,我想显示包含一些数据的列表视图。

任何帮助将AP preciated

@Siddharth
我似乎在做几乎同样的事情
这里是我的实际code

从活动1

 公共无效的onClick(视图v){            意向意图=新意图(View_Data.this,CategoryList.class);
               startActivity(意向);        }

在活动2

 公共类所属分类扩展ListActivity {公共TextView的选择;
公众的String []类别;
ArrayList的<串GT; type_of_category;
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.category_list);
    getCategories();}    公共无效getCategories(){        DBHelper dbhelper =新DBHelper(本);
        type_of_category =新的ArrayList<串GT;();        type_of_category = dbhelper.getTypesOfQuotes();
        的String []项目=新的String [100];
        的for(int i = 0; I< type_of_type_of_category.size();我++)
        {
            项[I] = type_of_type_of_category.get(ⅰ);
        }        setListAdapter(新ArrayAdapter<串GT;(这一点,android.R.layout.simple_list_item_1,物品));    }

}

下面是我的XML文件

 <的LinearLayout
的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
 机器人:方向=垂直
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
>
<的TextView的android:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:文字=@字符串/你好/>< / LinearLayout中>

在我的第二个活动,我得到的错误在这行

 的setContentView(R.layout.category_list);


解决方案

根据您的第二个活动数据是从数据库或静态数据,这code应该为你工作。我从您的文章,你不需要从第一个活动将数据发送到第二个假设的活动。这是从我的应用程序,它是数据库驱动的实际code。如果你不使用一个数据库,这个code的部分可以改为使用,而不是一个数据库。它应该让你开始:

从1号活动:

 公共无效onClickListContacts(查看目标)
{
    意图listContacts =新意图(这一点,com.dzinesunlimited.quotetogo.ContactsList.class);
    startActivity(listContacts);
}

第二活动:

 公共类ContactsList扩展ListActivity {
DBAdapter dbContactList;@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.contacts_list);    dbContactList =新DBAdapter(本);    //调用方法来显示所有联系人的ListView控件
    fillData();
}私人无效fillData(){
    //从数据库中提取数据
    的String []栏=新的String [] {} dbContactList.TABLE_CON_NAME;
    INT [] =意见INT新[] {} android.R.id.text1;    光标C = dbContactList.getAllContacts();
    startManagingCursor(C);    //设置的ListView
    ListAdapter prjName =新SimpleCursorAdapter(这一点,
            android.R.layout.simple_list_item_1,C,领域,意见);
    setListAdapter(prjName);    dbContactList.close();}

有关的静态列表,也可以参考这个教程:的http:/ /www.vogella.de/articles/Android/article.html#lists

希望这有助于。

PS:这将是巨大的帮助,如果你能后有问题,第二个活动code

添加到您的XML,看看是否有帮助:

 < ListView控件
    机器人:ID =@机器人:ID /列表
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:layout_weight =1>
< /&的ListView GT;

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="vertical"
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>

这篇关于显示在按钮单击Android的列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆