2013-10-21 69 views
0

我來填充自定義列表視圖。 此列表視圖包含一個文本框。如何使用setAdapter無需擴展ListActivity

列表視圖XML:

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

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@drawable/panel_shape" > 

    <Button 
     android:id="@+id/back_btn2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_margin="10dp" 
     android:text="Back_Recent_Song" /> 
</LinearLayout> 

<ListView 
    android:id="@+id/listsong_recent_listview" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@color/white" > 
</ListView> 

這是自定義列表

<?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" 
    android:gravity="center" 
    android:padding="5dp"> 
    <TextView 
    android:id="@+id/songTitlerecent" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:textSize="16dp" 
    android:padding="10dp" 
    android:color="#f3f3f3"/> 
</LinearLayout> 

主要XML文件:

 <include android:id="@+id/list_song_layout2" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     layout="@layout/list_recentsongs_screen" /> 

如何使用setlistadapter不延長listactivity?

代碼:

  ListView recent = (ListView)findViewById(R.id.listsong_recent_listview); 
     ArrayList<Data> List = new ArrayList<Data>(); 
    //Populate the array list 
    Log.d("Debug1*****************","******************"); 

    List.add(new Data("test album1")); // Set Name 
    List.add(new Data("test album2")); 
    List.add(new Data("test album3")); 


    for (int i = 0; i < List.size(); i++) { 
      // creating new HashMap 
      Data dataobj = List.get(i); 

      String Name = dataobj.getName(); 
      Log.d("Debug" ,"Name = " + Name); 


    } 
    //-------------------------------------------------------------------------------------------// 
    Log.d("Debug2*****************","******************"); 
    Integer size = List.size(); 

    Log.d("Debug3*****************","******************"); 
    Log.d("DEBUG123","size of list "+ size); 

    Log.d("Debug4*****************","******************"); 
    m_adapter = new MetadataArrayAdapter(this, R.layout.recentlistsong_item, List); // MetadataArrayAdapter is a class extending ArrayAdapter<Data> 

    // Data is a pojo class 

    recent.findViewById(R.id.listsong_recent_listview); 

    Log.d("Debug7*****************","******************"); 
    recent.setAdapter(m_adapter); 

MetaAdapter類:

public class MetadataArrayAdapter extends ArrayAdapter<MusicMetadata> { 

// declaring our ArrayList of items 
private ArrayList<MusicMetadata> objects; 

/* here we must override the constructor for ArrayAdapter 
* the only variable we care about now is ArrayList<PojoClass> objects, 
* because it is the list of objects we want to display. 
*/ 
public MetadataArrayAdapter(Context context, int textViewResourceId, ArrayList<MusicMetadata> objects) { 
    super(context, textViewResourceId, objects); 
    this.objects = objects; 
} 

/* 
* we are overriding the getView method here - this is what defines how each 
* list item will look. 
*/ 
public View getView(int position, View convertView, ViewGroup parent){ 

    // assign the view we are converting to a local variable 
    View v = convertView; 

    // first check to see if the view is null. if so, we have to inflate it. 
    // to inflate it basically means to render, or show, the view. 
    if (v == null) { 
     LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     v = inflater.inflate(R.layout.listsong_item, null); 
    } 

    /* 
    * Recall that the variable position is sent in as an argument to this method. 
    * The variable simply refers to the position of the current object in the list. (The ArrayAdapter 
    * iterates through the list we sent it) 
    * 
    * Therefore, i refers to the current Item object. 
    */ 
    MusicMetadata i = objects.get(position); 

    if (i != null) { 

     // This is how you obtain a reference to the TextViews. 
     // These TextViews are created in the XML files we defined. 

     TextView tt = (TextView) v.findViewById(R.id.songTitlerecent); 


     // check to see if each individual textview is null. 
     // if not, assign some text! 
     if (tt != null){ 
      tt.setText("Title :" + i.getAlbum()); 
     } 

    } 

    // the view must be returned to our activity 
    return v; 
} 

}

我無法看到列表中。還有什麼我必須在這裏添加?

參考鏈接:android using setlistadapter() without extending listactivity

如何使用setAdapter不擴展列表的活動?

請讓我知道什麼是錯?

由於提前

+0

的ListView。setAdapter(<你的適配器>); – nurisezgin

+0

發佈您的適配器代碼'MetadataArrayAdapter' – Raghunandan

+0

我發佈了metadataarrayAdapter – Jeet

回答

0

Intialize列表視圖和使用setAdapter設置適配器的ListView

m_adapter = new MetadataArrayAdapter(this, R.layout.recentlistsong_item, RecentSongsList); 
ListView recent = (ListView)findViewById(R.id.listsong_recent_listview); 
recent.setAdapter(m_adapter); 

刪除此recent.findViewById(R.id.listsong_recent_listview)

我無法看到列表中。還有什麼我必須在這裏添加?

檢查網卡的代碼,看看代碼getView

編輯:

public class MainActivity extends Activity { 
    MetadataArrayAdapter m_adapter; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.test); 
     ListView recent = (ListView)findViewById(R.id.lv); 
     ArrayList<Data> List = new ArrayList<Data>(); 
     List.add(new Data("test album1")); // Set Name 
     List.add(new Data("test album2")); 
     List.add(new Data("test album3")); 
     m_adapter = new MetadataArrayAdapter(this, R.layout.list_song_item, List); 
     recent.setAdapter(m_adapter); 
    } 
    } 

Data.java

public class Data { 
String name; 
public Data(String name) 
{ 
    this.name= name; 
} 
public String getName() { 
    return name; 
} 

public void setName(String name) { 
    this.name = name; 
} 

} 

MetadatArrayAdapter

public class MetadataArrayAdapter extends ArrayAdapter<Data> { 

    private ArrayList<Data> objects; 
    LayoutInflater inflater ; 
    public MetadataArrayAdapter(Context context, int textViewResourceId, ArrayList<Data> objects) { 
     super(context, textViewResourceId, objects); 
     this.objects = objects; 
     inflater = LayoutInflater.from(context); 
    } 
    public View getView(int position, View convertView, ViewGroup parent){ 

     ViewHolder holder; 
     if (convertView == null) { 
      convertView = inflater.inflate(R.layout.list_song_item, null); 
      holder = new ViewHolder(); 
      holder.tv= (TextView) convertView.findViewById(R.id.songTitlerecent); 
      convertView.setTag(holder); 
     } else { 
      holder = (ViewHolder) convertView.getTag(); 
     } 

     Data i = objects.get(position); 
     if (i != null) { 
     holder.tv.setText(i.getName()); 

     } 
     return convertView; 
    } 
    static class ViewHolder 
    { 
     TextView tv; 
    } 
    } 

的test.xml

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

android:orientation="vertical" > 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    > 

    <Button 
     android:id="@+id/back_btn2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_margin="10dp" 
     android:text="Back_Recent_Song" /> 
</LinearLayout> 

<ListView 
    android:id="@+id/lv" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    > 
</ListView> 
</LinearLayout> 

list_song_item

<?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" 
    android:gravity="center" 
    android:padding="5dp"> 
    <TextView 
    android:id="@+id/songTitlerecent" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:textSize="16dp" 
    android:padding="10dp" 
    android:color="#f3f3f3"/> 
</LinearLayout> 
+0

你能幫助嗎?http://stackoverflow.com/questions/28148618/listview-not-refreshing-after-click-on-button – 2015-01-27 13:19:10

相關問題