Android - ListView with 2 textView' s得到哪一个被点击

时间:2014-12-08 03:36:07

标签: java android listview android-arrayadapter

我有一个列表视图,我在其中设置了2个textview,在右边,一个在左边,我想为它们设置一个onClickListerner并获取一个onClick的文本单击了,以及单击的列表视图的行。

我已经能够设置获取被点击的行,但现在我需要帮助找出行中哪个文本视图被点击。

以下是我在列表视图中为单元格设置的内容:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="text1"
        android:id="@+id/1"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"

        android:textColor="#ffffffff" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="text2"
        android:id="@+id/2"
        android:minHeight="60dp"
        android:textSize="18dp"
        android:gravity="center"
 />

</RelativeLayout>

这是我的MainActivity.java,其中包含设置列表视图以及到目前为止我尝试过的内容。

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

    populateCarList();
    populateListView();
    registerClickCallback();

    private void populateCarList() {
    userDiningHall.add(new DiningHall("hall1", open1));
    userDiningHall.add(new DiningHall("hall2", open2));
    userDiningHall.add(new DiningHall("hall3", open3));
    userDiningHall.add(new DiningHall("hall4", open4));
}

private void populateListView() {
    ArrayAdapter<DiningHall> adapter = new MyListAdapter();
    ListView list = (ListView)findViewById(R.id.homeListView);
    list.setAdapter(adapter);
}

//THIS GETS WHAT ROW WAS CLICKED

private void registerClickCallback() {
    ListView list = (ListView)findViewById(R.id.homeListView);
    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent,View ViewClicked, int position, long id) {
            DiningHall clickedDiningHall = userDiningHall.get(position);
            String message = "You clicked position " + position + " which is dining hall " +
                    clickedDiningHall.getDiningHallName();
            Toast.makeText(MainActivity.this, message, Toast.LENGTH_LONG).show();

        }
    });


}

private class MyListAdapter extends ArrayAdapter<DiningHall> {
    public MyListAdapter() {
        super(MainActivity.this, R.layout.home_item_view, userDiningHall);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View itemView = convertView;
        if (itemView == null) {
            itemView = getLayoutInflater().inflate(R.layout.home_item_view, parent, false);
        }
        //Find the dining hall
        DiningHall currentDiningHall = userDiningHall.get(position);

        //Fill the view
        TextView diningHallText = (TextView)itemView.findViewById(R.id.itemDiningHallTextView);
        diningHallText.setText(currentDiningHall.getDiningHallName());

        TextView openText = (TextView)itemView.findViewById(R.id.itemOpenTextView);
        openText.setText(currentDiningHall.getDiningHallOpen());


        return itemView;
    }
}

如何获取点击的行以及点击的文本视图?

提前感谢您的帮助。 :)

2 个答案:

答案 0 :(得分:3)

您可以为View.OnClickListener设置相同的TextView。然后,只需通过调用View检查onClick(View v)中点击的v.getId()的ID。

答案 1 :(得分:0)

你可以为两个TextViews设置简单的setOnclickListener,也可以在listview中设置OnItemclickListener