0

I have an activity containing list view and a button at the bottom. When the button is clicked I need to open a fragment containing multiple choice list view and a button at the bottom... Now when I click the button in the fragment I need to get back to the same activity.

this is activity containing list and button

and

this is fragment with multiple choice list and a button

Help me to add fragment layout and fragment in my activity...

this is my activity code

public class MainActivity2 extends Activity implements
        OnItemClickListener  {
    int shop_len;
    String[] chennai,mumbai,calcutta;
    String[] desc_chennai,desc_mumbai,desc_calcutta;

    ListView listView2;
    List<Item2> rowItems2;
    Button button;

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        String city = getIntent().getExtras().getString("CNAME");
        chennai = getResources().getStringArray(R.array.area_chennai);
        mumbai = getResources().getStringArray(R.array.area_mumbai);
        calcutta = getResources().getStringArray(R.array.area_calcutta);
        shop_len=getResources().getStringArray(R.array.area_shop).length;
        desc_chennai = new String[]{String.valueOf(shop_len),String.valueOf(shop_len),String.valueOf(shop_len)};
        desc_mumbai = new String[]{String.valueOf(shop_len),String.valueOf(shop_len),String.valueOf(shop_len),String.valueOf(shop_len)};
        TextView cityname = (TextView) findViewById(R.id.name);
        cityname.setText("City: "+city);



        if (city.equals("Chennai")) {
            rowItems2 = new ArrayList<Item2>();
            for (int i = 0; i < chennai.length; i++) {
                Item2 item = new Item2(chennai[i], desc_chennai[i]);
                rowItems2.add(item);
            }

            listView2 = (ListView) findViewById(R.id.list2);
            CustomListViewAdapter2 adapter2 = new CustomListViewAdapter2(this,
                    R.layout.list_item2, rowItems2);
            listView2.setAdapter(adapter2);
            listView2.setOnItemClickListener(this);

        }else if(city.equals("Mumbai")){
            rowItems2 = new ArrayList<Item2>();
            for (int i = 0; i < mumbai.length; i++) {
                Item2 item = new Item2(mumbai[i], desc_mumbai[i]);
                rowItems2.add(item);
            }

            listView2 = (ListView) findViewById(R.id.list2);
            CustomListViewAdapter2 adapter2 = new CustomListViewAdapter2(this,
                    R.layout.list_item2, rowItems2);
            listView2.setAdapter(adapter2);
            listView2.setOnItemClickListener(this);
        }else if(city.equals("Calcutta")){
            rowItems2 = new ArrayList<Item2>();
            for (int i = 0; i < mumbai.length; i++) {
                Item2 item = new Item2(mumbai[i], desc_mumbai[i]);
                rowItems2.add(item);
            }

            listView2 = (ListView) findViewById(R.id.list2);
            CustomListViewAdapter2 adapter2 = new CustomListViewAdapter2(this,
                    R.layout.list_item2, rowItems2);
            listView2.setAdapter(adapter2);
            listView2.setOnItemClickListener(this);
        }

        button = (Button) findViewById(R.id.filterbutton);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {

                // Start NewActivity.class
                Intent myIntent = new Intent(MainActivity2.this,
                        FilterActivity.class);
                startActivity(myIntent);
                overridePendingTransition(R.anim.slide_in_up, R.anim.slide_out_up);
            }
        });
    }
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    }
}

layout of the activity:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <TextView android:id="@+id/name"
        android:layout_width="fill_parent"
        android:layout_height="40dp"

        android:textSize="20dip"
        android:textStyle="bold"/>
    <ListView
        android:id="@+id/list2"
        android:layout_width="match_parent"
        android:layout_height="460dp"
        />
    <Button
        android:id="@+id/filterbutton"
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:layout_gravity="center"

        android:text="@string/filter" />
</LinearLayout>

Now where do I add my fragment layout in this XML and how to add fragment in my activity?

ir-tech
  • 298
  • 4
  • 16
s.vijay
  • 31
  • 8

0 Answers0