我想填充点击列表视图中的项目时在新活动中打开的网格视图。如何实施?致力于Android应用开发
公共类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);
}
}
答案 0 :(得分:1)
您可以使用自定义适配器。
如果您不熟悉Android,请先阅读一些基本教程。