Gridview ,open new activity on item click

Clicking on the items here is supposed to go to the specific activity, but as I have given the if else statement, instead of going to the specific activity, the homework-2 activity goes as soon as the apps run. And after pressing back three times the main activity comes again. When I press on specific item on menu, supposed to go to the next activity, but nothing happens.

public class MainActivity extends AppCompatActivity {

GridView grid_view;

ArrayList <HashMap<String,String>>arrayList = new ArrayList<>();
HashMap<String,String>hashMap = new HashMap<>();

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

    grid_view = findViewById(R.id.grid_view);

    creatTable ();


    myAdapter myAdapter = new myAdapter();
    grid_view.setAdapter(myAdapter);

}


private class myAdapter extends BaseAdapter {

    @Override
    public int getCount() {
        return arrayList.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    } 

@Override public View getView(int position, View convertView, ViewGroup parent) { LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); View myView = layoutInflater.inflate(R.layout.item_lay,parent,false);

        TextView item_cat = myView.findViewById(R.id.item_cat);
        TextView item_title = myView.findViewById(R.id.item_title);

        HashMap<String,String>hashMap = arrayList.get(position);


        Random rnd = new Random();
        int color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
        item_cat.setBackgroundColor(color);

        Random rnd1 = new Random();
        int color1 = Color.argb(255, rnd1.nextInt(256), rnd1.nextInt(256), rnd1.nextInt(256));
        item_title.setBackgroundColor(color1);


        String cat = hashMap.get("CAt");
        String title = hashMap.get("Title");

        item_cat.setText(cat);
        item_title.setText(title);

       if (cat.contains("HomeWork_207")){
           startActivity(new Intent(MainActivity.this,HomeWork_One.class));
       }else if (cat.contains("HomeWork_214.1")){
           startActivity(new Intent(MainActivity.this,HomeWork_Two.class));
       }

        return myView;
    }


}
private void creatTable() {
    hashMap = new HashMap<>();
    hashMap.put("CAt","HomeWork_207");
    hashMap.put("Title","bmi cal");
    arrayList.add(hashMap);

    hashMap = new HashMap<>();
    hashMap.put("CAt","HomeWork_214.1");
    hashMap.put("Title","Divisible");
    arrayList.add(hashMap);

    hashMap = new HashMap<>();
    hashMap.put("CAt","HomeWork 214.2");
    hashMap.put("Title","Leap year");
    arrayList.add(hashMap);

    hashMap = new HashMap<>();
    hashMap.put("CAt","HomeWork 214.3");
    hashMap.put("Title","Week");
    arrayList.add(hashMap);

    hashMap = new HashMap<>();
    hashMap.put("CAt","HomeWork 214.4");
    hashMap.put("Title","Exam Grade");
    arrayList.add(hashMap);

    hashMap = new HashMap<>();
    hashMap.put("CAt","HomeWork 214.5");
    hashMap.put("Title","Bill");
    arrayList.add(hashMap);

    hashMap = new HashMap<>();
    hashMap.put("CAt","HomeWork 232.1");
    hashMap.put("Title","নামতা");
    arrayList.add(hashMap);

    hashMap = new HashMap<>();
    hashMap.put("CAt","HomeWork 232.2");
    hashMap.put("Title","E.No & sum");
    arrayList.add(hashMap);

    hashMap = new HashMap<>();
    hashMap.put("CAt","HomeWork 232.3");
    hashMap.put("Title","E.No & sum");
    arrayList.add(hashMap);

    hashMap = new HashMap<>();
    hashMap.put("CAt","HomeWork 232.3");
    hashMap.put("Title","N.terms");
    arrayList.add(hashMap);
}

}

I am trying this , way but problem is not solved yet.

Upvotes: 1

Views: 15

Answers (0)

Related Questions