从列表中打开新窗口

时间:2012-03-21 15:52:38

标签: android listview window new-operator

我已经制作了一个国家列表,我希望它在点击其中一个项目时打开一个新窗口,我不能这样做= /
这里是我的java代码

public class Country_list extends ListActivity{<br>
    @Override<br>
    protected void onCreate(Bundle savedInstanceState) {<br>
        // TODO Auto-generated method stub<br>
        super.onCreate(savedInstanceState);<br>
        setContentView(R.layout.main);<br>

        setListAdapter(new MyAdapter(this,
                android.R.layout.simple_list_item_1,R.id.imageView1,
                getResources().getStringArray(R.array.country)));
    }
  private class MyAdapter extends ArrayAdapter<String>{

        public MyAdapter(Context context, int resource, int textViewResourceId,
                String[] strings) {
            super(context, resource, textViewResourceId, strings);
            // TODO Auto-generated constructor stub
}
        public View getView(int position, View convertView, ViewGroup parent){

        LayoutInflater inflater= (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            View row = inflater.inflate(R.layout.list_country,parent, false);

            String[] items=getResources().getStringArray(R.array.country);

            ImageView iv= (ImageView) row.findViewById(R.id.imageView1);
            TextView tv=(TextView) row.findViewById(R.id.textView1);

            tv.setText(items[position]);

            if(items[position].equals("English")){
                iv.setImageResource(R.drawable.ic_english_h);
            }
            else if(items[position].equals("French")){
                iv.setImageResource(R.drawable.ic_france_h);
            }
            else if(items[position].equals("German")){
                iv.setImageResource(R.drawable.ic_german_h);
            }
            else if(items[position].equals("Turkish")){
                iv.setImageResource(R.drawable.ic_turky_h);
            }
            else if(items[position].equals("Chinese")){
                iv.setImageResource(R.drawable.ic_china_h);
            }
            else if(items[position].equals("Italian")){
                iv.setImageResource(R.drawable.ic_italy_h);
            }
            else if(items[position].equals("Russian")){
                iv.setImageResource(R.drawable.ic_russia);
            }
            return row;
        }
    }

1 个答案:

答案 0 :(得分:0)

在包含列表的主要活动中实施OnItemClickListener

public void onItemClick(AdapterView<?> arg0, View view, int position,long id) {
    Intent intent = new Intent(this, SecondActivity.class);
    intent.putExtra("data1", "countryname");
    intent.putExtra("data2", "countryCode");
    startActivity(intent);
}

如果要将数据传递给第二个活动,请在putExtra函数中指定它们,您可以像这样检索第二个活动中的数据

String data1 = getIntent().getStringExtra("data1");
String data2 = getIntent().getStringExtra("data2");

确保在清单文件中添加第二个活动

<activity android:name=".SecondActivity">
     <intent-filter >
          <action android:name="android.intent.action.VIEW" />

          <category android:name="android.intent.category.LAUNCHER" />
     </intent-filter>
</activity>

希望这会有所帮助。