How can I click the whole row in a list activity with a custom adapter instead of just the image I added?

此生再无相见时 提交于 2019-12-10 12:14:29

问题


I'm creating a ListActivity that has only images in the rows. I can display the images already but when I try to click on the row it does nothing. I can click on the image that I've added but I would like to be able to click on the whole row like a normal List Activity. Can anyone tell me how I can do that? Not sure why the code backtick isn't picking up my first line but here it is:

Still haven't been able to solve this problem. Anyone?

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

}


回答1:


Millec8,

This functionality would require you override the function onListItemClick() in your ListActivity...

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



回答2:


It seems like there should be an easier way to do this, but from a similar question I had with a custom ListView, I did the following:

Create and replace the ImageView with a new custom ImageView, something to this effect:

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

Basically, make it dead, where it won't handle any sort of touch or click events. If you need it to be selectable, you can add extra methods like setSelected() and then call those in your onListItemSelected() method. But I'm fairly certain that using the above should do it.




回答3:


I ended up making a holder in the getView() of my ArrayAdapter and loading it with an ImageView. I also put it in an AlertDialog because I suspect it may have been having problems with trying to have a list as well as other gui elements on the screen at the same time.



来源:https://stackoverflow.com/questions/5791580/how-can-i-click-the-whole-row-in-a-list-activity-with-a-custom-adapter-instead-o

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!