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

  • SEARCH
  • Home
  • 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 8202789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T07:18:57+00:00 2026-06-07T07:18:57+00:00

I write a activity with a ListView. Now I want to know, is it

  • 0

I write a activity with a ListView. Now I want to know, is it possible to simplify my code (make it shorter).

An example is under the codeblock

package de.bodprod.dkr;

import java.util.ArrayList;
import java.util.HashMap;


import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class MediMenue extends ListActivity {
    ArrayList<HashMap<String, Object>> medi_menue_items;
    LayoutInflater inflater;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.medi_menue);
        ListView antidotListView = (ListView) findViewById(android.R.id.list);
        inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);        


        medi_menue_items = new ArrayList<HashMap<String,Object>>();

        HashMap<String , Object> item = new HashMap<String, Object>();
        item.put("title", "Antidots");
        item.put("desc", "Toxine und die Gegenmittel");
        item.put("class_open", "MediAntidotList");
        medi_menue_items.add(item);

        item = new HashMap<String, Object>();
        item.put("title", "Notfallmedikamente");
        item.put("desc", "Dosierungen, Gegenanzeigen u.v.m.");
        item.put("class_open", "MediNotmediList");
        medi_menue_items.add(item);

        ListView lv;
        lv = (ListView) findViewById(android.R.id.list); 
        lv.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent in = new Intent();
                String class_open = ((TextView) view.findViewById(R.id.class_open)).getText().toString();
                if(class_open.equals("MediAntidotList")){
                    in = new Intent(getApplicationContext(), MediAntidotList.class);
                }else if(class_open.equals("MediNotmediList")){
                    in = new Intent(getApplicationContext(), MediNotmediList.class);
                }
                startActivity(in);

            }
        });     

        final CustomAdapter adapter = new CustomAdapter(this, R.layout.medi_menue, medi_menue_items);
        antidotListView.setAdapter(adapter);            

    }
    private class CustomAdapter extends ArrayAdapter<HashMap<String, Object>>{
        public CustomAdapter(Context context, int textViewResourceId, ArrayList<HashMap<String, Object>> Strings) {
            super(context, textViewResourceId, Strings);
        }
        private class ViewHolder{
            TextView class_open, title, desc;
        }
        ViewHolder viewHolder;

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if(convertView==null){        
                convertView=inflater.inflate(R.layout.medi_menue_item, null);
                viewHolder=new ViewHolder();
                viewHolder.class_open=(TextView) convertView.findViewById(R.id.class_open);
                viewHolder.title=(TextView) convertView.findViewById(R.id.medi_menue_name);
                viewHolder.desc=(TextView) convertView.findViewById(R.id.medi_menue_desc);
                convertView.setTag(viewHolder);
            }else{
                viewHolder=(ViewHolder) convertView.getTag();
            }
            viewHolder.class_open.setText(medi_menue_items.get(position).get("class_open").toString());
            viewHolder.title.setText(medi_menue_items.get(position).get("title").toString());
            viewHolder.desc.setText(medi_menue_items.get(position).get("desc").toString());
            return convertView;
        }
    }   
} 

For example: In the OnItemClickListener I get the id from the list item, and than I use with if and else. But the id is the same like the name from the activity, is it possible to say the new Intent, that it should open the class with the name wich is in the String class_open? Something like this:

        String class_open = ((TextView) view.findViewById(R.id.class_open)).getText().toString();
        Intent in = new Intent(getApplicationContext(), <class_open>.class);
        startActivity(in);
  • 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-06-07T07:18:59+00:00Added an answer on June 7, 2026 at 7:18 am

    First of all, since you are using ListActivity, you don’t need to call setOnListItemClick handler on the list itself, just override this method from the ListActivity:

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

    So you can have this:

    protected void onListItemClick(ListView l, View v, int position, long id) {
        Intent in = new Intent();
        String class_open = ((TextView)v.findViewById(R.id.class_open)).getText().toString();
        if(class_open.equals("MediAntidotList")){
                    in = new Intent(getApplicationContext(), MediAntidotList.class);
                }else if(class_open.equals("MediNotmediList")){
                    in = new Intent(getApplicationContext(), MediNotmediList.class);
                }
        startActivity(in);
    
    }
    

    Then to answer your other question, you can try the Java reflection to get a class:

    String class_open = ((TextView)view.findViewById(R.id.class_open)).getText().toString();
    Class c = Class.forName("com.yourpackagename." + class_open );
    Intent in = new Intent(getApplicationContext(), c);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For example, I want to write an activity that launches when you return from
I want to write a program from scratch to see the sockets activity, what
I want to write a simple program which shows my internet activity over a
i wrote a View class based on ViewSwitcher and now I want to write
I want to change the color of a TextView inside my Activity s ListView
Here is my code, public class SecondScreenActivity extends Activity { ListView foodJntListView; ArrayList<Restaurent> restaurentData;
I am trying to write a simple activity that received from the user its
I'm trying to write a library to separate all the disk activity out into
I'm trying to write a query to join a user table to an activity
I wrote some code that activity starts a service to get some text from

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.