2013-10-28 86 views
-1

在我的程序中,我顯示了多級Listview,但是每當我打電話給另一個Activity時,Fragment Tabs都不會出現。沒有出現片段選項卡

我使用這個偉大的教程:http://www.androidbegin.com/tutorial/android-actionbarsherlock-viewpager-tabs-tutorial/

參閱下圖,第1層的ListView:

enter image description here

二級的ListView:

enter image description here

ListCategoryFragment.xml: -

public class ListCategoryFragment extends SherlockFragment implements OnItemClickListener { 

    ListView lview3; 
    ListCategoryAdapter adapter; 

    private ArrayList<Object> itemList; 
    private ItemBean bean; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
     // Get the view from fragmenttab1.xml 
     View view = inflater.inflate(R.layout.fragment_category_tab, container, false); 
     prepareArrayList(); 
     lview3 = (ListView) view.findViewById(R.id.listView1); 
     adapter = new ListCategoryAdapter(getActivity(), itemList); 
     lview3.setAdapter(adapter); 
     lview3.setOnItemClickListener(this); 

    return view; 
} 

private static final int categoryFirst = 0; 
private static final int categorySecond = 1; 
private static final int categoryThird = 2; 

public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id) { 

    // Set up different intents based on the item clicked: 
    switch (position) 
    { 
     case categoryFirst: 
     Intent intent1 = new Intent(getActivity(), ListItemActivity.class); 
     intent1.putExtra("category", "Category - 1"); 
     startActivity(intent1);  
     break; 

     case categorySecond: 
     Intent intent2 = new Intent(getActivity(), ListItemActivity.class); 
     intent2.putExtra("category", "Category - 2"); 
     startActivity(intent2);  
     break; 

     case categoryThird: 
     Intent intent3 = new Intent(getActivity(), ListItemActivity.class); 
     intent3.putExtra("category", "Category - 3"); 
     startActivity(intent3);  
     break; 

     default: 
     break; 
    } 
} 

public void prepareArrayList() 
{ 
    itemList = new ArrayList<Object>(); 

    AddObjectToList("Category - 1"); 
    AddObjectToList("Category - 2"); 
    AddObjectToList("Category - 3"); 
} 

// Add one item into the Array List 
public void AddObjectToList(String title) 
{ 
    bean = new ItemBean(); 
    bean.setTitle(title); 
    itemList.add(bean); 
} 

@Override 
public void onSaveInstanceState(Bundle outState) { 
    super.onSaveInstanceState(outState); 
    setUserVisibleHint(true); 
} 

}

ListItemActivity.java:-

public class ListItemActivity extends SherlockFragmentActivity { 


static String URL = "http://www.site.url/tone.json"; 

static String KEY_CATEGORY = "item"; 
static final String KEY_TITLE = "title"; 

ListView list; 
ListItemAdapter adapter; 

/** Called when the activity is first created. */ 
@SuppressWarnings("deprecation") 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 

    getActionBar().setDisplayHomeAsUpEnabled(true); 
    setContentView(R.layout.activity_item_list); 

    final ArrayList<HashMap<String, String>> itemsList = new ArrayList<HashMap<String, String>>(); 
    list = (ListView) findViewById(R.id.listView1); 
    adapter = new ListItemAdapter(this, itemsList); 
    list.setAdapter(adapter); 

    Bundle bdl = getIntent().getExtras(); 
    KEY_CATEGORY = bdl.getString("category"); 

    if (isNetworkAvailable()) { 
     new MyAsyncTask().execute(); 
    } else { 

     AlertDialog alertDialog = new AlertDialog.Builder(ListItemActivity.this).create(); 
     alertDialog.setMessage("The Internet connection appears to be offline."); 
     alertDialog.setButton("OK", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int which) { 

      } 
     }); 
     alertDialog.show(); 
    } 
} 

public boolean onCreateOptionsMenu(Menu menu) { 
    MenuInflater inflater = getSupportMenuInflater(); 
    inflater.inflate(R.menu.main, menu); 

    return super.onCreateOptionsMenu(menu); 
} 

private Intent getDefaultShareIntent(){ 
     Intent intent = new Intent(Intent.ACTION_SEND); 
     intent.setType("text/plain"); 
     intent.putExtra(Intent.EXTRA_SUBJECT, "SUBJECT"); 
     intent.putExtra(Intent.EXTRA_TEXT, "TEXT"); 
     startActivity(Intent.createChooser(intent, "Share via")); 
     return intent; 
    } 

/** The event listener for the Up navigation selection */ 
@Override 
public boolean onOptionsItemSelected(com.actionbarsherlock.view.MenuItem item) { 

    switch(item.getItemId()) 
    { 
     case android.R.id.home: 
      finish(); 
      break; 

     case R.id.menu_item_share: 
      getDefaultShareIntent(); 
      break; 
    } 
    return true; 
} 

private boolean isNetworkAvailable() { 
    ConnectivityManager cm = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE); 
    NetworkInfo info = cm.getActiveNetworkInfo(); 
    return (info != null); 
} 

class MyAsyncTask extends 
     AsyncTask<String, Integer, ArrayList<HashMap<String, String>>> { 
    private ProgressDialog progressDialog = new ProgressDialog(
      ListItemActivity.this); 

    @Override 
    protected void onPreExecute() { 
     progressDialog.setMessage("Loading, Please wait....."); 
     progressDialog.show(); 
    } 

    final ArrayList<HashMap<String, String>> itemsList = new ArrayList<HashMap<String, String>>(); 

    @Override 
    protected ArrayList<HashMap<String, String>> doInBackground(
      String... params) { 
     HttpClient client = new DefaultHttpClient(); 
     // Perform a GET request for a JSON list 
     HttpUriRequest request = new HttpGet(URL); 
     // Get the response that sends back 
     HttpResponse response = null; 
     try { 
      response = client.execute(request); 
     } catch (ClientProtocolException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } catch (IOException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } 
     // Convert this response into a readable string 
     String jsonString = null; 
     try { 
      jsonString = StreamUtils.convertToString(response.getEntity() 
        .getContent()); 
     } catch (IllegalStateException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } catch (IOException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } 
     // Create a JSON object that we can use from the String 
     JSONObject json = null; 
     try { 
      json = new JSONObject(jsonString); 
     } catch (JSONException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } 

     try { 

      JSONArray jsonArray = json.getJSONArray(KEY_CATEGORY); 

      for (int i = 0; i < jsonArray.length(); i++) { 

       HashMap<String, String> map = new HashMap<String, String>(); 
       JSONObject jsonObject = jsonArray.getJSONObject(i); 

       map.put("id", String.valueOf(i)); 
       map.put(KEY_TITLE, jsonObject.getString(KEY_TITLE)); 
       itemsList.add(map); 
      } 
      return itemsList; 
     } catch (JSONException e) { 
      Log.e("log_tag", "Error parsing data " + e.toString()); 
     } 
     return null; 
    } 


    @Override 
    protected void onPostExecute(ArrayList<HashMap<String, String>> result) { 
     list = (ListView) findViewById(R.id.listView1); 
     adapter = new ListItemAdapter(ListItemActivity.this, itemsList); 
     list.setAdapter(adapter); 

     TextView lblTitle = (TextView) findViewById(R.id.text); 
     lblTitle.setText(KEY_CATEGORY); 

     this.progressDialog.dismiss(); 
     list.setOnItemClickListener(new OnItemClickListener() { 

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

       HashMap<String, String> map = itemsList.get(position); 

       Intent in = new Intent(ListItemActivity.this, ListItemDetailActivity.class); 
       in.putExtra(KEY_TITLE, map.get(KEY_TITLE)); 
       startActivity(in);     
      } 
     }); 
    } 
} 

}

回答

1

這是正常的行爲,因爲你是從移動片段活動到正常活動。但只有你的第一個活動有標籤。

這樣做的正確方法是刪除意圖和新活動。相反,它應該被替換爲片段本身。

例如,在列表項中單擊,停止將Intent調用到新的片段活動,並將其替換爲調用片段本身。

將只有一個活動,這將是持有的標籤和所有其他人應該是片段。您只需要替換同一活動中的片段即可。

+0

我認爲有用,7分鐘後會接受,是的你是對的,我試着通過調用另一個片段來進行同樣的活動,但是面臨的問題很少,你會幫助我嗎?如果你沒有任何問題,其實我是新人,並試圖讓這一個... – Sonali

+0

你面臨什麼樣的問題? –

+0

當然,會告訴你,請稍等片刻 – Sonali