ListView项目clicklistener无法运行android?

时间:2015-02-04 08:50:24

标签: android android-listview onitemclicklistener

我有一个listview,其中包含一个包含6个textviews的自定义适配器。 当我为它的项目点击监听器放置方法时,没有点击listview元素。可能是什么问题?

这是列表元素XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:orientation="vertical" >

<TextView
    android:id="@+id/textViewDealerName"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:text="Medium Text"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/textViewDealerNumber1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:text="Medium Text"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/textViewDealerNumber2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:text="Medium Text"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/textViewDealerAddress"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:text="Medium Text"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/textViewDealerPin"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:text="Medium Text"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/textViewDealerCity"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:text="Medium Text"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/textViewDealerState"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:text="Medium Text"
    android:textAppearance="?android:attr/textAppearanceMedium" />

这是适配器类

public class DealerListAdapter extends BaseAdapter {
Vector<String> dealerName = new Vector<String>();
Vector<String> dealerNum1 = new Vector<String>();
Vector<String> dealerNum2 = new Vector<String>();
Vector<String> dealerAdd = new Vector<String>();
Vector<String> dealerPin = new Vector<String>();
Vector<String> dealerCity = new Vector<String>();
Vector<String> dealerState = new Vector<String>();
private LayoutInflater inflater = null;
Context c;

public DealerListAdapter(Context a, Vector<String> name,
        Vector<String> num1, Vector<String> num2, Vector<String> add,
         Vector<String> pin, Vector<String> city,
        Vector<String> state) {
    this.dealerName = name;
    this.dealerNum1 = num1;
    this.dealerNum2 = num2;
    this.dealerAdd = add;
    this.dealerPin = pin;
    this.dealerCity = city;
    this.dealerState = state;
    this.c = a;
    inflater = (LayoutInflater) a
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
    return dealerName.size();
}

@Override
public Object getItem(int position) {
    return position;
}

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

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    final ViewHolder holder;
    View row = convertView;
    if (row == null) {

        row = inflater.inflate(R.layout.dealer_list_element, null);
        holder = new ViewHolder();
        holder.dName = (TextView) row.findViewById(R.id.textViewDealerName);
        holder.dNum1 = (TextView) row
                .findViewById(R.id.textViewDealerNumber1);
        holder.dNum2 = (TextView) row
                .findViewById(R.id.textViewDealerNumber2);
        holder.dAdd = (TextView) row
                .findViewById(R.id.textViewDealerAddress);
        holder.dPin = (TextView) row.findViewById(R.id.textViewDealerPin);
        holder.dCity = (TextView) row.findViewById(R.id.textViewDealerCity);
        holder.dState = (TextView) row
                .findViewById(R.id.textViewDealerState);

        row.setTag(holder);

    } else {
        holder = (ViewHolder) row.getTag();
    }

    holder.dName.setText("Dealership Name: "
            + dealerName.elementAt(position));
    holder.dNum1.setText("Contact Number: "
            + dealerNum1.elementAt(position));

    holder.dNum2.setText("Contact Number: " + dealerNum2.elementAt(position));
    holder.dNum2.setMovementMethod(LinkMovementMethod.getInstance());
    holder.dAdd.setText("Address: " + dealerAdd.elementAt(position));
    holder.dPin.setText("Pincode: " + dealerPin.elementAt(position));
    holder.dCity.setText("City: " + dealerCity.elementAt(position));
    holder.dState.setText("State: " + dealerState.elementAt(position));

    return row;
}

public final class ViewHolder {

    TextView dName, dNum1, dNum2, dAdd, dPin, dCity, dState;
}

}

我的onitemclicklistener

dealerListView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            // TODO Auto-generated method stub
            TextView pin = (TextView) view
                    .findViewById(R.id.textViewDealerPin);
            Log.e("pin text ",pin.getText().toString());

        }
    });

活动Lyout

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
tools:context="com.vfconect.locator.DealerListActivity" >

<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginStart="10dp" >

        <ListView
            android:id="@+id/listViewDealer"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
        </ListView>
    </RelativeLayout>
</FrameLayout>

<ListView
    android:id="@+id/left_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="@android:color/white"
    android:choiceMode="singleChoice"
    android:divider="#D8D8D8" />

 </android.support.v4.widget.DrawerLayout>

1 个答案:

答案 0 :(得分:0)

通过在listview元素的父布局中添加这些行来解决此问题

 android:baselineAligned="false"
android:descendantFocusability="blocksDescendants"

Listview onItemClickListener not working