单击列表视图时,在活动中显示不同的信息

时间:2015-02-08 22:37:02

标签: android arrays android-intent android-listview row

我希望listview打开一个新活动,并在listview的某一行中单击时显示某些信息。 到目前为止,我只能在点击每一行时显示相同的信息。我不知道如何为特定行设置这些信息,以便例如当我点击第1行时显示此地址,当我点击第2行时显示的另一个地址.....我的代码如下

第一项活动

 // storing string resources into Array
 String []Buildings_halls = getResources().getStringArray(R.array.halls);
    // Binding resources Array to ListAdapter
    this.setListAdapter(new ArrayAdapter<String>(this, R.layout.hallstudent, R.id.listhall, Buildings_halls));

 }

 protected void onListItemClick(ListView lv, View v, int position, long id){
     super.onListItemClick(lv, v, position, id);     

Intent intent = new Intent(StudentHall.this, StudentHallSelect.class);
Bundle bundle = new Bundle();
bundle.putInt("position", position);
// Or / And
intent.putExtra("id", String.valueOf(id));
intent.putExtra("description",getResources().getStringArray(R.array.hallsdescription));
intent.putExtras(bundle);
startActivity(intent);

        }               

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    finish();
}
}

第二项活动

        Bundle intent = getIntent().getExtras();

                if (intent !=null){
        int position = intent.getInt("position", 0);     

        String[] halldetails = getResources().getStringArray(R.array.hallsdescription);

        txt = (TextView) findViewById (R.id.select_details);
        txt.setText(halldetails[1]);

        txt2=(TextView)findViewById(R.id.details_select);
        txt2.setText(halldetails[0]); 

        // Here we turn your string.xml in an array
        String[] myKeys = getResources().getStringArray(R.array.halls);

        TextView myTextView = (TextView) findViewById(R.id.selecthalllist);
        myTextView.setText(myKeys[position]);


    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

即我要显示的第1行的信息

<string-array name="descriptions">
           <item>Address:</item>
                <item>  Contact: </item>
                      <item>OpeningHours:</item>
               </string-array>

即我要显示的第1行的信息

  <string-array name="hallsdescription">
           <item >Address:, Telephone No:,Opening Times:</item>
           <item >Address:Catherine Street, Telephone No:657263912739,Opening Times:23:00</item>

感谢

1 个答案:

答案 0 :(得分:0)

protected void onListItemClick(ListView lv, View v, int position, long id){
 super.onListItemClick(lv, v, position, id);     

    Intent intent = new Intent(StudentHall.this, StudentHallSelect.class);
    switch(position){
    case 0:
        intent.putExtra("description",getResources().getStringArray(R.array.hallsdescription)[0]);
        break;
    case 1:
        intent.putExtra("description",getResources().getStringArray(R.array.hallsdescription)[1]);
        break;
    }
    startActivity(intent);
}    

在你的第二项活动中,

txt.setText(getIntent().getExtras().getString("description"));