制作自定义Android ListView适配器Clickable

时间:2013-01-15 15:35:27

标签: android image listview customization clickable

我一直在尝试创建一个可点击的列表视图,它包含一个字符串数组和一些图像,并以文本视图样式显示它们。到目前为止,我已经设法创建了每个字符串和图像的列表视图,但是我不确定如何使用onClick方法,以便使textviews可以点击以开始新的活动等。

到目前为止,这是我的代码(不包括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);

}

 }

到目前为止它的表现如下:

enter image description here

我基本上不明白的是onClick方法;它需要什么参数,以及如何确定单击了哪个项目。任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:3)

试试这个:

ListView list1 = getListView();

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

                         //Take action here.
                 }
            }
);

答案 1 :(得分:2)

您正在寻找OnItemClickListener而不是OnClickListener

 lv.setOnItemClickListener(new OnItemClickListener(){

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

现在只讨论参数:

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.

我从android参考

获得了最后一部分

答案 2 :(得分:1)

您可以使用此代码:

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

其中lv是listView。 如果要添加上下文菜单:

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

如果您想了解更多信息,请查看我的博客herehere