如何使用自定义适配器而不是仅添加我添加的图像来单击列表活动中的整行?

时间:2011-04-26 14:09:06

标签: android imageview listactivity

我正在创建一个只包含行中图像的ListActivity。我可以显示图像但是当我尝试点击该行时它什么都不做。我可以点击我添加的图像,但我希望能够像普通列表活动一样点击整行。谁能告诉我怎么做到这一点?不知道为什么代码重写不会占用我的第一行但是在这里:

仍然无法解决这个问题。任何人吗?

public class AddNote extends ListActivity {

IconAdapter mAdapter;
private Button btnMove;
private Button btnDelete;
private Button btnSave;
private EditText etDescription;
private int iconPos = 0;
private String server;
private String project;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    final SharedPreferences app_preferences = PreferenceManager.getDefaultSharedPreferences(this);
    final SharedPreferences.Editor editor = app_preferences.edit();
    setContentView(R.layout.add_note);
    mAdapter = new IconAdapter(this);
    setListAdapter(mAdapter);
    mAdapter.addIcons();
    btnMove = (Button) findViewById(R.id.move);
    btnDelete = (Button) findViewById(R.id.delete);
    btnSave = (Button) findViewById(R.id.save);
    etDescription = (EditText) findViewById(R.id.description);
    server = app_preferences.getString("server", "none");
    project = app_preferences.getString("project", "no project");

    btnMove.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.e("Move", "Move");
        }
    });

    btnDelete.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.e("Delete", "Delete");
        }
    });

    btnSave.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (etDescription.getText().toString().trim().equals(""))
                Toast.makeText(getApplicationContext(), "Please enter a description", Toast.LENGTH_SHORT).show();
            else
            {
                UUID id = UUID.randomUUID();
                saveMapIcon(id, iconPos, etDescription.getText().toString(), "0,0");
            }
        }
    });
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
    super.onListItemClick(l, v, position, id);
    Log.e("Test", "testing....");
    iconPos = position;
}

private void saveMapIcon(UUID theID, int position, String description, String location)
{
    SharedPreferences app_preferences = PreferenceManager.getDefaultSharedPreferences(this);
    SharedPreferences.Editor editor = app_preferences.edit();

    String mapIcons = app_preferences.getString("mapNotes", "none");
    if (mapIcons.equals("none"))
    {
        mapIcons = server + "|" + project + "|" + theID + "|" + position + "|" + description + "|" + location + "*";
    }
    else
    {
        mapIcons += server + "|" + project + "|" + theID + "|" + position + "|" + description + "|" + location + "*";
    }
    Log.e("position:", String.valueOf(position));
    editor.putString("idToMove", theID.toString() + "|" + String.valueOf(position));
    editor.putString("mapNotes", mapIcons);
    editor.putBoolean("addingIcon", true);
    editor.commit();
    Log.e("Test", mapIcons);

    Intent intent = null;
    intent = new Intent(getApplicationContext(), GetMap.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(intent);
}

public class IconAdapter extends BaseAdapter {
    private Context mContext;
    ArrayList<View> imageViews = new ArrayList<View>();

    private Integer[] mIconList = {
            R.drawable.symbol1, R.drawable.symbol2, R.drawable.symbol3, R.drawable.symbol4, R.drawable.symbol5};

    private ArrayList<Integer> mIcons = new ArrayList<Integer>();

    public IconAdapter(Context c) {
        mContext = c;
    }

    public int getCount() {
        return mIconList.length;
    }

    public Object getItem(int position) {
        return mIconList[position];
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView i = new ImageView(mContext);
        i.setImageResource(mIcons.get(position));

        return i;
    }

    public void addIcons() {        
        for (int i=0; i<mIconList.length; i++)
        {
            mIcons.add(mIconList[i]);
        }
        notifyDataSetChanged();
    }
}

}

3 个答案:

答案 0 :(得分:0)

Millec8,

此功能需要您覆盖ListActivity中的函数onListItemClick() ...

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    //your code here
}    

答案 1 :(得分:0)

似乎应该有一种更简单的方法来做到这一点,但是从我使用自定义ListView的类似问题中,我做了以下内容:

使用新的自定义ImageView创建和替换ImageView,具有以下效果:

public class NoClickImageView extends ImageView {
    @Override
    protected void onKeyUp(int keyCode, KeyEvent event) {
        return false;
    }
    @Override
    protected void onKeyDown(int keyCode, KeyEvent event) {
        return false;
    }
    @Override
    protected void onTouchEvent(MotionEvent event) {
        return false;
    }
}

基本上,让它死了,它不会处理任何类型的触摸或点击事件。如果您需要选择它,可以添加setSelected()等额外方法,然后在onListItemSelected()方法中调用这些方法。但我相当肯定使用上述内容应该这样做。

答案 2 :(得分:0)

我最终在我的ArrayAdapter的getView()中创建了一个持有者,并用ImageView加载它。我还把它放在AlertDialog中,因为我怀疑它可能在尝试同时在屏幕上显示列表以及其他gui元素时遇到了问题。