Chad Precilla
Chad Precilla

Reputation: 57

How to start activity based on listview item click?

I have the following code but I have tried for days now and I cannot seem to figure out how to start an activity based on the listview item clicked, the result[position] does not work for start intent.

public class CustomAdapter extends BaseAdapter{
String [] result;
Context context;
int [] imageId;
private static LayoutInflater inflater=null;
public CustomAdapter(MainActivity mainActivity, String[] prgmNameList, int[] prgmImages) {
    // TODO Auto-generated constructor stub
    result=prgmNameList;
    context=mainActivity;
    imageId=prgmImages;
    inflater = ( LayoutInflater )context.
            getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
    // TODO Auto-generated method stub
    return result.length;
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

public class Holder
{
    TextView tv;
    ImageView img;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    Holder holder=new Holder();
    View rowView;
    rowView = inflater.inflate(R.layout.program_list, null);
    holder.tv=(TextView) rowView.findViewById(R.id.textView1);
    holder.img=(ImageView) rowView.findViewById(R.id.imageView1);
    holder.tv.setText(result[position]);
    holder.img.setImageResource(imageId[position]);
    rowView.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            Toast.makeText(context, "You Clicked "+result[position], Toast.LENGTH_LONG).show();
        }
    });
    return rowView;
}

EDIT : If the first list item is clicked I want it to start an activity (images class) , if second clicked to start images2 class and so on..

Upvotes: 0

Views: 904

Answers (4)

Skizo-ozᴉʞS ツ
Skizo-ozᴉʞS ツ

Reputation: 20616

Try something like this

rowView.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
         Intent intent=new Intent(v.getContext(),ActivityYouWantToGo.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
         v.getContext().startActivity(intent);
         Toast.makeText(context, "You Clicked "+result[position], Toast.LENGTH_LONG).show();
    }
});

EDIT

Just create this method on your BaseAdapter

public void StartActivityForImages(Context context, int i){

    switch (i){
        case 0:
            Intent Intent = new Intent(context, Image0.class);
            context.startActivity(Intent);
            break;

        case 1:
            Intent Intent2 = new Intent(context, Image1.class);
            context.startActivity(Intent2);
            break;
        case 2:
            Intent Intent3 = new Intent(context, Image3.class);
            context.startActivity(Intent3);
            break;

        ......
    }

}

And on your setOnClickListener()

You call this method as follows :

 StartActivityForImages(v.getContext(),position);
            Toast.makeText(context, "You Clicked " + result[position], Toast.LENGTH_LONG).show();

Efficient way to do it

If you don't want to create a method and do this stuff, you could do something like this :

Create a String with the name of your next class like :

String NextActivity = getPackageName()+".Image"+position;

And then you can do something like this :

try {
      String className =getPackageName()+".Image"+2;
      Intent openNewIntent = new Intent(v.getContext(), Class.forName( className ) );
      startActivity(  openNewIntent );
 } catch (ClassNotFoundException e) {
         Log.d("ERRORPEW", e.getMessage());
         e.printStackTrace();
 }

Upvotes: 0

Mohammad Tauqir
Mohammad Tauqir

Reputation: 1815

Please try this in onclick

    if(position ==1){
    --fire your first intent here.--
    } else if(position ==2) {
    -------your other intent------
    } 
    ----etc

Upvotes: 0

Arsal Imam
Arsal Imam

Reputation: 2982

Don't make position as a final static parameter in getView(), its a wronge way. Use below getView method to startActivity from list Item,

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    Holder holder=new Holder();
    View rowView;
    rowView = inflater.inflate(R.layout.program_list, null);
    holder.tv=(TextView) rowView.findViewById(R.id.textView1);
    holder.img=(ImageView) rowView.findViewById(R.id.imageView1);
    holder.tv.setText(result[position]);
    holder.img.setImageResource(imageId[position]);
    rowView.setTag(position);
    rowView.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            int pos = (Integer) v.getTag;
            Intent i = new Intent(context, YourActivity.class);
            context.startActivity(i);
        }
    });
    return rowView;
}

Upvotes: 0

iTurki
iTurki

Reputation: 16398

Starting an activity based on a ListView's item click is not done in the adapter. It is done in the Activity where you show the ListView:

In your Activity:

mListView.setAdapter(mAdapter); //mAdapter is your CustomAdapter 
mListView.setClickable(true);
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        //Start the activity here.
        //based on the value of position or id.
    }
});

Upvotes: 2

Related Questions