簡體   English   中英

Listview上的Android子項

[英]Android Subitems on a Listview

我的問題:為什么在嘗試將適配器設置為subListView.setAdapter(adapter2)中的我的一個列表視圖時,出現java.lang.NullPointerException錯誤?

我正在嘗試創建一個用於學習目的的應用程序,該應用程序將在列表視圖中顯示主題,並且當單擊列表視圖上的項目/主題之一時,將出現一個子項目列表。

現在,我只是試圖顯示一個帶有其子項目的listview視圖。

這是我的代碼:

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

<TextView
    android:id="@+id/textview1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:minHeight="150dp"
    android:layout_alignLeft="@+id/button1"
    android:layout_alignRight="@+id/listView1"
    android:layout_below="@+id/button1"
    android:text="@string/hello_world" />

<ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textview1" >

</ListView>

</RelativeLayout>

group_item.xml

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

<TextView
    android:id="@+id/groupItem"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:text="Medium Text"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<ListView
    android:id="@+id/sublistView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/groupItem"
    android:layout_marginLeft="26dp" >
</ListView>

</RelativeLayout>

db_items.xml

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

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:adjustViewBounds="true"
    android:maxHeight="60dp"
    android:maxWidth="60dp"
    android:src="@drawable/ic_launcher" />

<TextView
    android:id="@+id/subSubjectTextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/imageView1"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@+id/imageView1"
    android:text="Jesus is God"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
    android:id="@+id/subIdtextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:text="Small Text"
    android:visibility="invisible"
    android:textAppearance="?android:attr/textAppearanceSmall" />

<TextView
    android:id="@+id/subScriptTextView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/textView1"
    android:text="John 1:1-12"
    android:textAppearance="?android:attr/textAppearanceSmall" />

</RelativeLayout>

MainActivity.java

public class MainActivity extends Activity {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    protected void onCreate(Bundle savedInstanceState) {
        //Creates DB and Tables if they do not exist... This part works
        popList();
    }

    public void popList(){
        ListView listView = (ListView) findViewById(R.id.listView1);
    ListView subListView = (ListView) findViewById(R.id.sublistView);
    TextView textv = (TextView)findViewById(R.id.textview1);
    String topList[] = {"First", "Second", "Third"};

        //Queries the DB and stores it in a Cursor... This part works

        textv.append(" Cursor Count = " + c.getCount()); //this is for debugging purposes

        int iId = c.getColumnIndex("id");
    int iSummary = c.getColumnIndex("Summary");
        int iScripts = c.getColumnIndex("Scripts");
    int iDescription = c.getColumnIndex("Description");
    int iSourceType = c.getColumnIndex("SourceType");

        cursor.moveToFirst();
        for (int j=0; j<c.getCount(); j++)
    {
        int image = 0;
        if(c.getString(iSourceType).equals("Bible"))
        {
            image = R.drawable.bible;
        }
        if(c.getString(iSourceType).equals("Article"))
        {
            image = R.drawable.article;
        }
        if(c.getString(iSourceType).equals("Video"))
        {
            image = R.drawable.video;
        }


        myLVItems.add(new lvItem(c.getString(iId), c.getString(iSummary), c.getString(iScripts), c.getString(iDescription), image));

        c.moveToNext();
    }
    ArrayAdapter<lvItem> adapter2 = new myListAdapter();

    textv.append(" subListView Count = " + myLVItems.size()+ " adapter2 Count: " + adapter2.getCount());   //this is for debugging purposes

    subListView.setAdapter(adapter2);  //this is first error that the logcat points to


    for (int i=0; i<topList.length; i++)
    {
        myGroupTopItems.add(new topgroupItem(topList[i], null));
    }

    ArrayAdapter<topgroupItem> adapter = new myTopListAdapter();

    listView.setAdapter(adapter);
    }

private class myListAdapter extends ArrayAdapter<lvItem>{
    public myListAdapter(){
        super(MainActivity.this, R.layout.db_items, myLVItems);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // make sure we have a view to work with (may have been given null
        View itemView = convertView;
        if(itemView == null)
        {
            itemView = getLayoutInflater().inflate(R.layout.db_items, parent, false);
        }

        //find the item to work with
        lvItem currentLVItem = myLVItems.get(position);

        //fill the view
        ImageView imageView = (ImageView) itemView.findViewById(R.id.imageView1);
        imageView.setImageResource(currentLVItem.getIconId());

        TextView hiddenView = (TextView) itemView.findViewById(R.id.subIdtextView);
        hiddenView.setText(currentLVItem.getId());

        TextView summaryView = (TextView) itemView.findViewById(R.id.subSubjectTextView);
        summaryView.setText(currentLVItem.getSummary());

        TextView descripView = (TextView) itemView.findViewById(R.id.subScriptTextView2);
        descripView.setText(currentLVItem.getScripts());

        return itemView;
    }

}

private class myTopListAdapter extends ArrayAdapter<topgroupItem>{

    public myTopListAdapter() {
        super(MainActivity.this, R.layout.group_item, myGroupTopItems);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View itemView = convertView;

        if(itemView == null)
        {
            itemView = getLayoutInflater().inflate(R.layout.group_item, parent, false);
        }

        topgroupItem currentTGItem = myGroupTopItems.get(position);

        TextView textView = (TextView) itemView.findViewById(R.id.groupItem);
        textView.setText(currentTGItem.getText());

        return itemView;
    }

}
}

這是textv顯示的內容:Cursir Count = 3 subListView Count = 3 adapter2 Count:3

我不確定為什么要為subListView獲取Null。 我不是在尋找正在執行的擴展列表視圖

對不起,所有代碼。 任何幫助將不勝感激

首先:在onCreate()調用popList() onCreate() 那是在List填充項目之前,因此沒有ID為sublistView子視圖。

第二:不要將列表添加到列表項中! 另一個ScrollView中沒有ScrollViews! 如果您想擁有子列表,則可能想查看ExpandableListView

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM