我正在尝试将ciicked listitem数据传递给另一个活动,但每当我点击任何列表项时都无法获取Toast,也无法将所选列表项值传递给另一个活动。
所以我想知道:
如何使用点击列表项将值从一个活动传递到另一个活动?
和第二
为什么不将点击列表项目位置显示到Toast中?
MainActivity.java:
@Override
protected void onPostExecute(Void args) {
// Locate the listview in listview_main.xml
listview = (ListView) findViewById(R.id.listview);
// Pass the results into ListViewAdapter.java
adapter = new ListViewAdapter(MainActivity.this, arraylist);
// Set the adapter to the ListView
listview.setAdapter(adapter);
// Close the progressdialog
mProgressDialog.dismiss();
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(MainActivity.this, position, Toast.LENGTH_LONG).show();
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this, SingleItemView.class);
// Pass all data rank
intent.putExtra("rank", arraylist.get(position).get(MainActivity.RANK));
// Start SingleItemView Class
startActivity(intent);
}
});
}
在 Log.d 收到错误后,请查看下面的完整日志
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(MainActivity.this, position, Toast.LENGTH_LONG).show();
// TODO Auto-generated method stub
Log.d("RANK::", arraylist.get(position).get(MainActivity.RANK));
日志:
06-04 18:21:12.725: E/AndroidRuntime(19957): FATAL EXCEPTION: main
06-04 18:21:12.725: E/AndroidRuntime(19957): android.content.res.Resources$NotFoundException: String resource ID #0x0
06-04 18:21:12.725: E/AndroidRuntime(19957): at android.content.res.Resources.getText(Resources.java:242)
06-04 18:21:12.725: E/AndroidRuntime(19957): at android.widget.Toast.makeText(Toast.java:304)
06-04 18:21:12.725: E/AndroidRuntime(19957): at com.androidbegin.jsonparsetutorial.MainActivity$DownloadJSON$1.onItemClick(MainActivity.java:107)
06-04 18:21:12.725: E/AndroidRuntime(19957): at android.widget.AdapterView.performItemClick(AdapterView.java:301)
06-04 18:21:12.725: E/AndroidRuntime(19957): at android.widget.AbsListView.performItemClick(AbsListView.java:1280)
06-04 18:21:12.725: E/AndroidRuntime(19957): at android.widget.AbsListView$PerformClick.run(AbsListView.java:3071)
06-04 18:21:12.725: E/AndroidRuntime(19957): at android.widget.AbsListView$1.run(AbsListView.java:3972)
06-04 18:21:12.725: E/AndroidRuntime(19957): at android.os.Handler.handleCallback(Handler.java:615)
06-04 18:21:12.725: E/AndroidRuntime(19957): at android.os.Handler.dispatchMessage(Handler.java:92)
06-04 18:21:12.725: E/AndroidRuntime(19957): at android.os.Looper.loop(Looper.java:137)
06-04 18:21:12.725: E/AndroidRuntime(19957): at android.app.ActivityThread.main(ActivityThread.java:4921)
06-04 18:21:12.725: E/AndroidRuntime(19957): at java.lang.reflect.Method.invokeNative(Native Method)
06-04 18:21:12.725: E/AndroidRuntime(19957): at java.lang.reflect.Method.invoke(Method.java:511)
06-04 18:21:12.725: E/AndroidRuntime(19957): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1036)
06-04 18:21:12.725: E/AndroidRuntime(19957): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:803)
06-04 18:21:12.725: E/AndroidRuntime(19957): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
尝试在onCreat中编写以下行而不是onPostExecute
listview = (ListView) findViewById(R.id.listview);
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(MainActivity.this, position, Toast.LENGTH_LONG).show();
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this, SingleItemView.class);
// Pass all data rank
intent.putExtra("rank", arraylist.get(position).get(MainActivity.RANK));
// Start SingleItemView Class
startActivity(intent);
}
});
答案 1 :(得分:1)
更改此
Toast.makeText(MainActivity.this, position, Toast.LENGTH_LONG).show();
到
Toast.makeText(MainActivity.this, String.valueOf(position), Toast.LENGTH_LONG).show();
答案 2 :(得分:0)
尝试使用setTag(Object)和getTag()方法。在表示ListView中的行的每个视图上应用setTag()。然后,在onItemClick()方法中使用&#34; view&#34;变量,作为参数传递,使用getTag()提取传递的值。您的解决方案看起来很好,但由于优化原因,建议使用标签将值传递给onClicks。