2011-04-19 60 views
0
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical"> 
<LinearLayout android:id="@+id/LinearLayout1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"> 
    <Button 
     android:text="AddCar" 
     android:id="@+id/btnaddcar" 
     android:textSize="20dp" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 
    </Button> 
</LinearLayout> 
<ListView android:id="@id/android:list" 

android:layout_width="fill_parent" 
android:layout_height="fill_parent" 

android:layout_weight="2" 
android:drawSelectorOnTop="false"> 
</ListView> 
</LinearLayout> 


protected void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setContentView(LayoutInflater.from(getParent()).inflate(R.layout.carlist, null)); 
//  setContentView(R.layout.carlist); 
    db = new Database(getApplicationContext()); 
    db.open(); 
    //listview = (ListView) findViewById(R.id.list1); 
    cararr=new String[db.getCountCar()]; 
    carimg=new String[db.getCountCar()]; 
    carimg=db.getImageList(); 
    cararr=db.getAllCarName(); 



    /*adapter=new LazyAdapter(this, db.getImageList(),db.getAllCarName()); 

    listview.setAdapter(adapter);*/ 

    cararr=db.getAllCarName(); 
    SimpleAdapter adapter = new SimpleAdapter(
       this, 
       groupData, 
       R.layout.custom_row_view, 
       new String[] {"carname","image"}, 
       new int[] {R.id.text1, R.id.image} 
       ); 
    populateList(); 
     setListAdapter(adapter); 
    //listview.setOnItemClickListener(this); 
// listview.setOnItemSelectedListener(this); 

    db.close(); 
    btnaddcar = (Button)findViewById(R.id.btnaddcar); 
    btnaddcar.setOnClickListener(this); 
} 
private void populateList() { 
    // TODO Auto-generated method stub 

    Map<String, String> group; 
    db.open(); 
    cararr=new String[db.getCountCar()]; 
    carimg=new String[db.getCountCar()]; 
    carimg=db.getImageList(); 
    cararr=db.getAllCarName(); 
    for(int i=0;i<db.getCountCar();i++) 
    { 
     group = new HashMap<String, String>(); 
     group.put("carname", cararr[i]); 
     group.put("image",carimg[i]); 
     groupData.add(group); 
    } 
    db.close(); 
} 

如何在ListView上設置onItemselectlistener?如何在ListView上設置onItemselectlistener?

回答

0

這是示例代碼列表OnItemClickListener。

我的代碼是:

String strarray[]=new String[100]; 
ArrayAdapter<String> adapterarray=new ArrayAdapter<String>(Classname.this,android.R.layout.simple_expandable_list_item_1,strarray); 
lv.setAdapter(adapterarray); 
lv.setTextFilterEnabled(true); 

lv.setOnItemClickListener(new OnItemClickListener() { 

    @Override 
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) 
    { 
     // TODO Auto-generated method stub 
     String str=lv.getItemAtPosition(arg2).toString(); 
     Toast.makeText(getApplicationContext(), "List is:"+str, Toast.LENGTH_LONG).show(); 

    Log.i(TAG,str); 

    } 
}); 

希望這將幫助!