2014-10-29 35 views
-1

我想填充單擊新列表視圖中的項目時在新活動中打開的網格視圖。如何實施?在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); 
} 

}

+0

歡迎來到StackOverflow,您已經提出了一個相對廣泛的問題,這意味着讀者無法判斷(a)您是否是Android新手,並且正在詢問一個涵蓋許多在線教程的基本問題,或者(b)描述你可以改進的問題,告訴我們你試過/讀過/研究過什麼。 – 2014-10-29 16:04:12

回答

1
  1. 設置clickListener上的ListView項目。
  2. 創建新活動並將id(使用意圖)傳遞給新活動。
  3. 使用ID填充新活動。

您可以使用自定義適配器。

如果您是Android新手,請先遵循一些基本教程。

+0

如何使用Id填充新活動? – amantheleo92 2014-12-03 13:46:20

+0

你應該問一個單獨的問題,而不是使用評論。 這樣你的問題就會被更多人看到。 – sasquatch 2014-12-04 06:38:41

相關問題