无法为ListView选择行(setOnItemClickListener无法正常工作)

时间:2014-01-15 09:32:24

标签: android android-listview

我有一个自定义AlertDialog,其中有一个列表视图。在这个列表视图中,setOnItemClickListener不用担心。

我想选择一行。

主要活动代码:

public class MainActivity extends Activity implements OnClickListener{
private Button btnSearchFrmSrch, btnClearFrmSrch, btnCancelFrmSrch, btnAddFrmSrch;
private ListView lViewFrmSrch;
private ArrayList<SearchItem> arrayList;
private ArrayAdapter<SearchItem> adapterForSearch;
private AlertDialog dialog;

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


public void getAleart(View v){

    LayoutInflater inflater = getLayoutInflater();
    View view = inflater.inflate(R.layout.search_product, null);
    lViewFrmSrch = (ListView) view.findViewById(R.id.lViewFrmSrch2);
    arrayList = new ArrayList<SearchItem>();
    arrayList.add(new SearchItem("123", "123", "ABC", 12.9));
    arrayList.add(new SearchItem("456", "456", "DEF", 99.9));
    arrayList.add(new SearchItem("222", "666", "VVV", 546.2));
    arrayList.add(new SearchItem("111", "666", "CCC", 39.9));

    adapterForSearch = new SearchAdapter(this, R.layout.list_view_search,
            arrayList);

    lViewFrmSrch.setAdapter(adapterForSearch);

    btnSearchFrmSrch = (Button) view.findViewById(R.id.btnSearchFrmSrch);
    btnClearFrmSrch = (Button) view.findViewById(R.id.btnClearFrmSrch);
    btnCancelFrmSrch = (Button) view.findViewById(R.id.btnCancelFrmSrch);
    btnAddFrmSrch = (Button) view.findViewById(R.id.btnAddFrmSrch);

    lViewFrmSrch.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int pos, long arg3) {
            System.out.println("*#*#*#*#*#*#*#");
            System.out.println(parent.getSelectedItem());
        }
    });

    btnSearchFrmSrch.setOnClickListener(this);
    btnClearFrmSrch.setOnClickListener(this);
    btnCancelFrmSrch.setOnClickListener(this);
    btnAddFrmSrch.setOnClickListener(this);

    AlertDialog.Builder alert = new AlertDialog.Builder(this);
    alert.setTitle("Search Product");
    alert.setView(view);
    alert.setCancelable(false);

    dialog = alert.create();
    dialog.show();

}

@Override
public void onClick(View v) {
    switch (v.getId()) {
    case R.id.btnCancelFrmSrch:
        dialog.dismiss();
        break;

    case R.id.btnAddFrmSrch:
    break;
    }

}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
}

适配器代码:

public class SearchAdapter extends ArrayAdapter<SearchItem> {

private final Context context;
private final int rowResourceId;
private final ArrayList<SearchItem> arrayListSearchedItem;

// private final ViewGroup viewGroup;

public SearchAdapter(Context context, int listViewPayment,
        ArrayList<SearchItem> arrayListSearchedItem) {
    super(context, listViewPayment, arrayListSearchedItem);
    System.out.println("*** SearchAdapter() ***");

    this.context = context;
    this.rowResourceId = listViewPayment;
    this.arrayListSearchedItem = arrayListSearchedItem;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rowView = inflater.inflate(rowResourceId, parent, false);
    TextView tViewItemCodeFrmLstViewSrch = (TextView) rowView
            .findViewById(R.id.tViewItemCodeFrmLstViewSrch);
    TextView tViewItemNameFrmLstViewSrch = (TextView) rowView
            .findViewById(R.id.tViewItemNameFrmLstViewSrch);
    TextView tViewPriceFrmLstViewSrch = (TextView) rowView
            .findViewById(R.id.tViewPriceFrmLstViewSrch);

    tViewItemCodeFrmLstViewSrch
            .setText(arrayListSearchedItem.get(position).getStrSkuCode());
    tViewItemNameFrmLstViewSrch
            .setText(arrayListSearchedItem.get(position).getStrProductName());
    tViewPriceFrmLstViewSrch
            .setText(String.valueOf(arrayListSearchedItem.get(position).getDoubleOfrdPrice()));

    return rowView;
}
}

搜索商品代码:

public class SearchItem {
private String strSkuCode;
private String strBarcode;
private String strProductName;
private double doubleOfrdPrice;
public SearchItem(String strSkuCode, String strBarcode,
        String strProductName, double doubleOfrdPrice) {
    super();
    this.strSkuCode = strSkuCode;
    this.strBarcode = strBarcode;
    this.strProductName = strProductName;
    this.doubleOfrdPrice = doubleOfrdPrice;
}
public String getStrSkuCode() {
    return strSkuCode;
}
public String getStrBarcode() {
    return strBarcode;
}
public String getStrProductName() {
    return strProductName;
}
public double getDoubleOfrdPrice() {
    return doubleOfrdPrice;
}
@Override
public String toString() {
    return super.toString();
}
}

布局代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >


<Button 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Click"
    android:onClick="getAleart"
    />

自定义AleartDialog代码:

<?xml version="1.0" encoding="utf-8"?>

<ListView
    android:id="@+id/lViewFrmSrch2"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1" >
</ListView>

<TableLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:shrinkColumns="*"
    android:stretchColumns="*" >

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:paddingBottom="5dp"
        android:paddingTop="5dp" >

        <Button
            android:id="@+id/btnSearchFrmSrch"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="2dp"
            android:layout_marginRight="2dp"
            android:layout_span="10"
            android:text="@string/search"
            android:textSize="13sp" />

        <Button
            android:id="@+id/btnClearFrmSrch"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="2dp"
            android:layout_marginRight="2dp"
            android:layout_span="10"
            android:text="@string/clear"
            android:textSize="13sp" />

        <Button
            android:id="@+id/btnCancelFrmSrch"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="2dp"
            android:layout_marginRight="2dp"
            android:layout_span="10"
            android:text="@string/cancel"
            android:textSize="13sp" />

        <Button
            android:id="@+id/btnAddFrmSrch"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="2dp"
            android:layout_marginRight="2dp"
            android:layout_span="10"
            android:text="@string/add"
            android:textSize="13sp" />
    </TableRow>
</TableLayout>

自定义列表视图代码:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#BDBDBD"
android:shrinkColumns="*"
android:stretchColumns="*" >

<TableRow
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginBottom="10dp"
    android:layout_marginTop="10dp"
    android:gravity="center" >

    <TextView
        android:id="@+id/tViewItemCodeFrmLstViewSrch"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_span="10"
        android:gravity="left|center_vertical"
        android:maxLines="1"
        android:scrollHorizontally="true"
        android:text="@string/item"
        android:textColor="@color/black" />

    <HorizontalScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_span="22" >

        <TextView
            android:id="@+id/tViewItemNameFrmLstViewSrch"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ellipsize="marquee"
            android:focusable="false"
            android:focusableInTouchMode="true"
            android:gravity="left|center_vertical"
            android:marqueeRepeatLimit="marquee_forever"
            android:scrollHorizontally="true"
            android:singleLine="true"
            android:textColor="@color/black" />
    </HorizontalScrollView>

    <TextView
        android:id="@+id/tViewPriceFrmLstViewSrch"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_span="8"
        android:gravity="right|center_vertical"
        android:textColor="@color/black" />
</TableRow>

0 个答案:

没有答案