bhagya_kz
bhagya_kz

Reputation: 128

Listview and baseadapter

I have created listview using baseadapter. I want to go to different activities when click on different items but not able to get item value.So anyone can help me how to do it. For example on click on Item x i want to activity1 on clicking item say z,I want to go to avctivity2 Thank you.

Here is code:

    public class homeScreen extends Activity{
ListView list1; 

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.homescreen);

        list1=(ListView)findViewById(R.id.spacelist);
        list1.setAdapter(new MySimpleAdapter(this));
        list1.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View v, int position,long id) {
                // TODO Auto-generated method stub      
                Intent i=new Intent(homeScreen.this, privateSpaceList.class);
                startActivity(i);
            }
        });
    }
}

class SingleRow{

    String title;
    int image;
    public SingleRow(String title,int image) {
        // TODO Auto-generated constructor stub
        this.title=title;
        this.image=image;
    }
}
 class MySimpleAdapter extends BaseAdapter{

    ArrayList<SingleRow> list;
    Context context;
    public MySimpleAdapter(Context c) {
        // TODO Auto-generated constructor stub
        context=c;
        list=new ArrayList<SingleRow>();
        //putting actual values in array
        Resources res=c.getResources();
        String[] titles=res.getStringArray(R.array.titles);
        int[] images={R.drawable.error,R.drawable.ic_launcher,R.drawable.ic_launcher};

        //putting single row in arraylist
        for(int i=0;i<3;i++)
        {
            list.add(new SingleRow(titles[i], images[i]));
        }
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return list.size();
    }
    @Override
    public Object getItem(int i) {
        // TODO Auto-generated method stub
        return list.get(i);
    }
    @Override
    public long getItemId(int i) {
        // TODO Auto-generated method stub
        return i;
    }

    //called when want to display row
    @Override
    public View getView(int i, View view, ViewGroup viewgroup) {
        // TODO Auto-generated method stub
        LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View row=inflater.inflate(R.layout.single_row,viewgroup,false);
        TextView title=(TextView)row.findViewById(R.id.label);
        ImageView image=(ImageView)row.findViewById(R.id.imageView);
        SingleRow temp=list.get(i);
        title.setText(temp.title);
        image.setImageResource(temp.image);
        return row;
    }
}

Upvotes: 0

Views: 884

Answers (4)

Amit Gupta
Amit Gupta

Reputation: 8939

Try like this,

Its a very easy way to do without so much of conditions.

Define a array of all your Activity class name. But all activity should in the same package.

String[] Classtitles=new String[]{"Activity1","Activity2","Activity3"};

Get the Package Name

final String packageName = this.getClass().getPackage().getName();

And change inside the onItemClick()

list1.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View v, int position,long id) {
               try {
                        Class c = Class.forName(packageName + "." + Classtitles[position]);
                        startActivity(new Intent(homeScreen.this, c));
                    } catch (ClassNotFoundException e) {
                        Toast.makeText(homeScreen.this, String.valueOf(e), 5000).show();
                    }
            }
        });

Hope this will help you.

Upvotes: 0

Meenal
Meenal

Reputation: 2877

On your item click change your code to

list1.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> arg0, View v, int position,long id) {
        // TODO Auto-generated method stub  
       String[] titles=res.getStringArray(R.array.titles); 
         if((titles[position]).equalsIgnoreCase("Itemx"))
         {
              Intent i=new Intent(homeScreen.this, privateSpaceList.class);
              startActivity(i);
         }
         else if((titles[position]).equalsIgnoreCase.equals("Itemy"))
         {
              Intent i=new Intent(homeScreen.this, activity2.class);
              startActivity(i);
         }
         else if((titles[position]).equalsIgnoreCase.equals("Itemz"))
         {
              Intent i=new Intent(homeScreen.this, activity3.class);
              startActivity(i);
         }
    }
});

Upvotes: 0

Hariharan
Hariharan

Reputation: 24853

Try this..

list1.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View v, int position,long id) {
            // TODO Auto-generated method stub  
            TextView title=(TextView)v.findViewById(R.id.label);    
             if(title.getText().toString().trim().equals("Item x"))
             {
                  Intent i=new Intent(homeScreen.this, activity1.class);
                  startActivity(i);
             }
             else if(title.getText().toString().trim().equals("Item y"))
             {
                  Intent i=new Intent(homeScreen.this, activity2.class);
                  startActivity(i);
             }
             else if(title.getText().toString().trim().equals("Item z"))
             {
                  Intent i=new Intent(homeScreen.this, activity3.class);
                  startActivity(i);
             }
        }
    });

Upvotes: 0

artemiygrn
artemiygrn

Reputation: 1036

I think easy way to implement ClickListener on getView in Adapter class. Or, in your case you need store List<..>, also this list you add to adapter, and in this moment:

@Override
public void onItemClick(AdapterView<?> arg0, View v, int position,long id) {   
  Intent intent = new Intent(homeScreen.this, privateSpaceList.class);
  yourList.get(position) // your data
  startActivity(intent);
}

You get your object.

or like this:

    @Override
    public void onItemClick(AdapterView<?> arg0, View v, int position,long id) {  

    switch(position) {

      case 1:
          Intent intent = new Intent(homeScreen.this, privateSpaceList.class);
          startActivity(intent);
          break;

      }

    }

Upvotes: 1

Related Questions