如何在expandablelistview中获取onChit的特定对象?

时间:2016-09-07 17:50:58

标签: java android expandablelistview

我的Android应用程序(Java)中存在以下问题:

expandablelistview中的每个子代表一个名为Entry的对象,但只有标题(子)和日期(标题)存储在expandablelistview中。 单击子项时,应启动另一个活动,我可以在其中编辑Entry的值。如果我想完成这项专长,除了新活动之外,我还需要特定的条目。

这个宏伟的计划是,每个对象都是“待办事项”。每个任务都有一个日期,标题,优先级等。任务显示在expandablelistview中,如果你点击一个日期,每个任务标题应该显示该特定日期,如果你点击标题,另一个屏幕应该出现在哪里你可以编辑任务。

无论如何这有可能吗?

任何帮助都将不胜感激。

编辑:

这是我的expandableListAdapter:

ExpandableListAdapter.java
package info.androidhive.expandablelistview;

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

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;

public class ExpandableListAdapter extends BaseExpandableListAdapter {

private Context _context;
private List<String> _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap<String, List<String>> _listDataChild;

public ExpandableListAdapter(Context context, List<String> listDataHeader,
        HashMap<String, List<String>> listChildData) {
    this._context = context;
    this._listDataHeader = listDataHeader;
    this._listDataChild = listChildData;
}

@Override
public Object getChild(int groupPosition, int childPosititon) {
    return this._listDataChild.get(this._listDataHeader.get(groupPosition))
            .get(childPosititon);
}

@Override
public long getChildId(int groupPosition, int childPosition) {
    return childPosition;
}

@Override
public View getChildView(int groupPosition, final int childPosition,
        boolean isLastChild, View convertView, ViewGroup parent) {

    final String childText = (String) getChild(groupPosition, childPosition);

    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) this._context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.list_item, null);
    }

    TextView txtListChild = (TextView) convertView
            .findViewById(R.id.lblListItem);

    txtListChild.setText(childText);
    return convertView;
}

@Override
public int getChildrenCount(int groupPosition) {
    return this._listDataChild.get(this._listDataHeader.get(groupPosition))
            .size();
}

@Override
public Object getGroup(int groupPosition) {
    return this._listDataHeader.get(groupPosition);
}

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

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

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
        View convertView, ViewGroup parent) {
    String headerTitle = (String) getGroup(groupPosition);
    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) this._context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.list_group, null);
    }

    TextView lblListHeader = (TextView) convertView
            .findViewById(R.id.lblListHeader);
    lblListHeader.setTypeface(null, Typeface.BOLD);
    lblListHeader.setText(headerTitle);

    return convertView;
}

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

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
    return true;
}
}

这是我的主要活动:

package hartmann.todo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListView;

import com.google.gson.Gson;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;

public class MainActivity extends Activity {

ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
ListOfEntries listOfEntries;
Gson gson;
int id;
DateFormat dateFormat;
Calendar cal;
Entry entry;
List<String> day0, day1, day2, day3, day4, day5, day6, rest;

@Override

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    listOfEntries = new ListOfEntries();
    cal = Calendar.getInstance();
    dateFormat = new SimpleDateFormat("dd.MM.yyyy");
    listDataHeader = new ArrayList<>();
    listDataChild = new HashMap<>();
    id = 0;
    entry = new Entry();
    gson = new Gson();
    day0 = new ArrayList<>();
    day1 = new ArrayList<>();
    day2 = new ArrayList<>();
    day3 = new ArrayList<>();
    day4 = new ArrayList<>();
    day5 = new ArrayList<>();
    day6 = new ArrayList<>();
    rest = new ArrayList<>();


    // get the listview
    expListView = (ExpandableListView) findViewById(R.id.lvExp);

    // preparing list data
    listDataHeader.add(dateFormat.format(cal.getTime()));
    for (int m = 0; m < 6; m++) {
        cal.add(Calendar.DATE, 1);
        listDataHeader.add(dateFormat.format(cal.getTime()));
    }
    listDataHeader.add("Später");

    listDataChild.put(listDataHeader.get(0), day0);
    listDataChild.put(listDataHeader.get(1), day1);
    listDataChild.put(listDataHeader.get(2), day2);
    listDataChild.put(listDataHeader.get(3), day3);
    listDataChild.put(listDataHeader.get(4), day4);
    listDataChild.put(listDataHeader.get(5), day5);
    listDataChild.put(listDataHeader.get(6), day6);
    listDataChild.put(listDataHeader.get(7), rest);

    listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);

    // setting list adapter
    expListView.setAdapter(listAdapter);
}

//  Preparing the list data

private void prepareListData(Entry entry) {


        Calendar cal2 = Calendar.getInstance();

        cal2.clear(Calendar.HOUR);
        cal2.clear(Calendar.MINUTE);
        cal2.clear(Calendar.SECOND);
        cal2.clear(Calendar.MILLISECOND);


        if (entry.getDate().equals(cal2)) {
            day0.add(entry.getTitle());
        }
        cal2.add(Calendar.DATE, 1);
        if (entry.getDate().equals(cal2)) {
            day1.add(entry.getTitle());

        }
        cal2.add(Calendar.DATE, 1);
        if (entry.getDate().equals(cal2)) {
            day2.add(entry.getTitle());

        }
        cal2.add(Calendar.DATE, 1);
        if (entry.getDate().equals(cal2)) {
            day3.add(entry.getTitle());

        }
        cal2.add(Calendar.DATE, 1);
        if (entry.getDate().equals(cal2)) {
            day4.add(entry.getTitle());

        }
        cal2.add(Calendar.DATE, 1);
        if (entry.getDate().equals(cal2)) {
            day5.add(entry.getTitle());

        }
        cal2.add(Calendar.DATE, 1);

        if (entry.getDate().equals(cal2)) {
            day6.add(entry.getTitle());

        }
        if (entry.getDate().after(cal2)) {
            rest.add(entry.getTitle());
        }


    listDataChild.put(listDataHeader.get(0), day0);
    listDataChild.put(listDataHeader.get(1), day1);
    listDataChild.put(listDataHeader.get(2), day2);
    listDataChild.put(listDataHeader.get(3), day3);
    listDataChild.put(listDataHeader.get(4), day4);
    listDataChild.put(listDataHeader.get(5), day5);
    listDataChild.put(listDataHeader.get(6), day6);
    listDataChild.put(listDataHeader.get(7), rest);

    listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);

    // setting list adapter
    expListView.setAdapter(listAdapter);
}

public void newTask(View view) {
    Intent intent = new Intent(MainActivity.this, EditEntry.class);
    startActivityForResult(intent, 1);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == 1) {
        if (resultCode == Activity.RESULT_OK) {
            String strObj = data.getStringExtra("entry");
            entry = gson.fromJson(strObj, Entry.class);
            entry.setEntryId(id);
            id++;
            listOfEntries.addEntry(entry);
            prepareListData(entry);
        }
        if (resultCode == Activity.RESULT_CANCELED) {
            //Write your code if there's no result
        }
    }
}//onActivityResult

}

0 个答案:

没有答案