我想填充單擊新列表視圖中的項目時在新活動中打開的網格視圖。如何實施?在Android應用開發如何在點擊ListView中的項目時填充新打開的網格視圖
公共類MainActivity工作擴展ActionBarActivity {
private List<cars> myCars=new ArrayList<cars>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
populateCarsList();
populateListView();
registerClickCallback();
}
private void populateCarsList() {
// TODO Auto-generated method stub
myCars.add(new cars("Ford",1950,0,"Aman"));
myCars.add(new cars("Honda",1951,0,"Akhil"));
myCars.add(new cars("Fiat",1952,0,"Anil"));
myCars.add(new cars("Mahindra",1953,0,"Ankit"));
myCars.add(new cars("Maruti",1954,0,"Venu"));
myCars.add(new cars("Hyundai",1955,0,"Neeta"));
myCars.add(new cars("Toyota",1956,0,"Naresh"));
myCars.add(new cars("BMW",1957,0,"Amit"));
myCars.add(new cars("Merc",1958,0,"Arjun"));
myCars.add(new cars("Jaguar",1959,0,"Akshit"));
}
private void populateListView() {
ArrayAdapter<cars> adapter=new AmanListAdapter();
ListView list=(ListView) findViewById(R.id.carsListView);
list.setAdapter(adapter);
}
private void registerClickCallback() {
ListView list=(ListView) findViewById(R.id.carsListView);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View viewClicked,int position, long id)
{
cars clickedCar=myCars.get(position);
String message="You clicked position "+position+" which is "+clickedCar.getMake();
Toast.makeText(MainActivity.this,message,Toast.LENGTH_LONG).show();
if(position==0)
{
startActivity(new Intent(MainActivity.this,GridView1Activity.class));
}
else
{
startActivity(new Intent(MainActivity.this,GridView2Activity.class));
}
}
});
}
private class AmanListAdapter extends ArrayAdapter<cars>
{
public AmanListAdapter(){
super(MainActivity.this,R.layout.item_view,myCars);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View itemView=convertView;
if(itemView==null)
itemView=getLayoutInflater().inflate(R.layout.item_view,parent,false);
//Find the car to work with
cars currentCar=myCars.get(position);
//Fill the view
ImageView imageView=(ImageView)itemView.findViewById(R.id.item_Icon);
imageView.setImageResource(currentCar.geticonID());
//Make
TextView makeText=(TextView) itemView.findViewById(R.id.item_txtMake);
makeText.setText(currentCar.getMake());
//Year
TextView yearText=(TextView) itemView.findViewById(R.id.item_txtYear);
yearText.setText(""+currentCar.getYear());
//Condition
TextView conditionText=(TextView) itemView.findViewById(R.id.item_txtCondition);
conditionText.setText(currentCar.getCondition());
return itemView;
}
}
@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;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;`enter code here`
}
return super.onOptionsItemSelected(item);
}
}
歡迎來到StackOverflow,您已經提出了一個相對廣泛的問題,這意味着讀者無法判斷(a)您是否是Android新手,並且正在詢問一個涵蓋許多在線教程的基本問題,或者(b)描述你可以改進的問題,告訴我們你試過/讀過/研究過什麼。 – 2014-10-29 16:04:12