我用 ListView 编写了一个活动。现在我想知道,是否可以简化我的代码(使其更短)。
一个例子在代码块下
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;
}
}
}
例如:在 OnItemClickListener 中,我从列表项中获取 id,然后将其与 if 和 else 一起使用。但是 id 与活动中的名称相同,是否可以说新的 Intent,它应该打开名称在 String class_open 中的类?像这样的东西:
String class_open = ((TextView) view.findViewById(R.id.class_open)).getText().toString();
Intent in = new Intent(getApplicationContext(), <class_open>.class);
startActivity(in);