如何给出一个从活动中列出片段的意图

时间:2015-12-04 07:30:37

标签: android android-intent android-activity android-listfragment

我是否需要在Mainfest中添加ListFragment,因为在我的应用程序中我想跳转到ListFragment From Activity。我无法弄清楚它的解决方案。在logcat中它给了我Unable to find explicit activity class {com.example.tatson.brasham/com.example.tatson.brasham.Dish}; have you declared this activity in your AndroidManifest.xml?请帮忙。

的活动:

public class Restdish extends Activity {

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

    Button rest = (Button)findViewById(R.id.button_rest);
    Button dish = (Button)findViewById(R.id.button_dish);

    rest.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Toast.makeText(Restdish.this, "Enabling GPS...", Toast.LENGTH_SHORT).show();
        }
    });

    dish.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent showContent = new Intent(v.getContext(), Dish.class);
            startActivity(showContent);
        }
    });
}

ListFragment:

public class Dish extends ListFragment {
private List<ListViewItem> mItems;        // ListView items list

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // initialize the items list
    mItems = new ArrayList<ListViewItem>();
    Resources resources = getResources();

    mItems.add(new ListViewItem(ContextCompat.getDrawable(getContext(), R.drawable.samosa), getString(R.string.aim), getString(R.string.aim_description)));
    mItems.add(new ListViewItem(ContextCompat.getDrawable(getContext(),R.drawable.channamasala), getString(R.string.bebo), getString(R.string.bebo_description)));

    //   mItems.add(new ListViewItem(resources.getDrawable(R.drawable.youtube), getString(R.string.youtube), getString(R.string.youtube_description)));

    // initialize and set the list adapter
    setListAdapter(new ListViewDemoAdapter(getActivity(), mItems));
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    // remove the dividers from the ListView of the ListFragment
    getListView().setDivider(null);
}

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    // retrieve theListView item
    ListViewItem item = mItems.get(position);

    // do something
    Toast.makeText(getActivity(), item.title, Toast.LENGTH_SHORT).show();
}

0 个答案:

没有答案