0

How Create a dynamical Item list Or item in another item list like as BEETALK Android Software


hello guys. i'm searching for how i can add description on a item of list onClick event. like as contact list beetalk.
i wanna click on item1 and see description1 and when click on item2 description1 hide and description2 show and the end if item1 click and desc1 showing close desc1.

step 0 :

  • item1
  • item2

step 1 :

  • item1
  • desc1
  • item2

step 2 :

  • item1
  • item2
  • desc2

step 3 :

  • item1
  • item2

do you have any idea!

H.Rafiee
  • 358
  • 1
  • 9

2 Answers2

0

Create one setSelected(int position) method in the your list adapter. and from your onlistitemclick call it passing the clicked position.

edit your adapter code as following.

 int selectedPosition = -1;
 public void setSelected(int position)
{
    selectedPosition= position;
    notifyDataSetChanged();
}

public View getView(int position, View convertView, ViewGroup parent)
{
    // your adapter code
    if(position==selectedPosition)
         description.setVisibility(View.VISIBLE);

    return convertView;
}
Pr38y
  • 1,565
  • 13
  • 21
  • there is any way to add item for description on listview dynamically! – H.Rafiee Jun 16 '14 at 06:30
  • Then you can try something like this mAdapterView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView> arg0, View arg1, int position, long arg3) { TextView description= new TextView(getActivity()); description.setText("description"); currentView = ((LinearLayout)arg0.getChildAt(position)); currentView.addView(description); } – Pr38y Jun 16 '14 at 06:56
  • But since you want to show only one description at a time in the listView, it will be tedious to remove view from previous adapterView and keep it for currently selected only. – Pr38y Jun 16 '14 at 06:57
0

You need to use ExpandableListView, example here.

  1. Define two XML layouts for Group view and Child view, add TextView to each layout for showing item text and description.
  2. In code, set Item text to group's TextView and Description to child TextView, all children views will be hidden initially.
  3. Use ExpandableListView's collapseGroup() and expandGroup() methods to collapse or expand the groups.

For example, to collapse expanded groups before showing Description, first define getGroupCount() method in your ListAdapter class:

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

then call this method to collapse previously expanded groups:

private void collapseAll() {
  int cnt = listAdapter.getGroupCount();
  for (int i = 0; i < cnt; i++){
     yourList.collapseGroup(i);
  }
}

EDIT:

You can use TagHandler class with examples here and here to show dynamic child list on child's TextView by using:

Html.fromHtml("<ul><li>Item 1</li>...</ul>", null, new MyTagHandler()))

For that you need to transform child list's item text to HTML text by iterating over your dynamic List/ArrayList and making/appending HTML tags with string data inside them.

Community
  • 1
  • 1
Software Engineer
  • 3,906
  • 1
  • 26
  • 35