2012-12-02 3 views
1

사용자 지정 행을 사용하여 목록보기를 만들고 싶습니다. ListActivity을 확장하는 클래스와 각 행 항목에 대한 XML을 만들었습니다. 또한 더미 데이터를 생성하는 메서드를 추가했습니다. 그러나 이것은 내가 붙어있는 곳이다. 내 주 수업에서 이것을 호출하고 main.xml에 listview를 채우려면 어떻게해야합니까?사용자 지정 목록보기 채우기

CustomListView.java 사용자 지정 목록 셀을 만들기

public class CustomListView extends ListActivity { 

private ArrayList<HashMap<String,String>> list; 

public CustomListView(){ 

    list = new ArrayList<HashMap<String,String>>(); 

    SimpleAdapter adapter = new SimpleAdapter(
      this, 
      list, 
      R.layout.custom_list_item, 
      new String[] {"pen","price","color"}, 
      new int[] {R.id.text1,R.id.text2, R.id.text3}); 

    populateList(); 

    setListAdapter(adapter); 

} 

private void populateList() { 

    HashMap<String,String> temp = new HashMap<String,String>(); 
    temp.put("pen","MONT Blanc"); 
    temp.put("price", "200.00$"); 
    temp.put("color", "Silver, Grey, Black"); 
    list.add(temp); 
    HashMap<String,String> temp1 = new HashMap<String,String>(); 
    temp1.put("pen","Gucci"); 
    temp1.put("price", "300.00$"); 
    temp1.put("color", "Gold, Red"); 
    list.add(temp1); 
    HashMap<String,String> temp2 = new HashMap<String,String>(); 
    temp2.put("pen","Parker"); 
    temp2.put("price", "400.00$"); 
    temp2.put("color", "Gold, Blue"); 
    list.add(temp2); 
    HashMap<String,String> temp3 = new HashMap<String,String>(); 
    temp3.put("pen","Sailor"); 
    temp3.put("price", "500.00$"); 
    temp3.put("color", "Silver"); 
    list.add(temp3); 
    HashMap<String,String> temp4 = new HashMap<String,String>(); 
    temp4.put("pen","Porsche Design"); 
    temp4.put("price", "600.00$"); 
    temp4.put("color", "Silver, Grey, Red"); 
    list.add(temp4); 

} 

} 

custom_list_item.xml는

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" > 

<TextView android:id="@+id/text1" 
android:textSize="16sp" 
android:textStyle="bold" 
android:textColor="#FFFF00" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"/> 

<TextView android:id="@+id/text2" 
android:textSize="12sp" 
android:textStyle="bold" 
android:layout_width="wrap_content" 
android:layout_height="fill_parent"/> 

<TextView android:id="@+id/text3" 
android:typeface="sans" 
android:textSize="14sp" 
android:textStyle="italic" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content"/> 

답변

2

vogalla's tutorial on listView and Adapter을 읽어보세요. 이럴 필요 없어.

2 오버라이드 onCreate 방법

public void onCreate(Bundle icicle) { 
    super.onCreate(icicle); 
    list = new ArrayList<HashMap<String, String>>(); 
    populateList(); 
    SimpleAdapter adapter = new SimpleAdapter(this, list, 
      R.layout.custom_list_item, new String[] { "pen", "price", 
        "color" }, new int[] { R.id.text1, R.id.text2, 
        R.id.text3 }); 
    setListAdapter(adapter); 
} 

3 클릭 버튼이 원하는

주요 Activity.you에서 시작하는 방법
<activity 
     android:name=".CustomListView" 
     /> 

4처럼 manifest 파일에 활동을 추가 하시겠습니까? 그럼 당신은 사용할 수 Intents

startActivity(new Intent(yourMainActivity.this,CustomListView.class)) 
1

사용자 정의 어댑터를 만드는 것이 실제로 의미한다.

목록에 대한 데이터 원본 유형을 선택하면 해당 데이터 원본에 대한 어댑터를 만들고 원하는대로 정보를 표시합니다.

일반적으로 발생하는 현상은 getView 메서드를 구현하고 일반보기 대신 사용자 지정보기를 부 풀릴 필요가 있다는 것입니다. 여기

사용자 정의 어댑터 코드 샘플입니다 :

public class LazyAdapter extends BaseAdapter { 

    private Activity activity; 
    private String[] data; 
    private static LayoutInflater inflater=null; 
    public ImageLoader imageLoader; 

    public LazyAdapter(Activity a, String[] d) { 
     activity = a; 
     data=d; 
     inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     imageLoader=new ImageLoader(activity.getApplicationContext()); 
    } 

    public int getCount() { 
     return data.length; 
    } 

    public Object getItem(int position) { 
     return position; 
    } 

    public long getItemId(int position) { 
     return position; 
    } 

    public View getView(int position, View convertView, ViewGroup parent) { 
     View vi=convertView; 
     if(convertView==null) 
      vi = inflater.inflate(R.layout.item, null); 

     TextView text=(TextView)vi.findViewById(R.id.text);; 
     ImageView image=(ImageView)vi.findViewById(R.id.image); 
     text.setText("item "+position); 
     imageLoader.DisplayImage(data[position], image); 
     return vi; 
    } 
} 

는 50 %가 몇 가지 단계가

1 CustomListView의 생성자를 제거 남아있다 할 수 있습니다

+0

그럼'SimpleAdapter' 충분하지 않아? – Johan

+0

셀의 사용자 지정 레이아웃을 만들려는 경우 아니요. 네가 1-2 줄의 텍스트와 아이콘을 원하면 네. – thepoosh

관련 문제