마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

리스트뷰 인텐트에 대해 재질문 드립니다 ㅠㅠ

0 추천
myList.setOnChildClickListener(new OnChildClickListener() {
			@Override
			public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition,
					long id) {

				if (aclass == "1") {
					Intent intent = new Intent(MainActivity.this, ai_ia.class);
					startActivity(intent);
				} else if (bclass == "2") {
					Intent intent = new Intent(MainActivity.this, ai_miku.class);
					startActivity(intent);
				} else if (cclass == "3") {
					Intent intent = new Intent(MainActivity.this, ai_nayuta.class);
					startActivity(intent);
				}

				return false;
			}
		});
	}

	private void displayList() {

		loadSomeData();

		myList = (ExpandableListView) findViewById(R.id.expandableListView1);
		listAdapter = new MyListAdapter(MainActivity.this, continentList);
		myList.setAdapter(listAdapter);

	}

	private void loadSomeData() {

		ArrayList<Country> ai2 = new ArrayList<Country>();
		Country ai_1 = new Country("1", "11", aclass = "1");
		Country ai_2 = new Country("2", "22", bclass = "2");
		Country ai_3 = new Country("3", "33", cclass = "3");
		ai2.add(ai_1);
		ai2.add(ai_2);
		ai2.add(ai_3);

		Continent c1 = new Continent(R.drawable.ai_1, "ai", "(Ai)", ai2);

		continentList.add(c1);
	}

 

 

group은 continentList child는 ai2 입니다.

ai_1, ai_2, ai_3이 정의될 때 값을 넣어봤는데 이렇게하면 모든 값이 정의되서 같은 변수를 쓰면 마지막에 정의한 값이 변수로 들어가더라고요. (aclass = 1, aclass = 2, aclass = 3 하면 3이 나옵니다)

지금 결과는 aclass = 1, bclass = 2, cclass = 3 이라 if 가장 처음에 있는 ai_ia.class로 인텐트를 하는데요 ㅠ

여기서 대체 뭘 어떻게 하면 child item(a1_1, a1_2, a1_3) 하나하나 마다 유니크한 값을 줘서 그 값을 통해 인텐트를 시킬 수 있을까요 ?

yu-yu (140 포인트) 님이 2015년 12월 5일 질문

2개의 답변

0 추천
눌린위치에 따라서 다른 값을 사용하고 싶으시면 ID를 사용하면 됩니다.

 

listview onchildclick 메소드의 id를 공부해보시기 바랍니다.
Autopro (1,780 포인트) 님이 2015년 12월 6일 답변
아무리 구글링해도 groupPosition 하고 childPosition 을 이용해서 값 받는것 밖에 찾을수가 없네요.
지금 이 문제동안 일주일동안 고생중인데 조금만 더 힌트를 주시면 안댈까요 ㅠㅠ
0 추천

굳이 유니크한 값을 주는것보다 클릭한 위치로 인텐트시키는게 나을듯싶네요.

리스트뷰가 유동적으로 바뀌어서 특정한 값으로 구별할려면 차라리 각 아이템마다 추가하는 순서대로 1,2,3,4 번호를 매긴 후  childPosition과 비교하는것이 나을것같네요.

기본적으로 아래코드처럼 해보시는건 어떠신지..
 

if ( childPosition == 1) {
                    Intent intent = new Intent(MainActivity.this, ai_ia.class);
                    startActivity(intent);
		}else if (childPosition == 2) {
                    Intent intent = new Intent(MainActivity.this, ai_miku.class);
                    startActivity(intent);
                } else if (childPosition == 3) {
                    Intent intent = new Intent(MainActivity.this, ai_nayuta.class);
                    startActivity(intent);
                }

이런식으로 포지션으로 비교하는게 나을듯 싶네요.

박스마트 (630 포인트) 님이 2015년 12월 7일 답변
...