0

我用 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);
4

2 回答 2

0

因为我自己没有尝试过,所以我会在这里进行很长时间的拍摄。但是我看到您有一个未显示的 ViewHolder 类。如果您真的想更改您的 onItemClick 代码,您可以在每个 ViewHolder 中存储一个意图,该意图在您的 getView(...) 中设置。通过这种方式,您可以在 onItemClick 中获取 viewholder 标记,并从该标记中获取意图并启动它。

例子:

public void onItemClick(AdapterView<?> parent, View view, int position, long id)
 {
    // Get the intent that u stored in this viewholder
    ViewHolder clicked = (ViewHolder)view.getTag();
    Intent toStart = clicked.getIntent();
    startActivity(toStart);          
 }

而且您不需要设置 onItemClick 侦听器,因为您是从 ListActivity 扩展的,只需覆盖它即可。

要实现所有这些,您需要在适配器中的 getView() 中进行一些更改。我相信你能弄清楚是什么!

于 2012-07-08T01:36:26.547 回答
0

首先,由于您使用的是 ListActivity,因此您不需要在列表本身上调用 setOnListItemClick 处理程序,只需从 ListActivity 覆盖此方法:

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

所以你可以有这个:

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);

}

然后回答您的其他问题,您可以尝试 Java 反射来获取一个类:

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);
于 2012-07-08T00:50:22.410 回答