2013-08-19 20 views
1

我想用標籤欄做一個活動,並且我有5個標籤菜單。 MainActivity包含像這樣的Tabhost;無法設置OnListItemClick點擊標籤活動

public class MainActivity extends TabActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    setContentView(R.layout.activity_main); 

    String tab_title[] = { "Tab 1", "Tab 2", "Tab 3", "Tab 4", "Tab 5" }; 

    int tab_drawables[] = { R.drawable.menu_bookmark, 
      R.drawable.menu_filemanager, R.drawable.menu_download, 
      R.drawable.menu_sharepage, R.drawable.menu_about }; 

    Object tab_act[] = { Tab1.class, Tab2.class, Tab3.class, Tab4.class, Tab5.class }; 

    final TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost); 
    tabHost.setup(); 

    TabSpec tab_spec; 

    for (int i = 0; i < tab_act.length; i++) { 
     tab_spec = tabHost.newTabSpec(tab_title[i]); 
     tab_spec.setIndicator(tab_title[i], 
       getResources().getDrawable(tab_drawables[i])); 
     tab_spec.setContent(new Intent(this, (Class<?>) tab_act[i])); 
     tabHost.addTab(tab_spec); 
    } 
    tabHost.setCurrentTab(0); 

} 

@Override 
public boolean onKeyDown(int keyCode, KeyEvent event) { 
    if (keyCode == KeyEvent.KEYCODE_BACK) { 
     MainActivity.this.finish(); 
     return true; 
    } 
    return super.onKeyDown(keyCode, event); 
} 

}

當我點擊TAB2,Tab2.java類將被命名。這個類有一個列表視圖。當我點擊列表項目時,我想在YouTube上播放音樂。我的代碼正在工作,但OnListItemClick方法不起作用。

public class Tab2 extends Activity{ 

private LayoutInflater mInflater; 
private Vector<RowData> data; 
RowData rd; 
Context context ; 
Bitmap bitmap; 

public static String title[] = { "song 1", "song 2", "song 3"}; 

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

    mInflater = (LayoutInflater) getSystemService(Activity.LAYOUT_INFLATER_SERVICE); 
    data = new Vector<RowData>(); 
    for (int i = 0; i < title.length; i++) { 
     try { 
       rd = new RowData(i, title[i]); 
     } catch (ParseException e) { 
      e.printStackTrace(); 
     } 
     data.add(rd); 
    } 
    CustomAdapter adapter = new CustomAdapter(this, 
      R.layout.activity_list_view, R.id.title, data); 
} 

private class RowData { 
    protected int mId; 
    protected String mTitle; 

    RowData(int id, String title) { 
     mId = id; 
     mTitle = title; 
    } 

    @Override 
    public String toString() { 
     return mId + " " + mTitle; 
    } 
} 

public void onListItemClick(ListView parent, View v, int position, long id) { 
    if (position == 0) { 
     startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.youtube.com/watch?v=QK8mJJJvaes"))); 
    } 
    else if (position == 1) { 
     startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.youtube.com/watch?v=HWyEEj2pSt0"))); 
    } 
    else if (position == 2){ 
     startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.youtube.com/watch?v=HWyEEj2pSt0"))); 
    } 
} 

private class CustomAdapter extends ArrayAdapter<RowData> { 
    public CustomAdapter(Context context, int resource, 
      int textViewResourceId, List<RowData> objects) { 
     super(context, resource, textViewResourceId, objects); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     ViewHolder holder = null; 
     TextView title = null; 
     RowData rowData = getItem(position); 
     if (null == convertView) { 
      convertView = mInflater 
        .inflate(R.layout.activity_list_view, null); 
      holder = new ViewHolder(convertView); 
      convertView.setTag(holder); 
     } 
     holder = (ViewHolder) convertView.getTag(); 
     title = holder.gettitle(); 
     title.setText(rowData.mTitle); 

     return convertView; 
    } 

    private class ViewHolder { 
     private View mRow; 
     private TextView title = null; 
     private ImageView i11 = null; 

     public ViewHolder(View row) { 
      mRow = row; 
     } 

     public TextView gettitle() { 
      if (null == title) { 
       title = (TextView) mRow.findViewById(R.id.title); 
      } 
      return title; 
     } 

    } 
} 

}

我與佈局沒有問題,我肯定有關所以沒有必要顯示我的XML文件。 :)

更新

更改與以下代碼TAB2類;

public class Tab2 extends Activity implements AdapterView.OnItemClickListener{ 

private LayoutInflater mInflater; 
private Vector<RowData> data; 
RowData rd; 
Context context ; 
Bitmap bitmap; 

public static String title[] = { "song 1", "song 2", "song 3"}; 

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

    mInflater = (LayoutInflater) getSystemService(Activity.LAYOUT_INFLATER_SERVICE); 
    data = new Vector<RowData>(); 
    for (int i = 0; i < title.length; i++) { 
     try { 
       rd = new RowData(i, title[i]); 
     } catch (ParseException e) { 
      e.printStackTrace(); 
     } 
     data.add(rd); 
    } 
    CustomAdapter adapter = new CustomAdapter(this, 
      R.layout.activity_list_view, R.id.title, data); 

    ListView list = (ListView) findViewById(R.id.list); 
    list.setAdapter(adapter); 
    list.setOnItemClickListener(this); 
} 

private class RowData { 
    protected int mId; 
    protected String mTitle; 

    RowData(int id, String title) { 
     mId = id; 
     mTitle = title; 
    } 

    @Override 
    public String toString() { 
     return mId + " " + mTitle; 
    } 
} 


private class CustomAdapter extends ArrayAdapter<RowData> { 
    public CustomAdapter(Context context, int resource, 
      int textViewResourceId, List<RowData> objects) { 
     super(context, resource, textViewResourceId, objects); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     ViewHolder holder = null; 
     TextView title = null; 
     RowData rowData = getItem(position); 
     if (null == convertView) { 
      convertView = mInflater 
        .inflate(R.layout.activity_list_view, null); 
      holder = new ViewHolder(convertView); 
      convertView.setTag(holder); 
     } 
     holder = (ViewHolder) convertView.getTag(); 
     title = holder.gettitle(); 
     title.setText(rowData.mTitle); 

     return convertView; 
    } 

    private class ViewHolder { 
     private View mRow; 
     private TextView title = null; 
     private ImageView i11 = null; 

     public ViewHolder(View row) { 
      mRow = row; 
     } 

     public TextView gettitle() { 
      if (null == title) { 
       title = (TextView) mRow.findViewById(R.id.title); 
      } 
      return title; 
     } 

    } 
} 

@Override 
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
    // TODO Auto-generated method stub 
    if (position == 0) { 
     startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.youtube.com/watch?v=4jHrdLUFA-8"))); 
    } 
    else if (position == 1) { 
     startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.youtube.com/watch?v=-HMfKUtYHog"))); 
    } 
    else if (position == 2){ 
     startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.youtube.com/watch?v=7beBw8uQOR8"))); 
    } 
} 

}

+0

你有什麼問題?請閱讀'LogCat' /'Stack Traces' /'etc'。 –

+0

顯示所有列表項,但OnListItemClick方法不起作用。 –

回答

1

公共類TAB2擴展活動

Tab2延伸活動,不ListActivity

得到這個工作,找到ListView小部件R.layout.tab2

super.onCreate(savedInstanceState); 
setContentView(R.layout.tab2);  

mInflater = (LayoutInflater) getSystemService(Activity.LAYOUT_INFLATER_SERVICE); 

data = new Vector<RowData>(); 
for (int i = 0; i < title.length; i++) { 
    try { 
      rd = new RowData(i, title[i]); 
    } catch (ParseException e) { 
     e.printStackTrace(); 
    } 
    data.add(rd); 
} 

CustomAdapter adapter = new CustomAdapter(this, 
     R.layout.activity_list_view, R.id.title, data); 

ListView lv = (ListView) findViewById(R.id.your_listiew_id); 
lv.setAdapter(adapter); 

lv.setOnItemClickListener(this); 

類定義更改爲:

public class Tab2 extends Activity implements AdapterView.OnItemClickListener { 

並更改onListItemClick(ListView parent, View v, int position, long id)到:

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

的問題你的代碼是你永遠不會分配OnItemClickListenerListView。如果你想使用onListItemClick (ListView l, View v, int position, long id),那麼Tab2應該延伸ListActivity,而不是Activity。查看原因:Link

+0

我從tab2.xml中添加了我的ListView小部件,但是我無法運行該方法。您的解決方案不適合我:( –

+0

@Droid在上述問題中發佈您的更新代碼 – Vikram

+0

我更改了@vikram,您可以看到我遵循了您的建議 –

相關問題