Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7010803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:02:45+00:00 2026-05-27T22:02:45+00:00

Im trying to built this app which simple consist of a ListView and different

  • 0

Im trying to built this app which simple consist of a ListView and different other activities that will be called when an item from the view is clicked. However, I am unable to implement a working onclicklistener in the main activity. With the code below, I am unable to open the activity.

Heres the code:

public void setOnItemClickListener(AdapterView<?> parent, View view,
      int position, long id) {
    switch( position )
    {
       case 0:  Intent newActivity0 = new Intent(this, TempuraActivity.class);     
                startActivity(newActivity0);
                break;
       case 1:  Intent newActivity1 = new Intent(this, TempuraActivity.class);     
                startActivity(newActivity1);
                break;
       case 2:  Intent newActivity2 = new Intent(this, TempuraActivity.class);     
                startActivity(newActivity2);
                break;
       case 3:  Intent newActivity3 = new Intent(this, TempuraActivity.class);     
                startActivity(newActivity3);
                break;
       case 4:  Intent newActivity4 = new Intent(this, TempuraActivity.class);     
                startActivity(newActivity4);
                break;
    }
}

Any help would be appreciated 🙂 Comment if U need to see the full code, I will update in the post. Thanks!

Thanks for all the Replies!
Heres the full code:

public class MainActivity extends ListActivity {
private LayoutInflater mInflater;
private Vector<RowData> data;
RowData rd;

static final String[] title = new String[] {
    "Christmas Crepes", "Dorayaki", "Corned Beef", "Tempura"
    };

static final String[] release = new String[] {
"New!", "New!", "4 months ago", "New!"
};

static final String[] description = new String[] {
    "Blehh..","Pam pam pam","new era","Steve Jobs is dead"
    };

static final String[] difficulty = new String[] {
"Hard","Easy","Medium","Hard"
};

static final String[] serves = new String[] {
"4 persons","2 persons","2 cups","1 person"
};

static final String[] time = new String[] {
"40 mins","20 mins","50 mins","120 mins"
};

private Integer[] imgid = {
  R.drawable.thumbone,R.drawable.thumbtwo,R.drawable.thumbthree,
  R.drawable.thumbfour
};

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mInflater = (LayoutInflater) getSystemService(
Activity.LAYOUT_INFLATER_SERVICE);
data = new Vector<RowData>();
for(int i=0;i<title.length;i++){

try {
rd = new       RowData(i,title[i],release[i],description[i],difficulty[i],serves[i],time[i]);
    } catch (ParseException e) {
    e.printStackTrace();
   }
   data.add(rd);
   }
   CustomAdapter adapter = new CustomAdapter(this, R.layout.list, android.R.id.list, data);
   setListAdapter(adapter);
   getListView().setTextFilterEnabled(true);
   }
   public void setOnItemClickListener(AdapterView<?> parent, View view,
      int position, long id) {
    switch( position )
    {
       case 0:  Intent newActivity0 = new Intent(this, TempuraActivity.class);     
                startActivity(newActivity0);
                break;
       case 1:  Intent newActivity1 = new Intent(this, TempuraActivity.class);     
                startActivity(newActivity1);
                break;
       case 2:  Intent newActivity2 = new Intent(this, TempuraActivity.class);     
                startActivity(newActivity2);
                break;
       case 3:  Intent newActivity3 = new Intent(this, TempuraActivity.class);     
                startActivity(newActivity3);
                break;
       case 4:  Intent newActivity4 = new Intent(this, TempuraActivity.class);     
                startActivity(newActivity4);
                break;
    }
}
   private class RowData {
   protected int mId;
   protected String mTitle;
   protected String mRelease;
   protected String mDescription;
   protected String mDifficulty;
   protected String mServes;
   protected String mTime;
   RowData(int id, String title, String release, String description, String difficulty, String serves, String time){
   mId=id;
   mTitle = title;
   mRelease = release;
   mDescription = description;
   mDifficulty = difficulty;
   mServes = serves;
   mTime = time;
   }
   @Override
   public String toString() {
           return mId+" "+mTitle+" "+mRelease+" "+mDescription+" "+mDifficulty+" "+mServes+" "+mTime;
   }
}
private class CustomAdapter extends ArrayAdapter<RowData> {

public CustomAdapter(Context context, int resource, int textViewResourceId, List<RowData> objects) {               

super(context, resource, textViewResourceId, objects);
}
  @Override
   public View getView(int position, View convertView, ViewGroup parent) {   

   ViewHolder holder = null;
   TextView title = null;
   TextView release = null;
   TextView description = null;
   TextView difficulty = null;
   TextView serves = null;
   TextView time = null;
   ImageView thumbnail=null;
   RowData rowData= getItem(position);
   if(null == convertView){
        convertView = mInflater.inflate(R.layout.list, null);
        holder = new ViewHolder(convertView);
        convertView.setTag(holder);
}
         holder = (ViewHolder) convertView.getTag();
         title = holder.gettitle();
         title.setText(rowData.mTitle);
         release = holder.getdescription();
         release.setText(rowData.mRelease);
         description = holder.getrelease();
         description.setText(rowData.mDescription);
         difficulty = holder.getdifficulty();
         difficulty.setText(rowData.mDifficulty);
         serves = holder.getserves();
         serves.setText(rowData.mServes);
         time = holder.gettime();
         time.setText(rowData.mTime);

         thumbnail=holder.getImage();
         thumbnail.setImageResource(imgid[rowData.mId]);
         return convertView;
}
        private class ViewHolder {
        private View mRow;
        private TextView title = null;
        private TextView release = null;
        private TextView description = null;
        private TextView difficulty = null;
        private TextView serves = null;
        private TextView time = null;
        private ImageView thumbnail=null; 

        public ViewHolder(View row) {
        mRow = row;
}
    public TextView gettitle() {
         if(null == title){
             title = (TextView) mRow.findViewById(R.id.title);
            }
        return title;
     }     

    public TextView getrelease() {
        if(null == release){
            release = (TextView) mRow.findViewById(R.id.release);
           }
       return release;
    } 

     public TextView getdescription() {
         if(null == description){
              description = (TextView) mRow.findViewById(R.id.description);
                }
       return description;
     }
     public TextView getdifficulty() {
         if(null == difficulty){
             difficulty = (TextView) mRow.findViewById(R.id.difficulty);
            }
        return difficulty;
     }  
     public TextView getserves() {
         if(null == serves){
             serves = (TextView) mRow.findViewById(R.id.serves);
            }
        return serves;
     }  
     public TextView gettime() {
         if(null == time){
             time = (TextView) mRow.findViewById(R.id.title);
            }
        return time;
     }  

    public ImageView getImage() {
         if(null == thumbnail){
              thumbnail = (ImageView) mRow.findViewById(R.id.thumbnail);
                                  }
            return thumbnail;
        }
     }            
   }
}

If there are any better method to achieve what my app should, please post! Im in desperate need to finish this for once!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T22:02:46+00:00Added an answer on May 27, 2026 at 10:02 pm

    UPDATE

    Change

    public void setOnItemClickListener(AdapterView<?> parent, View view,
      int position, long id) {
    

    To

    protected void onListItemClick(ListView l, View v, int position, long id) {
    

    And it should work.

    ORIGINAL

    Your code is a little confusing, a setOnItemClickListener exists but you need to actually pass the method a onItemClickListener object (or anonymous class).

    Perhaps you are looking for something like this in your ListActivity.

    final ListView listView = getListView();
    listView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> paramAdapterView, View paramView, int paramInt,
                long paramLong) {
            switch( position )
    {
       case 0:  Intent newActivity0 = new Intent(YourActivity.this, TempuraActivity.class);     
                startActivity(newActivity0);
                break;
       case 1:  Intent newActivity1 = new Intent(YourActivity.this, TempuraActivity.class);     
                startActivity(newActivity1);
                break;
       case 2:  Intent newActivity2 = new Intent(YourActivity.this, TempuraActivity.class);     
                startActivity(newActivity2);
                break;
       case 3:  Intent newActivity3 = new Intent(YourActivity.this, TempuraActivity.class);     
                startActivity(newActivity3);
                break;
       case 4:  Intent newActivity4 = new Intent(YourActivity.this, TempuraActivity.class);     
                startActivity(newActivity4);
                break;
    }              
        }
    });
    

    Here you can implement your switch statement inside the onItemClick method of the onItemClickListener and it should work.

    Note: You will have to change this to YourActivity.this as you will be inside an inner class and will need to reference the outer activity class.

    Alternatively you could override onListItemClick:

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        // ... put switch statement here
    }
    

    In your list activity to eliminate the additional call to getListView()

    If this doesn’t answer your question please full code

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to built a simple app which has a translucent frame and it
I am trying to build a query which will do this: Lets say for
I'm trying to build a very, very simple micro-webapp which I suspect will be
i am trying to build simple php crawler for this purpose i am getting
I'm trying to build a view that operators something similar to this: The user
I'm trying out log4j in a simple test app. I create a new Java
I'm trying to create a simple Android UI that consists of a EditText and
I've been trying to build this small java app. I find it very difficult
I'm totally new to flash, and I'm trying to build a very simple app.
I am trying to build a rails app that's basically just a text-editor (with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.