2012-03-07 170 views
0

我創建了自定義列表視圖,它擴展ListActivity並正常工作。但是我想把一個ListView放在擴展Activity的主要活動中。我怎樣才能做到這一點?使用自定義列表視圖

例如,我想在我的屏幕的一部分中放置自定義ListView,而不是僅由一個ListView擴展ListActivity的屏幕填充屏幕。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent">  
<TextView 
    android:id="@+id/lvresult" 
    android:paddingTop="2dip" 
    android:paddingBottom="3dip" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" /> 
    </LinearLayout> 

,這是MyListActivity:

package Dic.proj.pkg; 
import java.util.ArrayList; 
import android.app.ListActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.ArrayAdapter; 
import android.widget.EditText; 
import android.widget.ListView; 
import android.widget.TextView; 
import android.widget.Toast; 
public class MyListActivity extends ListActivity { 
EditText et; 
TextView tv; 
TextView tvresult; 
String resulttext; 
ArrayList<String> mArrayList = new ArrayList<String>(); 
public void onCreate(Bundle icicle) { 
    super.onCreate(icicle); 
    String[] values = new String[] { "Android", "iPhone", "WindowsMobile", 
      "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X", 
      "Linux", "OS/2" }; 
    // Use your own layout 
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
      R.layout.customlist, R.id.lvresult, values); 
    setListAdapter(adapter); 
} 

@Override 
protected void onListItemClick(ListView l, View v, int position, long id) { 
    String item = (String) getListAdapter().getItem(position); 
    Toast.makeText(this, item + " selected", Toast.LENGTH_LONG).show(); 
    } 
} 

這工作得很好,但我只能看到一個屏幕列表視圖。我想把這個自定義Listview放在我的主要活動中。我怎樣才能做到這一點?

+0

你谷歌? [android中的自定義列表視圖示例](http://www.google.co.in/#hl=zh-CN&safe=active&sclient=psy-ab&q=custom+listview+example+in+android&oq=custom+list&aq=1&aqi=g4&aql=&gs_sm = 3&gs_upl = 182417l185071l1l186944l15l10l2l3l4l1l183l1619l0.10l15l0&gs_l = hp.3.1.0l4.182417l185071l1l186944l15l10l2l3l4l1l183l1619l0j10l15l0&PBX = 1&BAV = on.2,or.r_gc.r_pw.r_qf。,cf.osb&FP = bb4200f2eeeb84b9&BIW = 1280&波黑= 677&安全=上) – 2012-03-07 04:58:19

回答

1

你象下面這樣: -

public class StoreListActivity extends Activity { 
    private List<Store> mStores; 
    private StoreAdapter mStoreAdapter; 
    private ListView mListView; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.store_list); 
     mListView = (ListView) findViewById(R.id.store_listview); 

      mStores = getTheStoresFromSomewhere(); 
     mStoreAdapter = new StoreAdapter(this, mStores); 
     mListView.setAdapter(mStoreAdapter); 
    } 
0
// roadmapscreen.xml 
<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:background="#ffffff" 
    android:layout_height="fill_parent" 
    > 

    <ListView 

     android:cacheColorHint="#00000000" 
android:id="@+id/roadmaplist" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:listSelector="@android:color/transparent" 
android:divider="@null"/> 

</RelativeLayout> 

//roadmap_list_item 
<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="#ffffff" 



    > 

     <TextView 
      android:id="@+id/text" 
     android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
     android:text="Hello" 
     android:textColor="#000000" 
     android:layout_marginTop="12dp" 
     android:layout_marginLeft="12dp" 
     android:textStyle="bold" 
     /> 

    </RelativeLayout> 

//Main Activity 
public class RoadmapActivity extends Activity { 
ListView lv; 
ArrayList<String> catList; 
@Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     setContentView(R.layout.roadmapscreen); 
catList=new ArrayList<String>(); 

     catList.add("Work"); 
     catList.add("Time"); 
     catList.add("Money"); 
     catList.add("Health"); 
     catList.add("Fun & Recreation"); 
     catList.add("Friends & Relatives");    
     catList.add("Spirituality & Growth"); 
     catList.add("Home & Housework"); 
     catList.add("Death & Dying"); 
     catList.add("Sexual Intimacy"); 
     catList.add("Marriage/Commitment"); 
     catList.add("Children"); 
     catList.add("Miscellaneous"); 

lv=(ListView)findViewById(R.id.roadmaplist); 
     lv.setAdapter(new CustomAdapter()); 
} 
public class CustomAdapter extends BaseAdapter 
{ 

    public int getCount() { 
     // TODO Auto-generated method stub 
     return catList.size(); 
    } 

    public Object getItem(int position) { 
     // TODO Auto-generated method stub 
     return position; 
    } 

    public long getItemId(int position) { 
     // TODO Auto-generated method stub 
     return position; 
    } 

    public View getView(final int position, View convertView, ViewGroup parent) { 
     // TODO Auto-generated method stub 
     final ViewHolder holder; 
     LayoutInflater inflater=(LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE); 
     if (convertView == null) 
     { 
      convertView=inflater.inflate(R.layout.roadmap_list_item, null); 
      holder = new ViewHolder(); 


     } 
     else   
     { 
      holder = (ViewHolder) convertView.getTag(); 
     } 
     holder.txt1=(TextView)convertView.findViewById(R.id.text); 
     convertView.setTag(holder); 
     holder.txt1.setText(catList.get(position)); 
     convertView.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 
       // TODO Auto-generated method stub 

      } 
     }); 

     return convertView; 
    } 
    class ViewHolder { 

     TextView txt1; 

    } 

} 
}