English 中文(简体)
在列表视图中的右侧和机器人右侧添加自定义按钮
原标题:adding custom button in list view on the right side in android
  • 时间:2012-05-28 14:13:32
  •  标签:
  • android

大家好,晚上好

i am trying to create custom list view where in the right side i am adding one button initially i ser the text of button to "ADD", now when i click on that button first time text should change to Remove, now my problem is when i click on Button with ADD text, it set Remove text on diff. positions, like if i click on position 1 then it set Remove text to 6th or 7 and 12th like this,

please help me on this any help is appreciated. my code is here

    // 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;
                  }
问题回答

以下代码用于填充 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" />





相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签