滚动时Listview项目选择错误

时间:2013-08-12 12:04:19

标签: android listview android-listview scroll

我有一个多栏列表视图,显示股票列出公司。 代码适用于处理小数据。 (它们适合屏幕,不需要滚动。) 但库存数量不能适合屏幕。发生以下问题。

1。当列表项的详细信息展开并滚动时,也会选择其他项目。 (例如,我扩展3.stock,它选择15.stock和27.stock和39.stock等。)

http://i.stack.imgur.com/CLRSJ.jpg

2. 另一个问题是细节布局。通常它有两个选项卡,但它显示太多选项卡。

http://i.stack.imgur.com/E2MyX.png

这是我的适配器类:

    public class StockListAdapter extends BaseAdapter
{
    public ArrayList<HashMap<String, String>> list;
    Activity activity;
    public ArrayList<EditText> editTextListMiktar = new ArrayList<EditText>();
    public ArrayList<String> stockNoList = new ArrayList<String>();
    public ArrayList<LinearLayout> stockLayoutList = new ArrayList<LinearLayout>();
    private String stokNo;
    private Context context;
    private boolean isDetail;
    private String birim;
    private String processType;
    private String processNo;
    private Object kodTuru;
    private boolean clearAlsaDeta;
    public static String x;

    public StockListAdapter(Activity activity, ArrayList<HashMap<String, String>> list, Context context,String kodTuru, String processNo, String processType, boolean clearAlsaDeta) {
        super();
        this.activity = activity;
        this.list = list;
        this.context = context;
        this.kodTuru = kodTuru;
        this.processNo = processNo;
        this.processType = processType;
        this.clearAlsaDeta = clearAlsaDeta;
    }

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

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

    @Override
    public long getItemId(int position)
    {
        // TODO Auto-generated method stub
        return 0;
    }

    static class ViewHolder
    {
        ImageView stockDetail;
        TextView stockNo;
        TextView stockCode;
        TextView stockName;
        Spinner stockBirim;
        TextView stockKdv;
        EditText stockMiktar;
        LinearLayout linearLayoutStockBase;
        LinearLayout linearLayoutStockDetail;
        TabHost TabHostStockDetail;
        ListView listviewDetailFiyat;
        ListView listviewDetailStok;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        final ViewHolder holder;
        LayoutInflater inflater = activity.getLayoutInflater();

        if (convertView == null)
        {
            convertView = inflater.inflate(R.layout.stocklistitems, null);
            holder = new ViewHolder();
            holder.linearLayoutStockBase = (LinearLayout) convertView.findViewById(R.id.linearLayoutStockBase);
            stockLayoutList.add(holder.linearLayoutStockBase);
            holder.stockDetail = (ImageView) convertView.findViewById(R.id.imageViewStockDetail);
            holder.stockNo = (TextView) convertView.findViewById(R.id.textViewStockNo);
            holder.stockCode = (TextView) convertView.findViewById(R.id.textViewStockCode);
            holder.stockName = (TextView) convertView.findViewById(R.id.textViewStockName);
            holder.stockBirim = (Spinner) convertView.findViewById(R.id.spinnerStockBirim);
            holder.stockKdv = (TextView) convertView.findViewById(R.id.textViewStockKdv);
            holder.stockMiktar = (EditText) convertView.findViewById(R.id.editTextStockMiktar);
            editTextListMiktar.add(holder.stockMiktar);
            holder.linearLayoutStockDetail = (LinearLayout) convertView.findViewById(R.id.linearLayoutStockDetails);
            holder.TabHostStockDetail = (TabHost) convertView.findViewById(R.id.tabHostStokList);
            holder.listviewDetailFiyat = (ListView) convertView.findViewById(R.id.listViewStoklistDetailFiyat);
            holder.listviewDetailStok = (ListView) convertView.findViewById(R.id.listViewStoklistDetailStok);
            holder.stockMiktar.setText("");
            // setMiktar(stokNo, processNo, holder.stockMiktar,
            // holder.linearLayoutStockBase);
            convertView.setTag(holder);
        } else
        {
            holder = (ViewHolder) convertView.getTag();
        }

        final HashMap<String, String> map = list.get(position);
        holder.stockNo.setText(map.get("StokNo"));
        stokNo = map.get("StokNo");
        stockNoList.add(stokNo);
        holder.stockCode.setText(map.get("StokCode"));
        holder.stockName.setText(map.get("StokName"));
        holder.stockKdv.setText(map.get("StokKdv"));
        holder.stockBirim.setAdapter(TableUtils.SpinnerAdapter(context, "SELECT BIRIM FROM STOKBIRI WHERE STOK_NO=" + stokNo, null));
        Functions.selectPrefItemsToSpinner(holder.stockBirim, map.get("StokBirim"));
        Functions.setSpinnerEnable(holder.stockBirim);
        holder.stockMiktar.setText("");

        holder.stockMiktar.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_CLASS_PHONE);

        // tab detail kısmının eklenmesi
        tabDetailSetup(stokNo, holder.TabHostStockDetail, holder.listviewDetailFiyat, holder.listviewDetailStok);

        // detay göster
        holder.stockDetail.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                if (!isDetail)
                {
                    holder.linearLayoutStockDetail.setVisibility(View.VISIBLE);
                    holder.stockDetail.setBackgroundResource(R.drawable.box_delete_expand);
                    isDetail = true;
                } else
                {
                    holder.linearLayoutStockDetail.setVisibility(View.GONE);
                    holder.stockDetail.setBackgroundResource(R.drawable.box_plus);
                    isDetail = false;
                }
            }
        });

        holder.linearLayoutStockBase.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                if (!isDetail)
                {
                    holder.linearLayoutStockDetail.setVisibility(View.VISIBLE);
                    holder.stockDetail.setBackgroundResource(R.drawable.box_delete_expand);
                    isDetail = true;
                } else
                {
                    holder.linearLayoutStockDetail.setVisibility(View.GONE);
                    holder.stockDetail.setBackgroundResource(R.drawable.box_plus);
                    isDetail = false;
                }

            }
        });

        holder.stockMiktar.addTextChangedListener(new TextWatcher()
        {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count)
            {
                map.put("StokMiktar", s.toString());
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after)
            {
                // TODO Auto-generated method stub
            }

            @Override
            public void afterTextChanged(Editable s)
            {
                map.put("StokMiktar", s.toString());
            }
        });

        holder.stockBirim.setOnItemSelectedListener(new OnItemSelectedListener()
        {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long arg3)
            {
                birim = holder.stockBirim.getItemAtPosition(position).toString();
                map.put("StokBirim", birim);

            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0)
            {

            }
        });

        return convertView;
    }

        public void tabDetailSetup(String stokNo, TabHost th, ListView lsFiyat, ListView lsStok)
    {
        th.setup();
        TabSpec spec1 = th.newTabSpec("Tab 1");
        spec1.setContent(R.id.tabStoklistFiyat);
        spec1.setIndicator("Fiyatlar");
        TabSpec spec2 = th.newTabSpec("Tab 2");
        spec2.setIndicator("Stoklar");
        spec2.setContent(R.id.tabStoklistStok);
        th.addTab(spec1);
        th.addTab(spec2);
...      }

1 个答案:

答案 0 :(得分:0)

第一个问题是您没有设置linearLayoutStockDetail的可见性。用于行的android重用视图中的ListView。当您打开第3行的详细视图并滚动底部时 - android重用您的行号3.您为文本字段设置了新值,但不隐藏详细信息视图。

我想,第二个问题是一样的。您没有显示tabDetailSetup方法,但我认为您每次都会添加标签。您需要先清除旧标签。

请记住,当您在适配器中使用convertView时,您可以使用自己的属性获取旧视图,这是您上次设置的。