0

カスタムリストビューを作成する必要があります。このリスト ビューには、テキスト ボックスが含まれています。

リストビュー 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" />

listactivity を拡張せずに setlistadapter を使用するには?

コード :

            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);

メタアダプタ クラス:

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;
}

}

リストを見ることができません。ここに他に何を追加する必要がありますか?

Link を参照して: listactivity を拡張せずに setlistadapter() を使用する android

リストアクティビティを拡張せずに setAdapter を使用するには?

何が悪いのか教えてください。

前もって感謝します

4

1 に答える 1