Android - 可扩展列表视图 - 子数据

时间:2016-10-31 11:24:07

标签: java android xml android-layout expandablelistview

我有一个用android编写的小应用程序。我试图在我的主布局中实现ExpandableListView,但子数据不存在。折叠工作正常,但没有提供数据。

我运行了调试器并看到我的列表中有数据。

这是我的代码:

主要布局 - activity_post_details.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_post_details"
    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="hedza.com.durmitortourism.PostDetailsActivity">


    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true">

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

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_alignParentBottom="true"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:gravity="center"
                android:id="@+id/postDetailsTitleText"
                android:textSize="32dp"
                android:textStyle="bold"
                android:textAlignment="center" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_marginLeft="10dp"
                android:layout_marginStart="10dp"
                android:textSize="16dp"
                android:layout_marginTop="18dp"
                android:id="@+id/postDescText" />

            <ExpandableListView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginStart="10dp"
                android:id="@+id/ExplistDetails"
                />

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

                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:id="@+id/postGalleryBtn"
                    android:layout_alignParentTop="true"
                    android:layout_alignParentLeft="true"
                    android:layout_marginTop="20dp"
                    />

                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:id="@+id/postLocationBtn"
                    android:layout_alignParentTop="true"
                    android:layout_alignParentRight="true"
                    android:layout_alignParentEnd="true"
                    android:layout_marginTop="20dp"
                    />

            </RelativeLayout>

        </LinearLayout>
    </ScrollView>

</RelativeLayout>  

group_item - expandable_list_group.xml

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

    <TextView
        android:id="@+id/listTitle"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
        android:textColor="@android:color/black"
        android:paddingTop="10dp"
        android:paddingBottom="10dp" />

</LinearLayout>

expandable_list_item.xml

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

    <TextView
        android:id="@+id/expandedListItem"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft"
        android:paddingTop="10dp"
        android:textColor="#000000"
        android:textSize="16dp"
        android:paddingBottom="10dp" />

</LinearLayout>

ExpandableListAdapter.class

import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import java.util.HashMap;
import java.util.List;

public class ExpandableListAdapter extends BaseExpandableListAdapter {

private Context context;
private List<String> expandableListTitle;
private HashMap<String, List<String>> expandableListDetail;

public ExpandableListAdapter(Context context, List<String> expandableListTitle,
                             HashMap<String, List<String>> expandableListDetail)
{
    this.context = context;
    this.expandableListTitle = expandableListTitle;
    this.expandableListDetail = expandableListDetail;
}

@Override
public Object getChild(int listPosition, int expandedListPosition)
{
    return this.expandableListDetail.get(this.expandableListTitle.get(listPosition))
            .get(expandedListPosition);
}

@Override
public long getChildId(int listPosition, int expandedListPosition) {
    return expandedListPosition;
}

@Override
public View getChildView(int listPosition, final int expandedListPosition,
                         boolean isLastChild, View convertView, ViewGroup parent)
{
    final String expandedListText = (String) getChild(listPosition, expandedListPosition);
    if (convertView == null)
    {
        LayoutInflater layoutInflater = (LayoutInflater) this.context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        convertView = layoutInflater.inflate(R.layout.expandable_list_item, null);
    }

    TextView expandedListTextView = (TextView) convertView
            .findViewById(R.id.expandedListItem);
    expandedListTextView.setText(expandedListText);

    return convertView;
}

@Override
public int getChildrenCount(int listPosition) {
    return this.expandableListDetail.get(this.expandableListTitle.get(listPosition))
            .size();
}

@Override
public Object getGroup(int listPosition) {
    return this.expandableListTitle.get(listPosition);
}

@Override
public int getGroupCount() {
    return this.expandableListTitle.size();
}

@Override
public long getGroupId(int listPosition) {
    return listPosition;
}

@Override
public View getGroupView(int listPosition, boolean isExpanded,
                         View convertView, ViewGroup parent)
{
    String listTitle = (String) getGroup(listPosition);

    if (convertView == null)
    {
        LayoutInflater layoutInflater = (LayoutInflater) this.context.
                getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        convertView = layoutInflater.inflate(R.layout.expandable_list_group, null);
    }

    TextView listTitleTextView = (TextView) convertView
            .findViewById(R.id.listTitle);

    listTitleTextView.setTypeface(null, Typeface.BOLD);
    listTitleTextView.setText(listTitle);

    return convertView;
}

@Override
public boolean hasStableIds() {
    return false;
}

@Override
public boolean isChildSelectable(int listPosition, int expandedListPosition) {
    return true;
}

}

PostDetailsActivity.class

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class PostDetailsActivity extends AppCompatActivity {

    SharedPreferences languageSharedPreferences;

    private TextView postTitleTextView;
    private TextView postDescTextView;

    private ExpandableListView expandableListView;
    private ExpandableListAdapter expandableListAdapter;

    private List<String> detailsList;
    private HashMap<String, List<String>> expandableListDetail;

    private Button galleryBtn;
    private Button locationBtn;

    String locationJSON = null;
    String title = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_post_details);

        // setting icon in the action bar
        getSupportActionBar().setDisplayShowTitleEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        getSupportActionBar().setIcon(R.mipmap.ic_launcher);

        // get postID from post activity bundle
        Intent intent = getIntent();
        String postID = intent.getStringExtra("postID");

        // getting language from shared preferences
        languageSharedPreferences = getSharedPreferences("LanguagePreferences",
                Context.MODE_PRIVATE);
        String language = languageSharedPreferences.getString("lang", "me");

        final ArrayList<String> viewPagerImages = new ArrayList<>();

        // expandable list details initialization
        detailsList = new ArrayList<String>();
        expandableListDetail = new HashMap<String, List<String>>();

        // initialize elements
        postTitleTextView = (TextView) findViewById(R.id.postDetailsTitleText);
        postDescTextView = (TextView) findViewById(R.id.postDescText);
        expandableListView = (ExpandableListView) findViewById(R.id.ExplistDetails);

        galleryBtn = (Button) findViewById(R.id.postGalleryBtn);
        locationBtn = (Button) findViewById(R.id.postLocationBtn);

        // get data from DB by post id
        final PostSQLiteManager postObject = new PostSQLiteManager(getApplicationContext());
        postObject.openConnection();

        JSONObject posts = postObject.getPostsById(postID);
        postObject.close();

        try
        {
            JSONArray array = posts.getJSONArray("DATA");
            JSONObject arrayObject = array.getJSONObject(0);

            String[] images = arrayObject.getString("IMAGES").split(",");
            for(int i = 0; i < images.length; i++){
                viewPagerImages.add(images[i]);
            }

            title = arrayObject.getString("TITLE");

            // set properties values
            postTitleTextView.setText(arrayObject.getString("TITLE"));
            postDescTextView.setText(arrayObject.getString("DESC"));

            String postWebsite = arrayObject.getString("WEBSITE");
            String postEmail = arrayObject.getString("EMAIL");
            String postTelephone = arrayObject.getString("TELEPHONE");

            locationJSON = arrayObject.getString("LOCATION");

            // handling languages
            if(language.contentEquals("me"))
            {
                detailsList.add("Web Stranica: "+postWebsite);
                detailsList.add("Telefon: "+postTelephone);
                detailsList.add("E-pošta: "+postEmail);

                expandableListDetail.put("Detalji", detailsList);

                galleryBtn.setText(R.string.galleryPostME);
                locationBtn.setText(R.string.locationPostME);
            }
            else
            {
                detailsList.add("Website: "+postWebsite);
                detailsList.add("Telephone: "+postTelephone);
                detailsList.add("E-mail: "+postEmail);

                expandableListDetail.put("Details", detailsList);

                galleryBtn.setText(R.string.galleryPostEN);
                locationBtn.setText(R.string.locationPostEN);
            }

            final List<String> listTitles = new ArrayList<>(expandableListDetail.keySet());

            // generate list
            expandableListAdapter = new ExpandableListAdapter(this, listTitles,
                    expandableListDetail);
            expandableListView.setAdapter(expandableListAdapter);

            // click on gallery button
            galleryBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    if (!viewPagerImages.isEmpty()){
                        Intent galleryIntent = new Intent(getApplicationContext(), GalleryActivity.class);
                        galleryIntent.putStringArrayListExtra("images", viewPagerImages);
                        startActivity(galleryIntent);
                    }else{
                        Toast.makeText(getApplicationContext(), "Nema slika", Toast.LENGTH_SHORT)
                                .show();
                    }
                }
            });

            // click on location button
            locationBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent locationIntent = new Intent(getApplicationContext(), MapActivity.class);
                    locationIntent.putExtra("location", locationJSON);
                    locationIntent.putExtra("title", title);
                    startActivity(locationIntent);
                }
            });

            expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {

                @Override
                public void onGroupExpand(int groupPosition) {
                    Toast.makeText(getApplicationContext(),
                            listTitles.get(groupPosition) + " List Expanded.",
                            Toast.LENGTH_SHORT).show();
                }
            });

        }
        catch(JSONException exception) {
            exception.printStackTrace();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(android.view.Menu menu) {
        super.onCreateOptionsMenu(menu);

        languageSharedPreferences = getSharedPreferences("LanguagePreferences",
                Context.MODE_PRIVATE);
        String language = languageSharedPreferences.getString("lang", "me");

        // generate menu and its items
        MenuInflater myMenu = getMenuInflater();
        myMenu.inflate(R.menu.menu_layout, menu);

        // change menu items depending on selected language
        switch(language)
        {
            case "me":
                menu.findItem(R.id.languageID).setTitle(R.string.languageME);
                menu.findItem(R.id.aboutID).setTitle(R.string.aboutME);
                menu.findItem(R.id.sponsorID).setTitle(R.string.sponsorME);
                break;

            case "en":
                menu.findItem(R.id.languageID).setTitle(R.string.languageEN);
                menu.findItem(R.id.aboutID).setTitle(R.string.aboutEN);
                menu.findItem(R.id.sponsorID).setTitle(R.string.sponsorEN);
                break;

            default:
                menu.findItem(R.id.languageID).setTitle(R.string.languageME);
                menu.findItem(R.id.aboutID).setTitle(R.string.aboutME);
                menu.findItem(R.id.sponsorID).setTitle(R.string.sponsorME);
        }

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        super.onOptionsItemSelected(item);

        switch (item.getItemId()) {
            case R.id.languageID:
                Intent languageIntent = new Intent(getApplicationContext(), LanguageActivity.class);
                startActivity(languageIntent);
                return true;

            case R.id.aboutID:
                Intent aboutIntent = new Intent(getApplicationContext(), AboutActivity.class);
                startActivity(aboutIntent);
                return true;

            case R.id.sponsorID:
                Intent sponsorIntent = new Intent(getApplicationContext(), SponsorActivity.class);
                startActivity(sponsorIntent);

            default:
                return true;
        }
    }
}

有没有人知道这里有什么问题?
提前谢谢。

0 个答案:

没有答案