2011-11-17 4 views
0

첫 번째 탭에서 나는 문자열을 추출하는 위치에서 listview를 가지며 두 번째 탭에서는 textview를가집니다. 목록에서 항목을 클릭하면 두 번째 탭으로 이동해야하며 문자열 값은 텍스트보기에 표시되어야합니다.동적으로 탭으로 이동하고 동시에 내용을 변경합니까?

public static String mystring; 
protected void onListItemClick (ListView l, View v, int position, long id){ 
    super.onListItemClick(l, v, position, id); 
    mystring = listofHashMap.get(position).get("keyname").toString(); //it's ok here 
    Intent intent=new Intent(this, MyActivity.class); 
    startActivity(intent); 
} 

위 (ListViewActivity에 위치) 코드는 내가 시도 무엇이며, (, 그것은 전체 화면 독립 활동이다하지만 MyActivity이 더 이상 tabcontent되지 않습니다) 다음 코드에서를 가진 그것은 작동

public MyTabActivity taba = new MyTabActivity(); 
protected void onListItemClick (ListView l, View v, int position, long id){ 
    super.onListItemClick(l, v, position, id); 
    mystring = listofHashMap.get(position).get("keyname").toString(); 
    int i=1;//the tab where I want to go, and MyActivity is its content 
    taba.ChangeTab(i); 
} 
: 나는 MyActivity이 tabcontent 할 것을 원하므로 내가 이것을 시도 말했듯이

tv = (TextView)findViewById(R.id.tvid); 
lv = new ListViewActivity(); 
tv.setText(lv.mystring); 

하지만, : MyActivity3210

ChangeTab()는 단순히setCurrentTab (I) 않는 MyTabActivity (TabActivity 연장 활성) 내의 정적 방법이다.

그래서,이 일을, MyActivity 항상 심지어 나는 내가해야 할 것은 MyActivity 다시 의 setContent() 것을 보라 좋구나 목록에서 다른 항목을 선택, 먼저 클릭 된 항목의 이름을 표시합니다 또는 무언가를 정적 문자열 mystring, 나는 많은 해결책을 시도했지만 결과가 없습니다. 나는 내가하고 싶은 것에 할 수있는만큼 정확하게하려고 노력했다.이 문제에 대한 약간의 도움을 부탁한다.

답변

0

내 제안은 브로드 캐스트를 사용하는 것입니다. 나는 타이밍이 끈적 거리는 방송을 요구할 것이라고 생각한다. 이 같은 아마 뭔가 :

public MyTabActivity taba = new MyTabActivity(); 
protected void onListItemClick (ListView l, View v, int position, long id){ 
    super.onListItemClick(l, v, position, id); 
    mystring = listofHashMap.get(position).get("keyname").toString(); 

    // create the intent to send and give it the data 
    Intent i = new Intent("updateTextView"); 
    i.putExtra("textFromList", mystring); 
    sendStickyBroadcast(i); 

    int i=1;//the tab where I want to go, and MyActivity is its content 
    taba.ChangeTab(i); 
} 

그리고 MyActivity에서 작성 뭔가 같은 : 끈적 끈적한 방송

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    // your onCreate method 
    registerReceiver(updateTextReceiver, new IntentFilter("updateTextView")); 
} 

protected void onDestroy() { 
    super.onDestroy(); 
    // your onDestroy method 
    unregisterReceiver(updateTextReceiver); 
} 

private BroadcastReceiver updateTextReceiver = new BroadcastReceiver() { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     tv = (TextView)findViewById(R.id.tvid); 
     String myString = intent.getStringExtra("textFromList"); 
     if (myString != null) { 
      tv.setText(myString); 
     } 
    } 

}; 

는 매니페스트에 필요한 추가 권한이있다. 확실하지는 않지만 브로드 캐스트를 전송하고 탭을 변경하는 순서를 바꾸면 일반 방송을 보낼 수 있습니다.

+0

우수! 나는 감명 받았다. 필요한 권한은 다음과 같다 : . 이것은 매우 간단하고 우아한 해결책이다. 추신 : 나는 TabContentFactory() 및 createTabContent()를 사용하여 문제를 해결하려고 노력 중이었고 앞으로 해결 방법이 될 수 있다면이 방법으로 어떻게 할 수 있을지 알고 싶습니다. – AlexAndro

+0

그 해결책을 몰랐습니다. 나는 그 메소드가 뷰를 반환하고 그것이 MyActivity가 시작되고 싶다는 것을 이해하고 있기 때문에 그것이 당신에게 도움이 될지 확신 할 수 없다. –

관련 문제