简体   繁体   中英

Making a custom android ListView adapter Clickable

I've been trying to create a clickable listview that takes in a string array and a few images and presents them in a textview style. So far I have managed to create a listview with each of the strings and images, however I am unsure how to use the onClick method so as to make the textviews clickable to start new activities etc.

Here is my code so far (Excluding XML):

public class MySimpleArrayAdapter extends ArrayAdapter<String> {
    private final Context context;
    private final String[] values;

    public MySimpleArrayAdapter(Context context, String[] values) {
    super(context, R.layout.activity_test2, values);
    this.context = context;
    this.values = values;
}

    /* Print a toast when a list item is clicked, don't know what to do */
 public void onClick() {
    switch (list item) {
    case 0:
        Toast.makeText(this.context, "Pressed!", Toast.LENGTH_LONG).show()
        break;
    }
            case 1:
                    etc....

} 

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View rowView = inflater.inflate(R.layout.activity_test2, parent, false);
    TextView textView = (TextView) rowView.findViewById(R.id.label);
    ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
    textView.setText(values[position]);

    String s = values[position];
    if (s.startsWith("Report a Road Delay")) {
        imageView.setImageResource(R.drawable.ic_menu_compose);
    } else if (s.startsWith("View Reported Delays")) {
        imageView.setImageResource(R.drawable.ic_menu_view);
    } else if (s.startsWith("Search a Road for Delays")) {
        imageView.setImageResource(R.drawable.ic_menu_search);
    } else if (s.startsWith("Update a Delay Report")) {
        imageView.setImageResource(R.drawable.ic_menu_edit);
    } else if (s.startsWith("Validate a Delay Report")) {
        imageView.setImageResource(R.drawable.ic_menu_mark);

    }
    return rowView;
  }
}

 public class MainActivity extends ListActivity {
public void onCreate(Bundle SavedInstanceState) {
    super.onCreate(SavedInstanceState);
    String[] values = new String[] { "Report a Road Delay",
            "View Reported Delays", "Search a Road for Delays",
            "Update a Delay Report", "Validate a Delay Report" };
    MySimpleArrayAdapter adapter = new MySimpleArrayAdapter(this, values);
    setListAdapter(adapter);

}

 }

This is how it looks so far:

在此处输入图片说明

All I basically don't understand is the onClick method; what parameters it takes it, and how to determine which item was clicked. Any help would be appreciated.

Try this:

ListView list1 = getListView();

list1.setOnItemClickListener(
        new OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> arg0, View view,
                    int position, long id) {

                         //Take action here.
                 }
            }
);

You are looking for an OnItemClickListener and not an OnClickListener

 lv.setOnItemClickListener(new OnItemClickListener(){

    @Override
    public void onItemClick(AdapterView<?> parent, View v, int position, long id) {   
         // DO SOMETHING WITH CICK EVENT HERE
    }
 }

Now only to discus the params:

parent   The AdapterView where the click happened.
view     The view within the AdapterView that was clicked
position The position of the view in the adapter.
id       The row id of the item that was clicked.

I got the last part from android reference

You could use this code:

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        public void onItemClick(AdapterView<?> parentAdapter, View view, int position,
                long id) {


            // We know the View is a <extView so we can cast it
            TextView clickedView = (TextView) view;

            Toast.makeText(MainActivity.this, "Item with id ["+id+"] - Position ["+position+"] - Planet ["+clickedView.getText()+"]", Toast.LENGTH_SHORT).show();

        }
       });

      // we register for the contextmneu        
      registerForContextMenu(lv);
    }

where lv is the listView. If you want to add a context menu:

   // We want to create a context Menu when the user long click on an item
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
        ContextMenuInfo menuInfo) {

    super.onCreateContextMenu(menu, v, menuInfo);
    AdapterContextMenuInfo aInfo = (AdapterContextMenuInfo) menuInfo;

    // We know that each row in the adapter is a Map
    Planet planet =  aAdpt.getItem(aInfo.position);

    menu.setHeaderTitle("Options for " + planet.getName());
    menu.add(1, 1, 1, "Details");
    menu.add(1, 2, 2, "Delete");

}




// This method is called when user selects an Item in the Context menu
@Override
public boolean onContextItemSelected(MenuItem item) {
    int itemId = item.getItemId();
    AdapterContextMenuInfo aInfo = (AdapterContextMenuInfo) item.getMenuInfo();
    planetsList.remove(aInfo.position);
    aAdpt.notifyDataSetChanged();
    return true;
}

If you want to have more information give a look on my blog here and here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM