我有自定义ListView有两个textviews和一个按钮我想在行中的每个按钮上启动另一个活动?

时间:2017-05-01 10:57:00

标签: android listview android-custom-view

我希望当连续单击按钮时,它将启动另一个活动,该活动将显示特定行的详细信息。!
如何在每行的按钮上设置监听器。

主要活动:

公共类MainActivity扩展了AppCompatActivity {

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

    String[] friendList = {"Javad","Usama","Umar","Bilal","Bhutta sb","Zain Ul Abidin"};

    ListAdapter listAdapter = new CustomClass(this,friendList);
    ListView listView = (ListView)findViewById(R.id.listview);
    listView.setAdapter(listAdapter);



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

            String name = String.valueOf(parent.getItemAtPosition(position));

            Toast.makeText(MainActivity.this,name,Toast.LENGTH_SHORT).show();

        }
    });

}

}

3 个答案:

答案 0 :(得分:0)

在onItemClick内部

if(view=R.id.ButtonID)
{ 
launch new Activity
 }

答案 1 :(得分:0)

您可以将适配器中的按钮单击侦听器绑定到onItemClick中 在getView方法中返回视图之前。如果您不理解这个答案,那么请输入您的自定义适配器代码

      @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            LayoutInflater myCustomInflater = LayoutInflater.from(getContext());
            View customView = myCustomInflater.inflate(row, parent, false);
            String singleFoodItem = getItem(position);
            TextView itemText = (TextView) customView.findViewById(R.id.textView);
            ImageView buckysImage = (ImageView) customView.findViewById(R.id.imageView);
            itemText.setText(singleFoodItem);
Button button = customView.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(position == 0){
                  //go for next activity
                }
            }
        });
            itemText.setText(singleFoodItem);
            buckysImage.setImageResource(R.mipmap.doctorcartoon);
            return customView;
        }

答案 2 :(得分:0)

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

    String[] friendList = {"Javad","Usama","Umar","Bilal","Bhutta sb","Zain Ul Abidin"};

    ListAdapter listAdapter = new CustomClass(this,friendList);
    ListView listView = (ListView)findViewById(R.id.listview);
    listView.setAdapter(listAdapter);



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

            String name = String.valueOf(parent.getItemAtPosition(position));
            Intent i = new Intent(this,SecondActivity.class);
            i.putExtra("selectedValue",friendList[position]);
            startActivity(i);
            Toast.makeText(MainActivity.this,name,Toast.LENGTH_SHORT).show();

        }
    });

在OnCreate下的第二个活动

if(getIntent().getExtras() != null)
{
    String strData = getIntent().getExtras().getString("selectedValue")
}