0

我创建了一个自定义列表视图并通过解析链接来设置数据并将它们排序在列表中。现在,当我要单击并获取行的单个对象的值时,我无法获取单击行的对象。

public class MainActivity extends Activity implements OnChildClickListener,
        OnItemClickListener {
    private ExpandableListView mExpandableListView;
    private List<GroupEntity> mGroupCollection;
    String URL;
    ArrayList<EventParsingClass> EventObject_Collection = new ArrayList<EventParsingClass>();
    ArrayList<Date> DateArray = new ArrayList<Date>();

    ArrayList<ArrayList<EventParsingClass>> arrayOfEventDescription = new ArrayList<ArrayList<EventParsingClass>>();

    MyListAdapter adapter;

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

        URL = "http://..............";

        ParsingWithURL(URL);

    }

    private void ParsingWithURL(String uRL2) {
        // TODO Auto-generated method stub

        new JSONPARSINGFOREVENTSTREAM().execute(URL);

    }

    private class JSONPARSINGFOREVENTSTREAM extends
            AsyncTask<String, Void, String> {

        private final String TAG_ID = "id";
        private final String TAG_Title = "title";
        private final String TAG_Description = "description";
        private final String TAG_StartDate = "start_datetime";
        private final String TAG_EndDate = "end_datetime";
        private final String TAG_City = "place_city";
        private final String TAG_Club = "place_club";
        private final String TAG_AgeLimit = "event_agelimit";
        private static final String TAG_Event_streamable = "data";

        EventParsingClass EPC;
        JSONArray streamable = null;
        ProgressDialog pDialog;

        @SuppressLint("SimpleDateFormat")
        @Override
        protected String doInBackground(String... arg0) {
            // TODO Auto-generated method stub

            Log.d("************PARAMS", arg0[0]);

            JSONParser jparser = new JSONParser();
            JSONObject json = jparser.getJSONFromUrl(arg0[0]);

            try {
                streamable = json.getJSONArray(TAG_Event_streamable);

                for (int i = 0; i < streamable.length(); i++) {
                    EPC = new EventParsingClass();
                    JSONObject c = streamable.getJSONObject(i);
                    EPC.setId(c.getString(TAG_ID));
                    EPC.setPlace_city(c.getString(TAG_City));
                    EPC.setPlace_club(c.getString(TAG_Club));
                    EPC.setTitle(c.getString(TAG_Title));
                    EPC.setDescription(c.getString(TAG_Description));
                    EPC.setSratdate_time(c.getString(TAG_StartDate));
                    EPC.setEnddate_time(c.getString(TAG_EndDate));
                    EPC.setEvent_agelimit(c.getString(TAG_AgeLimit));

                    long difference = EPC.geEnddate_time_date().getTime()
                            - EPC.getSratdate_time_date().getTime();

                    int day_difference = (int) (difference / (1000 * 3600 * 24));

                    // Log.d("Difference", "" + day_difference);

                    if (day_difference == 0) {

                        AddDay(EPC.getSratdate_time_date());

                    } else {

                        if (DateArray.size() == 0) {

                            DateArray.add(EPC.getSratdate_time_date());

                            long startday = EPC.getSratdate_time_date()
                                    .getTime();

                            for (int k = 1; k <= day_difference; k++) {

                                long constructedday = startday
                                        + (1000 * 3600 * 24) * k;

                                Date Constructed_value = new Date(
                                        constructedday);

                                DateArray.add(Constructed_value);
                            }

                        } else {

                            AddDay(EPC.getSratdate_time_date());

                            long startday = EPC.getSratdate_time_date()
                                    .getTime();

                            for (int k = 1; k <= day_difference; k++) {

                                long constructedday = startday
                                        + (1000 * 3600 * 24) * k;

                                Date Constructed_value = new Date(
                                        constructedday);

                                AddDay(Constructed_value);
                            }

                        }

                    }
                    EventObject_Collection.add(EPC);

                }

            } catch (JSONException e) {

                e.printStackTrace();
            }

            return null;
        }

        private void AddDay(Date value) {
            // TODO Auto-generated method stub

            if (DateArray.size() == 0) {
                DateArray.add(value);
            } else {
                boolean b = true;
                for (Date s : DateArray) {
                    if (s.equals(value)) {

                        b = false;
                        break;
                    }
                }

                if (b) {
                    DateArray.add(value);
                }
            }

        }

        @Override
        protected void onPostExecute(String result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);

            Log.d("+++++++++++++++++++++++number of Items in List", ""
                    + DateArray.size());
            AddDetailedItemToListView();
            AddHeaderItemsToListView();
            pDialog.dismiss();

        }

        private void AddDetailedItemToListView() {
            // TODO Auto-generated method stub

            for (Date s : DateArray) {

                ArrayList<EventParsingClass> constructed_arrayfor_items = new ArrayList<EventParsingClass>();

                for (int g = 0; g < EventObject_Collection.size(); g++) {

                    EventParsingClass EVPC = EventObject_Collection.get(g);
                    long new_startdate = EVPC.getSratdate_time_date().getTime();
                    long new_endtdate = EVPC.geEnddate_time_date().getTime();
                    long date = s.getTime();

                    if (date >= new_startdate && date <= new_endtdate) {

                        Log.d("^^^^^^^^^^^ Value Of Date  ", "" + s);
                        Log.d("^^^^^^^^^^^ Value Of StartDay ",
                                "" + EVPC.getSratdate_time_date());
                        Log.d("^^^^^^^^^^^ Value Of EndDay ",
                                "" + EVPC.geEnddate_time_date());

                        constructed_arrayfor_items.add(EVPC);

                    }
                }
                arrayOfEventDescription.add(constructed_arrayfor_items);

                Log.d("^^^^^^^^^^^^^^^^^^^arrayOfEventDescription", ""
                        + arrayOfEventDescription);
            }

        }

        private void AddHeaderItemsToListView() {
            // TODO Auto-generated method stub

            ListView lv = (ListView) findViewById(R.id.list_evevnt);

            LayoutInflater i = LayoutInflater.from(MainActivity.this);

            List<Item> items = new ArrayList<Item>();
            int length_of_datearray = DateArray.size();

            Log.d("!!!!!!!!!!!!!!!", "" + DateArray.size());
            Log.d("EEEEEEEEEEEEEEEEEEEE", "" + arrayOfEventDescription.size());

            for (ArrayList<EventParsingClass> It : arrayOfEventDescription) {
                Log.d("", "" + It.size());

                for (EventParsingClass oETC : It) {
                    Log.d("*******" + oETC.getTitle(),
                            "" + oETC.getSratdate_time_date());
                }

            }

            for (int m = 0; m < length_of_datearray; m++) {

                String day_of_header = (String) android.text.format.DateFormat
                        .format("EEEE", DateArray.get(m));
                String month_of_header = (String) android.text.format.DateFormat
                        .format("MMM", DateArray.get(m));
                String date_of_header = (String) android.text.format.DateFormat
                        .format("dd", DateArray.get(m));

                String total_header = day_of_header + " " + month_of_header
                        + " " + date_of_header;

                items.add(new Header(i, "" + total_header));

                ArrayList<EventParsingClass> Arraylist_for_loop = arrayOfEventDescription
                        .get(m);

                for (int h = 0; h < Arraylist_for_loop.size(); h++) {

                    String description = Arraylist_for_loop.get(h).getId();
                    String title = Arraylist_for_loop.get(h).getTitle();

                    String place_city = Arraylist_for_loop.get(h)
                            .getPlace_city();

                    String age_limit = Arraylist_for_loop.get(h)
                            .getEvent_agelimit();

                    String dayOfTheWeek = (String) android.text.format.DateFormat
                            .format("EEEE", Arraylist_for_loop.get(h)
                                    .getSratdate_time_date());

                    String DayofWeek = dayOfTheWeek;

                    if (!(dayOfTheWeek == day_of_header)) {

                        DayofWeek = day_of_header;
                    }

                    SimpleDateFormat sdf = new SimpleDateFormat("EEEE");
                    Date d = new Date();
                    String Today = sdf.format(d);

                    String Value_of_today = "";

                    if (Today.contentEquals(DayofWeek)) {

                        Value_of_today = "Today";
                    }

                    items.add(new EventItem(i, Value_of_today, DayofWeek,
                            "12:00", title, description, place_city, "10",
                            age_limit));

                }

            }
            MyListAdapter adapter = new MyListAdapter(MainActivity.this, items);
            lv.setAdapter(adapter);
            lv.setOnItemClickListener(MainActivity.this);

        }

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();

            pDialog = new ProgressDialog(MainActivity.this);
            pDialog.setMessage("Loading...");
            pDialog.setCancelable(true);
            pDialog.show();

        }

    }

    private void prepareResource() {

        mGroupCollection = new ArrayList<GroupEntity>();

        for (int i = 1; i < 3; i++) {
            GroupEntity ge = new GroupEntity();
            ge.Name = "City " + i;

            for (int j = 1; j < 4; j++) {
                GroupItemEntity gi = ge.new GroupItemEntity();
                gi.Name = "Venu" + j;
                ge.GroupItemCollection.add(gi);
            }

            mGroupCollection.add(ge);
        }

    }

    private void initPage() {
        mExpandableListView = (ExpandableListView) findViewById(R.id.expandableListView);
        ExpandableListAdapter adapter = new ExpandableListAdapter(this,
                mExpandableListView, mGroupCollection);

        mExpandableListView.setAdapter(adapter);
        mExpandableListView.setOnChildClickListener(this);
    }

    @Override
    public boolean onChildClick(ExpandableListView parent, View v,
            int groupPosition, int childPosition, long id) {
        Toast.makeText(getApplicationContext(), childPosition + "Clicked",
                Toast.LENGTH_LONG).show();

        return true;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {


            EventParsingClass obj = (EventParsingClass) parent.getItemAtPosition(position);

    Toast.makeText(getApplicationContext(),  obj.getPlace_city() + "Clicked",Toast.LENGTH_LONG).show();

    }

}

在这两种情况下我该如何进行?

EventParsingClass EPSP= ???

EPSP.getid= ??

4

4 回答 4

1
fetch[0]="XXX"
fetch[1]="YYY"
fetch[2]="ZZZ"

lv.setOnItemClickListener(MainActivity.this);

   public void onItemClick(AdapterView<?> parent, View view, int position,long id)


    Toast.makeText(getApplicationContext(), fetch[position] + "Clicked",
            Toast.LENGTH_LONG).show();

}

只需声明fetch[position]以获取单击项目的值。希望这会给你一些解决方案。

于 2013-06-28T13:25:32.787 回答
0

我得到了解决方案:

EventParsingClass new_method(int value) {

int item_count = 0;

for (int i = 0; i < arrayOfEventDescription.size(); i++) {

    ArrayList<EventParsingClass> Arraylist_for_loop = arrayOfEventDescription
            .get(i);

    item_count++;
    for (int j = 0; j < Arraylist_for_loop.size(); j++) {

        if (value == item_count) {

            return Arraylist_for_loop.get(j);
        }

        item_count++;

    }

}

return null;

}

并从这里调用它:

@Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

        EventParsingClass newObject = new_method(arg2);

        if (newObject == null) {

        } else {
            Log.d("Generated Value Id : ", "" + newObject.getId());

            Toast.makeText(getApplicationContext(),
                    "Item Clicked" + arg2 + "-----" + newObject.getTitle(),
                    Toast.LENGTH_LONG).show();
        }
    }
于 2013-07-01T09:06:17.860 回答
0
EventItem item = (EventItem) parent.getItemAtPosition(position);

现在你掌握了EventItem. 所以你可以开始使用get你的类的方法EventItem来获得你想要的任何东西。

于 2013-06-28T13:20:26.263 回答
0

使用 int position 从您的数据列表(数组列表或您曾经使用过的)中找出值。

lv.setOnItemClickListener(MainActivity.this);

 public void onItemClick(AdapterView<?> parent, View view, int position,long id) 

 Toast.makeText(getApplicationContext(),  EPSP.getid(position) + "Clicked",Toast.LENGTH_LONG).show();

}
于 2013-06-28T13:29:28.730 回答