java - ImageView的onClick方法

标签 java android xml android-layout android-listview

我有一个 Activity MyActivityListView她的 xml 文件是这个 http://pastebin.com/B2thprhT

该 Activity 没有任何特殊作用,当我们单击一行时,会调用另一个传递元素的 Activity 。此 xml 的行是使用另一个 xml 文件 http://pastebin.com/H321gRP8 构建的.

在与广告相关的 Activity 中,onCreate() 上的第一个 xml 文件方法显示这一行:listView.setAdapter(new MyListViewAdapter(getApplicationContext()));MyListViewAdapter是这样制作的

public class MyListViewAdapter extends BaseAdapter {
    List<TestTable> testTableArrayList;
    private LayoutInflater inflater;
    private Context context;
    private DatabaseHelper databaseHelper;

    public MyListViewAdapter(/*ArrayList<TestTable> testTableArrayList,*/ Context context) {
//        this.testTableArrayList = testTableArrayList;
        this.context = context;
        this.databaseHelper = new DatabaseHelper(context);
        this.testTableArrayList = databaseHelper.getValues();
        inflater = ( LayoutInflater )context.
                getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    /**
     * Ritorna il numero degli elementi presenti nell'ArrayList
     * @return numero di elementi nell'ArrayList
     */
    @Override
    public int getCount() {
//        return 0;
        return testTableArrayList.size();
    }

    /**
     * Restituisce l'oggetto in posizione position
     * @param position = posizione dell'oggetto che verra' restituito
     * @return oggetto in posizione position
     */
    @Override
    public Object getItem(int position) {
//        return null;
        return testTableArrayList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
//        LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View row = inflater.inflate(R.layout.custom_single_row_list_view, null);

        TextView zero = (TextView) row.findViewById(R.id.txtIdOnCustomView);
        TextView uno = (TextView) row.findViewById(R.id.txtPrimoValoreOnCustomView);
        TextView due = (TextView) row.findViewById(R.id.txtSecondoValoreOnCustomView);
        TextView tre = (TextView) row.findViewById(R.id.txtTerzoValoreOnCustomView);

        TestTable temp = testTableArrayList.get(position);

        zero.setText(String.valueOf(temp.getId()));
        uno.setText(temp.getVal1());
        due.setText(temp.getVal2());
        tre.setText(temp.getVal3());

//        TextView uno

        return row;
    }
}`

如您所见,第二个 xml 文件中存在 ImageView这是可点击的,但我不明白在哪里插入与点击关联的方法,因为如果我在 MyListViewAdapter 上插入该方法如果我插入 ActivityListView 类,它们会返回错误类他们返回错误。 非常感谢并原谅我的英语,这是我第一次用英语问问题:)

最佳答案

只需使用 findViewById()获取您的ImageView的引用就像您对 TextView 所做的那样s 并设置 OnClickListener在代码中。在您获得 TextView 的同一位置执行此操作。 s。

关于java - ImageView的onClick方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32445069/

相关文章:

java - Apache Camel 和 Blueprint,如何在另一个包中找到一条路线?

java - Windows Vista 中的图像对象 (Java)

java - 无法将 StAX 输入转换为 DOM DocumentFragment

java - BuildConfig 多个 dex 文件错误 (Gradle 3.0.1)

android - 如何对齐底部的 Include?

xml - XSL 转换因多个模板而失败

java - 在板上移动 Snake 的 body 会导致 body 结 block

android - 操作栏不显示操作 View 图标

xml - XPath 祖先或自身解析顺序

java - 呈现期间引发的异常:android.support.design.widget.AppBarLayout无法强制转换为android.preference.Preference