CustomListView的不同项目上的多个意图

时间:2018-04-18 16:05:05

标签: android listview android-intent onclicklistener

每当用户点击"电话图标"和 sms图标打算拨打号码或发送短信。我不知道如何编写OnClickListener代码,以便列表视图中的两个图标响应click事件

MainActivity.java

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

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

        final ArrayList<Student> students = new ArrayList<Student>();

        ListView studentListView = (ListView) findViewById(R.id.list);

        StudentAdapter adapter = new StudentAdapter(this, R.layout.item_list, students);

        studentListView.setAdapter(adapter);

        studentListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {

                Student student = students.get(position);
                String url = student.getStudentNumber();

                           Student student = students.get(position);
            String url = student.getStudentNumber();
            Intent smsIntent = new Intent(android.content.Intent.ACTION_VIEW);
            smsIntent.setType("vnd.android-dir/mms-sms");
            smsIntent.putExtra("address", url);
            smsIntent.putExtra("sms_body", "your desired message");
            startActivity(smsIntent);
                }
            }
        });
    }
}

当我单击ListView项目的任何地方时,此Sms意图正常工作但是 让我们说有一个 带有&#34; sms图标&#34;每个项目中的 R.id.smsButton ,我只希望它响应click事件并启动intent,而不是我的CustomListView项目中的任何其他视图。     `

1 个答案:

答案 0 :(得分:1)

在适配器类本身的getView()方法中编写单击侦听器

@query
"SELECT o FROM Offer o "
+ "JOIN o.attributes a "
+ "LEFT OUTER JOIN o.offers oo "
+ "LEFT OUTER JOIN oo.attributes ooa " <-- referencin JOIN
+ "WHERE o.status = 1 "
+ "AND oo.status = 1 " 
+ "AND a.id = 100 " 
+ "AND ooa.id = 101 " 
+ "GROUP BY o.id "

}