ImageView onClick方法

时间:2015-09-07 19:37:30

标签: java android xml android-layout android-listview

我有一个活动MyActivityListView,她的xml文件是http://pastebin.com/B2thprhT

当我们点击一​​行调用另一个传递元素的活动时,该活动没有什么特别之处。此xml的行是使用其他xml文件http://pastebin.com/H321gRP8构建的。

在活动关联广告中,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,但我不明白在点击时插入关联方法的位置,因为如果我在{{1上插入该方法它们返回一个错误,如果我在MyListViewAdapter类上插入它们会返回错误。 谢谢你,请原谅我的英语,这是我第一次用英语提问:)

1 个答案:

答案 0 :(得分:1)

只需使用findViewById()获取ImageView的引用,就像使用TextView一样,并在代码中设置OnClickListener。在同样获得TextView s的地方执行此操作。