获取按钮单击Android的当前项目详细信息

时间:2015-04-19 21:39:39

标签: android

        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> a,
                                View v, int position, long id) {
            b.setTag(position);
            City city = (City) a.getItemAtPosition(position);
            Intent intent = new Intent(v.getContext(), DetailsActivity.class);
            intent.putExtra("in.wptrafficanalyzer.listviewcheckbox.City", city);
            startActivity(intent);
        }

    });

                b.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View arg0) {
                    int position = (Integer)arg0.getTag();
                    String name = mListView.getItemAtPosition(position).toString();
              }
            });

这是我到目前为止关于项目列表/项目详细信息所做的工作。列表工作正常,当我点击特定项目时会显示详细信息。

我想从当前所选项目的TextView中获取详细信息。

例如,假设我有列表项Carrot。我点击它,显示项目的详细信息:Carrots have vitamin A。如何获取该字符串并将其分配给String变量?

我已尝试获得该位置,然后将该项目移至该位置。我没有尝试获取该项目的详细信息,因为它给了我一个错误。

我在Android Studio工作,无论我得到什么错误都打印相同的堆栈?允许我确定错误放置位置的唯一因素是它指定了行。

谢谢..

编辑:堆栈跟踪

04-20 00:30:19.335  12519-12519/in.wptrafficanalyzer.listviewcheckbox E/Trace﹕ error opening trace file: No such file or directory (2)
04-20 00:30:19.495  12519-12519/in.wptrafficanalyzer.listviewcheckbox W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41f6e390)
 04-20 00:30:19.505  12519-12519/in.wptrafficanalyzer.listviewcheckbox E/AndroidRuntime﹕ FATAL EXCEPTION: main

 java.lang.RuntimeException: Unable to start activity ComponentInfo{in.wptrafficanalyzer.listviewcheckbox/in.wptrafficanalyzer.listviewcheckbox.MainActivity}: java.lang.NullPointerException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2355)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2391)
        at android.app.ActivityThread.access$600(ActivityThread.java:151)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1335)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:155)
        at android.app.ActivityThread.main(ActivityThread.java:5511)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:796)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
        at in.wptrafficanalyzer.listviewcheckbox.MainActivity.onCreate(MainActivity.java:75)
        at android.app.Activity.performCreate(Activity.java:5066)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1101)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2311)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2391)
        at android.app.ActivityThread.access$600(ActivityThread.java:151)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1335)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:155)
        at android.app.ActivityThread.main(ActivityThread.java:5511)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:796)
        at dalvik.system.NativeStart.main(Native Method)

注意:我没有尝试获取当前所选项目,而是项目的详细信息,因为按钮b位于项目详细信息布局中。

1 个答案:

答案 0 :(得分:0)

onItemClick(AdapterView<?> a, View v, int position, long id)内,您可能会使用mListView.getAdapter().getItem(position);获得排名值。另外View v是你点击的整个列表行,所以如果你只需要文本,你可以使用像:

String yourText = 
    ((TextView)v.findViewById(R.id.textViewInRow)).getText().toString();

如果您使用某种默认适配器(例如ArrayAdapter),默认ID可能是android.R.id.text1android.R.id.text(取决于适配器的deafult视图和每行中的视图数)