Pregunta

In my program i am showing multilevel Listview, but whenever i am calling another Activity then Fragment Tabs are not appearing.

I am using this great tutorial: http://www.androidbegin.com/tutorial/android-actionbarsherlock-viewpager-tabs-tutorial/

See below Images, 1st Level ListView:

enter image description here

2nd Level 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);                  
            }
        });
    }
}

}

¿Fue útil?

Solución

This is the normal behavior because you are moving from the Fragment Activity to a normal Activity. But only your first activity has the tabs.

The correct way of doing this is to remove the intents and the new activities. Instead it should be replaced with fragments itself.

For example, in the list item click, stop calling the Intent to the new fragment activity, and replace it calling a fragment itself.

There will be only one Activity and that will be the one which is holding the tabs and all others should be fragments. You just need to replace the fragments from within the same activity.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top