简体   繁体   English

将onClick添加到列表视图?

[英]Adding onClick to listview?

I have a ListView and I want to run a function according to the clicked value on ListView for now i just want to know only how to get and toast the value I was trying to do it but don't know the proper code... please check my code. 我有一个ListView ,现在我想根据ListView上的单击值运行一个函数,我只想只知道如何获取和烘烤我尝试执行的值,但不知道正确的代码...请检查我的代码。

public class SubCategory extends AppCompatActivity implements View.OnClickListener {

private ListView listView;
String buttonType;
ArrayAdapter adapter;

static final String[] FRUITS = new String[] { "Apple", "Avocado", "Banana"};
static final String[] NAMES = new String[] { "Sachin", "Brett", "Shane", "Zaheer"};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sub_category);



    Bundle bundle = getIntent().getExtras();
    if(bundle != null) {
        buttonType = bundle.getString("BUTTON_CLICKED");
    }
    if(buttonType != null && buttonType.equals("A")) {
        adapter = new ArrayAdapter<String>(this, R.layout.activity_listview, FRUITS);
    } else if(buttonType != null && buttonType.equals("B")) {
        adapter = new ArrayAdapter<String>(this, R.layout.activity_listview, NAMES);
    }
    listView = (ListView)findViewById(R.id.listview);
    listView.setAdapter(adapter);
}

     public void getList() {

        Toast.makeText(listView.getContext(), Toast.LENGTH_LONG).show();

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.listview:
                getList();
                break;
    }
}
}
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        list.get(position).GET_INFORMATION(); //GET_INFORMATION will be the method in your POJO class
    }
});

try this 尝试这个

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
      String value="";
      if(!TextUtils.isEmpty(buttonType) && buttonType.equals("A")) {
          value=FRUITS[position];
       } else if(!TextUtils.isEmpty(buttonType) && buttonType.equals("B")) {
          value=NAMES[position];
       }
     // you can get value in "value"
       showToast(value);
    }
});
public void showToast(String value) {

    Toast.makeText(this,value, Toast.LENGTH_LONG).show();

}

To add click listener to listview follow these steps 要将单击侦听器添加到列表视图,请按照下列步骤操作

1.add click listener like this in oncreate method of the activity. 1.在活动的oncreate方法中添加这样的点击监听器。

listView.setOnItemClickListener(this);

2.Implement OnItemClickListener in the activity. 2.在活动中添加OnItemClickListener。

public class Main2Activity extends AppCompatActivity implements AdapterView.OnItemClickListener {

3.Override the onItemClick method in the activity. 3.重写活动中的onItemClick方法。

@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
    //Call method here
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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