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);
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:
So you can have this:
Then to answer your other question, you can try the Java reflection to get a class: