Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Listview , open new activity onClick

Hey everyone I've looked for hours trying to find a solution to this, my goal is to have a Listview when it opens well open another activity. Well actually I got it to be able to open another activity when its click but how do I get it so that each list item will open its own activity? I am terribly sorry if this question is already answered but the links I found doesn't really describe what the code is doing [Yes i am a newbie :)]

this is the code im using

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

      String[] countries = getResources().getStringArray(R.array.countries_array);
      setListAdapter(new ArrayAdapter<String>(this, R.layout.newfile, countries));

      ListView lv = getListView();
      lv.setTextFilterEnabled(true);

      lv.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {
          // When clicked, show a toast with the TextView text
             Intent myIntent = new Intent(view.getContext(), Html_file.class);
             startActivityForResult(myIntent, 0);

        }
      });
    }
}
like image 774
Devin Avatar asked Jul 12 '11 06:07

Devin


People also ask

Which is better ListView or RecyclerView?

Simple answer: You should use RecyclerView in a situation where you want to show a lot of items, and the number of them is dynamic. ListView should only be used when the number of items is always the same and is limited to the screen size.

What is AdapterView?

AdapterView is a ViewGroup that displays items loaded into an adapter. The most common type of adapter comes from an array-based data source.

How many types of adapters are there in Android?

Android provides several subclasses of Adapter that are useful for retrieving different kinds of data and building views for an AdapterView ( i.e. ListView or GridView). The common adapters are ArrayAdapter,Base Adapter, CursorAdapter, SimpleCursorAdapter,SpinnerAdapter and WrapperListAdapter.


1 Answers

lv.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view,
        int position, long id) {
        // When clicked, show a toast with the TextView text
        if(position == 1) {
            //code specific to first list item    
            Intent myIntent = new Intent(view.getContext(), Html_file1.class);
            startActivityForResult(myIntent, 0);
        }

        if(position == 2) {
            //code specific to 2nd list item    
            Intent myIntent = new Intent(view.getContext(), Html_file2.class);
            startActivityForResult(myIntent, 0);
        }
    }
});
like image 80
ngesh Avatar answered Oct 02 '22 19:10

ngesh