大家晚上好,
我正在尝试创建自定义列表视图在右侧我最初添加一个按钮我将按钮文本添加到“ADD”, 现在,当我点击该按钮时,第一次文本应该更改为Remove, 现在我的问题是,当我点击带有ADD文本的Button时,它设置了差异删除文本。位置,就像我点击位置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;
}
答案 0 :(得分: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" />