2012-05-28 59 views
0

你好晚上好一切,添加自定義按鈕,在列表視圖右側在android系統

我試圖創造一個在右邊我加入一個按鈕,最初我SER按鈕上的文字自定義列表視圖到「ADD」, 現在當我點擊該按鈕的第一次文本應該改爲刪除, 現在我的問題是當我點擊添加文本按鈕時,它設置刪除差異文本。位置,就像如果我點擊位置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; 
        } 

回答

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" /> 

相關問題