0

嗨,大家晚上好,

我正在尝试创建自定义列表视图,在右侧我最初添加一个按钮我将按钮的文本提供给“添加”,现在当我第一次单击该按钮时,文本应该更改为删除,现在我的问题是什么时候我单击带有添加文本的按钮,它在差异上设置了删除文本。位置,例如,如果我单击位置 1,那么它将删除文本设置为第 6 或第 7 和第 12,如下所示,

请在这方面帮助我任何帮助表示赞赏。我的代码在这里

    // TODO Auto-generated method stub
            //      View row = null;
      if (convertView == null) {
            //      if(row==null)
        convertView = mInflater.inflate(R.layout.apps_list, null);

            // Creates a ViewHolder and store references to the two children views
             // we want to bind data to.
            holder = new ViewHolder();
            holder.appName = (TextView) convertView.findViewById(R.id.app_name);
            holder.addRemove = (Button) convertView.findViewById(R.id.add_or_remove);
            holder.cb = (CheckBox)convertView.findViewById(R.id.cb);

            convertView.setTag(holder);
            } else {
           // Get the ViewHolder back to get fast access to the TextView
           // and the ImageView.
           holder = (ViewHolder) convertView.getTag();
           }
    prefsEditor = myPrefs.edit();

     holder.appName.setText(optionalAppsArray.get(position));
     String addOrRemove = holder.addRemove.getText().toString();
     int pos = position;
     holder.addRemove.setOnClickListener(new OnClickListener() {



            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                try {
                    if(!bool){
                        holder.addRemove.setText("Add");
                        bool = true;
                    }else {
                        holder.addRemove.setText("remove");
                        bool = false;
                    }
                } catch (Exception e) {
                    // TODO: handle exception
                }

            }
        });

            return convertView;
                  }
4

1 回答 1

1

下面的代码用于填充 ListView 并将数据绑定到布局

public class AccessoriesListActivity extends ListActivity {

private static final String STAR_STATES = "listviewtipsandtricks:star_states";

private AccessoriesAdapter mAdapter;
private boolean[] mStarStates;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (savedInstanceState != null) {
        mStarStates = savedInstanceState.getBooleanArray(STAR_STATES);
    } else {
        mStarStates = new boolean[CHEESES.length];
    }

    mAdapter = new AccessoriesAdapter();
    setListAdapter(mAdapter);
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putBooleanArray(STAR_STATES, mStarStates);
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    showMessage(getString(R.string.you_want_info_about_format, CHEESES[position]));
}

private static class AccessoriesViewHolder {
    public CheckBox star;
    public TextView content;
}

private class AccessoriesAdapter extends BaseAdapter {

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

    @Override
    public String getItem(int position) {
        return CHEESES[position];
    }

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

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

        AccessoriesViewHolder holder = null;

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

            holder = new AccessoriesViewHolder();
            holder.star = (CheckBox) convertView.findViewById(R.id.btn_star);
            holder.star.setOnCheckedChangeListener(mStarCheckedChanceChangeListener);
            holder.content = (TextView) convertView.findViewById(R.id.content);

            ((Button) convertView.findViewById(R.id.btn_buy)).setOnClickListener(mBuyButtonClickListener);

            convertView.setTag(holder);
        } else {
            holder = (AccessoriesViewHolder) convertView.getTag();
        }

        holder.star.setChecked(mStarStates[position]);
        holder.content.setText(CHEESES[position]);

        return convertView;
    }
}

private void showMessage(String message) {
    Toast.makeText(AccessoriesListActivity.this, message, Toast.LENGTH_SHORT).show();
}

private OnClickListener mBuyButtonClickListener = new OnClickListener() {
    @Override
    public void onClick(View v) {
        // TODO Cyril: Not implemented yet!
    }
};

private OnCheckedChangeListener mStarCheckedChanceChangeListener = new OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
     // TODO Cyril: Not implemented yet!
    }
};
}

此活动的布局是

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="6dp">

<CheckBox
    android:id="@+id/btn_star"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:button="@android:drawable/btn_star" />

<TextView
    android:id="@+id/content"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:layout_weight="1"
    android:ellipsize="end"
    android:paddingLeft="6dp"
    android:paddingRight="6dp"
    android:singleLine="true"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<Button
    android:id="@+id/btn_buy"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:text="@string/buy_it" />

于 2012-05-28T16:28:55.787 回答