onClick to Activity的意图项目

时间:2015-05-21 10:55:49

标签: android listview android-intent android-activity listactivity

我有这个ListActivity用于显示从ListViewActivity获取的项目。

public class ListTasksActivity extends ListActivity implements AdapterView.OnItemClickListener {
    ArrayList<HashMap<String,String>> adapterItems;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent i = getIntent();

        ListView lv = getListView();
        lv.setOnItemClickListener(this);

        ArrayList<HashMap<String, String>> taskItems = (ArrayList<HashMap<String, String>>) i.getSerializableExtra("taskItems");
        String[] stringArr = i.getStringArrayExtra("adapterStringArray");
        int[] intArr = i.getIntArrayExtra("adapterIntArray");

        ListAdapter taskadapter = new SimpleAdapter(this, taskItems,
                R.layout.list_item_tasks,
                stringArr,
                intArr);

        setListAdapter(taskadapter);
        adapterItems = taskItems;
        //CustomAdapterTasks customAdapterTasks = new CustomAdapterTasks((ListViewActivity) ListViewActivity.getAppContext(),);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        String[] adapterString = new String[] {"location","object_id","task_subject","task_action","object"};
        int[] adapterInt = new int[]  {R.id.singleTaskLocation,R.id.singleTaskInvNumber,R.id.singleTaskSubject,R.id.singleTaskAction,R.id.singleTaskObject};

        //neue Oberflaeche starten
        Intent in = new Intent(this,ListSingleTaskActivity.class);
        in.putExtra("adapterStringArray",adapterString);
        in.putExtra("adapterIntArray",adapterInt);
        in.putExtra("taskItems",adapterItems);

        startActivity(in);
    }
}

如果我点击一个项目,则会发生这种情况:

public class ListSingleTaskActivity extends Activity {
    private static final int CAMERA_PIC_REQUEST = 2500;

    ListView mylistview;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.single_task_layout);

        Intent i = getIntent();

        ArrayList<HashMap<String, String>> taskItems = (ArrayList<HashMap<String, String>>) i.getSerializableExtra("taskItems");
        String[] stringArr = i.getStringArrayExtra("adapterStringArray");
        int[] intArr = i.getIntArrayExtra("adapterIntArray");

        ListAdapter taskadapter = new SimpleAdapter(this, taskItems,
                R.layout.single_task_layout,
                stringArr,
                intArr)
}

我的问题是我想将被点击的项目的信息发送到新的活动并显示它们。上面代码的结果是他向我展示了整个项目。不只是点击了一个。 有什么问题?

我希望它清楚我的问题是什么。如果缺少某些内容,请发表评论

编辑:

现在我收到了这个错误:

Caused by: java.lang.ClassCastException: java.util.HashMap cannot be cast to java.util.ArrayList
        at de.example.app.ListSingleTaskActivity.onCreate(ListSingleTaskActivity.java:30)

这一行:

ArrayList<HashMap<String, String>> taskItems = (ArrayList<HashMap<String, String>>) i.getSerializableExtra("taskItems");

4 个答案:

答案 0 :(得分:4)

在ListTasksActivity中发送选定的数据:

Intent in = new Intent(this,ListSingleTaskActivity.class);

in.putExtra("adapterStringArray",adapterString.get(position));
in.putExtra("adapterIntArray",adapterInt[position]);
in.putExtra("taskItems",adapterItems.get(position));

startActivity(in);

答案 1 :(得分:0)

你发送整个数组,这就是为什么你看到onclick里面的整个对象只发送如下所选的对象:

in.putExtra("adapterStringArray",adapterString.get[position]);
in.putExtra("adapterIntArray",adapterInt[position]);
in.putExtra("taskItems",adapterItems.get(position));

对于第二次崩溃问题,试试这个

HashMap<String, String> hashMap = (HashMap<String, String>)   intent.getSerializableExtra("taskItems");

答案 2 :(得分:0)

如下所示更改OnItem单击侦听器,然后它将起作用

 @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        String[] adapterString = new String[] {"location","object_id","task_subject","task_action","object"};
        int[] adapterInt = new int[]  {R.id.singleTaskLocation,R.id.singleTaskInvNumber,R.id.singleTaskSubject,R.id.singleTaskAction,R.id.singleTaskObject};

        //neue Oberflaeche starten
        Intent in = new Intent(this,ListSingleTaskActivity.class);
        in.putExtra("adapterStringArray",adapterString.get(position));
        in.putExtra("adapterIntArray",adapterInt[position]);
        in.putExtra("taskItems",adapterItems.get(position));

        startActivity(in);
    }

答案 3 :(得分:0)

现在你在意图中传递完整的数组。

实际上,您需要传递已在意图中单击的项目的值。 使用此getItemAtPosition(位置)并将其发送到in.putExtra,然后只有您才能获得所选项目。

了解更多信息。 http://developer.android.com/reference/android/widget/AdapterView.OnItemClickListener.html