0

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

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

}

 }

这是到目前为止的样子:

在此处输入图像描述

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

4

3 回答 3

3

试试这个:

ListView list1 = getListView();

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

                         //Take action here.
                 }
            }
);
于 2013-01-15T15:45:24.970 回答
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参考中得到了最后一部分

于 2013-01-15T15:37:09.670 回答
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 是列表视图。如果要添加上下文菜单:

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

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

于 2013-01-15T15:47:28.663 回答