onClick ListView帮助

时间:2011-05-26 20:33:36

标签: android listview onclick

好的,我对Android和编程很新。我按照Android开发者页面上的教程创建了一个列表视图。现在我真的想用它做点什么。我让它去了一个视图,但是我希望列表中的两个项目分别用于不同的Activity。这是我已有的代码。

package com.pais.convert;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class list extends ListActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);

      String[] choose = getResources().getStringArray(R.array.list_chooser);
      setListAdapter(new ArrayAdapter<String>(this,
              android.R.layout.simple_list_item_1, choose));

      ListView lv = getListView();
      lv.setTextFilterEnabled(true);
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        Intent intent = new Intent(this, tempConvert.class);
        intent.putExtra("KEY_SELECTED_INDEX", position);
        startActivity(intent);
    }



    }

我有一个不同的活动但是如何在listView中有两个单独的项目,每个项目都会进行不同的活动。但仍然从同一个名单发布?

感谢所有帮助。

1 个答案:

答案 0 :(得分:1)

对于ListActivity,最简单的方法是在活动本身中覆盖onListItemClick

编辑:

这就是我的意思:

public class test extends ListActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);

      String[] choose = getResources().getStringArray(R.array.list_chooser);
      setListAdapter(new ArrayAdapter<String>(this,
              android.R.layout.simple_list_item_1, choose));

      ListView lv = getListView();
      lv.setTextFilterEnabled(true);
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        // Start another activity to do something with the selected item.
        // I'll assume the other activity is defined in the class
        // AnotherActivity:
        Intent intent = new Intent(this, AnotherActivity.class);
        // now you can add additional information to the intent for the
        // other activity to use. For instance, to pass just the index of the
        // selected item, you could code:
        intent.putExtra("KEY_SELECTED_INDEX", position);
        // (The string "KEY_SELECTED_INDEX" is an arbitrary string you choose
        // to name this piece of data. AnotherActivity will use the same name
        // to retrieve it. Other extras would be added under different names.)
        startActivity(intent);
    }
}

然后你必须定义一个单独的活动来显示第二个视图:

public class AnotherActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent intent = getIntent();
        int position = intent.getIntExtra("KEY_SELECTED_INDEX", -1);
        if (position == -1) {
            Toast.makeText(this, "No selection to show!", Toast.DURATION_LONG)
                 .show();
        }

        // continue with setting up the activity
    }
}

您还需要将此第二个活动添加到您应用的清单文件中。