简化我的ListView(使用Intent,setOnItemClick等)

时间:2012-07-08 00:16:08

标签: android listview android-intent onitemclicklistener

我用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,它应该打开名为wich的类在class class_open中?像这样:

        String class_open = ((TextView) view.findViewById(R.id.class_open)).getText().toString();
        Intent in = new Intent(getApplicationContext(), <class_open>.class);
        startActivity(in);

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分:0)

因为我自己没试过这个,所以我要去这里打远看。但是我看到你有一个你没有展示的ViewHolder类。如果你真的想要更改你的onItemClick代码,你可以在每个ViewHolder中存储一个intent,它在你的getView(...)中设置。通过这种方式,您可以在onItemClick中获取viewholder标记并从该标记获取intent并启动它。

示例:

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()中进行一些更改。我相信你能搞清楚什么!