【问题标题】:Android -Stuck with ListView ItemAndroid - 卡在 ListView 项目上
【发布时间】:2013-04-04 15:10:43
【问题描述】:

我正在开发一个 Android 应用程序。在该应用程序中,我设置了一个列表视图,每个视图都有一个 Imageview、Progressbar(InVisible) 和两个按钮。

plAdapter = new PlayAdapter();
         lv1.setAdapter(plAdapter); 
}

private static class AccessoriesViewHolder {
Button play,download;
ProgressBar pb;

}

private class PlayAdapter extends BaseAdapter {

    @Override
    public int getCount() {
        return contactList.toArray().length;
    }

    @Override
    public String getItem(int position) {
        return a;
        //return al[position];
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {


         holder = null;

        if (convertView == null) {
            convertView = getLayoutInflater().inflate(R.layout.design, parent, false);

            holder = new AccessoriesViewHolder();
            holder.play = (Button) convertView.findViewById(R.id.btn_play);
            holder.download = (Button) convertView.findViewById(R.id.btn_dwnld);

            ((Button) convertView.findViewById(R.id.btn_play)).setOnClickListener(mBuyButtonClickListener);
            ((Button) convertView.findViewById(R.id.btn_dwnld)).setOnClickListener(mBuyButtonClickListener);
           ((ProgressBar) convertView.findViewById(R.id.progress_bar)).setVisibility(View.INVISIBLE);

           // convertView.setTag(holder);
        } else 

        {

            holder = (AccessoriesViewHolder) convertView.getTag();
        }


        /*
         * The Android API provides the OnCheckedChangeListener interface
         * and its onCheckedChanged(CompoundButton buttonView, boolean
         * isChecked) method. Unfortunately, this implementation suffers
         * from a big problem: you can't determine whether the checking
         * state changed from code or because of a user action. As a result
         * the only way we have is to prevent the CheckBox from callbacking
         * our listener by temporary removing the listener.
         */


        return convertView;
    }
}

/**
 * Quickly shows a message to the user using a {@link Toast}.
 * 
 * @param message The message to show
 */
private void showMessage(String message) {
    Toast.makeText(MainActivity.this, message, Toast.LENGTH_SHORT).show();
}

private OnClickListener mBuyButtonClickListener = new OnClickListener() {
    @Override
    public void onClick(View v) {
        final int position = lv1.getPositionForView(v);

        // int dummy=position;
            showMessage(listsong[position].toString()+"");


            ((ProgressBar) convertView.findViewById(R.id.progress_bar)).setVisibility(View.INVISIBLE);

    }
};

protected AdapterView<ListAdapter> getListView() 
{
    // TODO Auto-generated method stub
    return null;
}

}

如果我连续单击其中一个 Button,则进度条必须在该行中可见。

问题是,如果我点击一个按钮,其他行上的进度条会变得可见但不对应的行。

【问题讨论】:

  • Rakesh,你得修改getView的逻辑。创建布尔数组作为数据,并在列表项上完成单击时的 OnClickListener 获取索引,如果该元素编号在布尔数组中设置为 true,则会在 getViewMethod 上显示进度条。

标签: java android


【解决方案1】:

只需在 ListView 适配器的 getView() 方法中为按钮添加一个 OnClickListener 并在 OnClickListener 中设置 ProgressBar 的可见性

【讨论】:

    【解决方案2】:

    尝试像这样更改您的 getView 方法:

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    
    
         holder = new AccessoriesViewHolder();
    
         convertView = getLayoutInflater().inflate(R.layout.design, parent, false);
    
         holder.play = (Button) convertView.findViewById(R.id.btn_play);
         holder.download = (Button) convertView.findViewById(R.id.btn_dwnld);
    
         ((Button) convertView.findViewById(R.id.btn_play)).setOnClickListener(mBuyButtonClickListener);
         ((Button) convertView.findViewById(R.id.btn_dwnld)).setOnClickListener(mBuyButtonClickListener);
         ((ProgressBar) convertView.findViewById(R.id.progress_bar)).setVisibility(View.INVISIBLE);
    
         return convertView;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多