0

我可以在线性布局中定义文本视图和列表视图吗?

如果是,那么我正在尝试定义一个列表视图并希望每个列表项都打开一个新活动。但我的活动没有运行,它显示“不幸的是,您的应用程序被迫关闭”。请帮忙!

这是代码:

public class Second_listview extends ListActivity
{

static final String[] type = new String[]{

    "Array", "Strings" 

 };

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    setContentView(R.layout.second_listview);

    // setting up list view

    setListAdapter (new ArrayAdapter<String>(this, R.layout.second_listview, type));
    ListView list = getListView();
    list.setTextFilterEnabled(true);
    list.setOnItemClickListener(new OnItemClickListener(){

        @Override
        public void onItemClick(AdapterView<?> arg0, View view, int arg2,
                long arg3) {
            // TODO Auto-generated method stub

            //liking each list item to start a new activity

            switch(arg2)

            {
              case 1 : Intent myIntent1 = new Intent(view.getContext(), Array_list.class);
                       startActivityForResult(myIntent1, 0);
                       break;
              case 2 : Intent myIntent2 = new Intent(view.getContext(), String_list.class);
                       startActivityForResult(myIntent2, 0);
                       break;

            }

        }



    });
}



   }
4

3 回答 3

0

只需执行以下操作...

FirstActivity(MainActivity.java)

package com.example.getlistitemvalueinnewactivity;

import android.os.Bundle;

import android.app.Activity;

import android.content.Intent;

import android.view.Menu;

import android.view.View;

import android.widget.AdapterView;

import android.widget.AdapterView.OnItemClickListener;

import android.widget.ArrayAdapter;

import android.widget.ListView;

import android.widget.TextView;

import android.widget.Toast;

public class MainActivity extends Activity implements OnItemClickListener {

String[] str={"Hello","Android","Apple","Rakesh","BlackBerry"};

@Override

public void onCreate(Bundle savedInstanceState)

{

super.onCreate(savedInstanceState);

 setContentView(R.layout.activity_main);

  ListView lv=(ListView)findViewById(R.id.listView1);

  ArrayAdapter arr =new     ArrayAdapter(this,android.R.layout.simple_expandable_list_item_1,str);                                                                              

lv.setAdapter(arr);

lv.setOnItemClickListener(this);

}

public void onItemClick(AdapterView arg0, View arg1, int arg2, long arg3) {

// TODO Auto-generated method stub

String s= (String) ((TextView) arg1).getText();

//Toast.makeText(this, s, 100).show();

Intent intent = new Intent();

intent.setClass(this, Next.class);

Bundle bundle = new Bundle();

bundle.putString("Value", s);

intent.putExtras(bundle);

startActivity(intent);

}

}

SecondActivity(Next.java)

package com.example.getlistitemvalueinnewactivity;

import android.os.Bundle;
import android.widget.TextView;
import android.app.Activity;


public class Next extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.nextpage);
 TextView tv1=(TextView)findViewById(R.id.textView1);
 Bundle bundle = this.getIntent().getExtras();
 String textName_value = bundle.getString("Value"); 
 tv1.setText(textName_value);
  }
}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" 
android:background="#000000">

<ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true" >
   </ListView>

 </RelativeLayout>

下一页.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="200dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="192dp"
        android:text="TextView" />

</RelativeLayout>

AndroidManifest.xml

  <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.example.getlistitemvalueinnewactivity"
  android:versionCode="1"
  android:versionName="1.0" >

  <uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="15" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/title_activity_main" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
        <activity android:name=".Next"></activity>
      </application>

          </manifest>
于 2013-01-19T16:03:03.390 回答
0

如下所述,将您的列表视图的 ID 设置为“@android:id/list”。

            <ListView
            android:id="@android:id/list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

这可能是一个问题。如果可能的话,也让我们知道您的布局文件。

于 2013-01-19T13:02:02.947 回答
0

改变

setListAdapter (new ArrayAdapter<String>(this,R.layout.main, type));

进入

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

请参阅下面的完整代码

public class Second_listview extends ListActivity
{

static final String[] type = new String[]{

"Array", "Strings" 

 };

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);

setContentView(R.layout.second_listview);

// setting up list view

setListAdapter (new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, type));
ListView list = getListView();
list.setTextFilterEnabled(true);
list.setOnItemClickListener(new OnItemClickListener(){

    @Override
    public void onItemClick(AdapterView<?> arg0, View view, int arg2,
            long arg3) {
        // TODO Auto-generated method stub

        //liking each list item to start a new activity

        switch(arg2)

        {
          case 1 : Intent myIntent1 = new Intent(view.getContext(), Array_list.class);
                   startActivityForResult(myIntent1, 0);
                   break;
          case 2 : Intent myIntent2 = new Intent(view.getContext(), String_list.class);
                   startActivityForResult(myIntent2, 0);
                   break;

        }

    }



});
}
}
于 2013-01-19T13:34:13.570 回答