通过onClick从listAdapter打开新的xml

时间:2014-07-23 11:08:53

标签: android android-listview android-asynctask rss-reader

我已经创建了一个rss阅读器,现在我可以看到带有主题的列表。 下一步是在单击列表项时打开包含主题内容的新窗口。 它会这样工作:

 protected void onPostExecute(List<String> rssFeed) {
    if (rssFeed != null) {
        setListAdapter(new ArrayAdapter<>(
                getActivity(),
                android.R.layout.simple_list_item_1,
                android.R.id.text1,
                rssFeed));

    }
    ListView lv = getListView();
    lv.setOnItemClickListener(new AdapterView.OnItemClickListener()
    {
        @Override
        public void onItemClick(AdapterView<?> adapter, View v, int position,
                                long arg3)
        {

        }
    });

如果是, 我怎么能打开一个只显示一些文字的新窗口?例如,简单地说“Hello World”

这是开始新意图的唯一方法吗?如果是,这意味着我需要一个新的课程?

或者我可以创建另一个layout.xml,定义文本字段并通过其ID调用它吗?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="HelloWold"
        android:id="@+id/textView"
        android:layout_gravity="center_horizontal" />
</LinearLayout>

这可能吗?

如果我想通过Intent生成,请使用我的onItemClick方法。

  Intent intent = new Intent(view.getContext(), Activity.class);

但是如何才能获得view.getContext的观点?

我非常困惑:D

编辑:

我尝试了这个:

protected void onPostExecute(List<String> rssFeed) {
                if (rssFeed != null) {
                    setListAdapter(new ArrayAdapter<>(
                            getActivity(),
                            android.R.layout.simple_list_item_1,
                            android.R.id.text1,
                            rssFeed));

                }
                ListView lv = getListView();
                lv.setOnItemClickListener(new AdapterView.OnItemClickListener()
                {
                    @Override
                    public void onItemClick(AdapterView<?> adapter, View v, int position,
                                            long arg3)
                    {

                        Intent intent = new Intent(v.getContext(), Activity2.class);
                        startActivityForResult(intent, 0);
                    }

                });
            }

它似乎不起作用,我的清单xml也找不到<activity android:name=".Activity2"></activity>

我收到此错误:

`07-23 13:35:05.019  21802-21802/com.example.MPAK.mpaknewsreader E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.MPAK.mpaknewsreader, PID: 21802
    android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.MPAK.mpaknewsreader/com.example.MPAK.mpaknewsreader.MainActivity$Activity2}; have you declared this activity in your AndroidManifest.xml?
            at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1767)
            at android.app.Instrumentation.execStartActivity(Instrumentation.java:1680)
            at android.app.Activity.startActivityFromFragment(Activity.java:4038)
            at android.app.Fragment.startActivityForResult(Fragment.java:1156)
            at android.app.Fragment.startActivityForResult(Fragment.java:1140)
            at com.example.MPAK.mpaknewsreader.MainActivity$PlaceholderFragment$GetRssFeedTask$1.onItemClick(MainActivity.java:243)
            at android.widget.AdapterView.performItemClick(AdapterView.java:299)
            at android.widget.AbsListView.performItemClick(AbsListView.java:1282)
            at android.widget.ListView.performItemClick(ListView.java:4450)
            at android.widget.AbsListView$PerformClick.run(AbsListView.java:3174)
            at android.widget.AbsListView$3.run(AbsListView.java:3925)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:157)
            at android.app.ActivityThread.main(ActivityThread.java:5872)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:852)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:668)
            at dalvik.system.NativeStart.main(Native Method)`

但它在清单xml中声明...它只是找不到它。

在我的MainActivity类中,我有一个名为:

的类
public class Activity2 extends Activity {

    /**
     * Called when the activity is first created.
     */
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test);

    }
}

为什么它找不到它?

1 个答案:

答案 0 :(得分:0)

首先在清单中定义Activity2.java,如下所示:

  <activity android:name="com.example.hello.Activity2"/>

然后像这样定义Intent

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

而不是:

                  Intent intent = new Intent(v.getContext(), Activity2.class);
                    startActivityForResult(intent, 0);