gpt4 book ai didi

安卓微调页脚

转载 作者:行者123 更新时间:2023-11-29 14:34:04 25 4
gpt4 key购买 nike

有没有办法在微调器下拉列表中添加页脚?例如,假设我正在从数据库中填充微调器,但无论我如何排列列表,我都希望在列表底部进行选择。该列表可能是“数据库项目 1、2..、3...”,并且在列表底部的页脚选项是“将项目添加到数据库”。

到目前为止,我已经按如下方式使用了您的 CustomSpinner 类:

public class CustomSpinner extends Spinner implements OnItemClickListener {

private AlertDialog myDialog = null;
private OnClickListener myButtonClickListener = null;
private GrainSpinnerAdapter adapter = null;

public CustomSpinner(Context context, AttributeSet attrs) {
super(context, attrs);
}

public void setButtonClickListener(OnClickListener listener) {
myButtonClickListener = listener;
}

public void setAdapter(GrainSpinnerAdapter adapter){
this.adapter = adapter;
}

@Override
public boolean performClick() {
Context context = getContext();

//Inflate the layout
final LayoutInflater inflater = LayoutInflater.from(getContext());
final View v = inflater.inflate(R.layout.my_custom_spinner, null);

// set up list view
final ListView lv = (ListView) v.findViewById(R.id.list);
lv.setAdapter(adapter);
lv.setSelection(getSelectedItemPosition());
lv.setOnItemClickListener(this);

// set up button
final Button btn = (Button) v.findViewById(R.id.addButton);
btn.setOnClickListener(myButtonClickListener);

// build our dialog
AlertDialog.Builder builder = new AlertDialog.Builder(context);

// show prompt, just as our Spinner parent does
if (getPrompt() != null) {
builder.setTitle(getPrompt());
}

// create and show dialog
myDialog = builder.setView(v).create();
myDialog.show();

return true;
}

@Override
public void onItemClick(AdapterView<?> view, View itemView, int position, long id) {
setSelection(position);
if (myDialog != null) {
myDialog.dismiss();
myDialog = null;
}

}

}我想使用像这样的单独适配器:

public class GrainListAdapter extends SimpleCursorAdapter {

private static final String DEFAULT_UNITS = "American";
private Button upButton;
private Context myContext;
private RecipeGrainActivity parentActivity;
private Button downButton;
private String units;
private double getLbs;

public GrainListAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
super(context, layout, c, from, to);
myContext = context;
parentActivity = (RecipeGrainActivity) myContext;

//Checks for metric pref.
SharedPreferences myPrefs = PreferenceManager.getDefaultSharedPreferences(context);
units = String.valueOf(myPrefs.getString(context.getString(R.string.pref_measurement), DEFAULT_UNITS));
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
int idColumn = cursor.getColumnIndex("_id");
final int getId = cursor.getInt(idColumn);
final double increment = 0.25;

UnitsConversions convert = new UnitsConversions();

int nameColumn = cursor.getColumnIndex("name");
String getName = cursor.getString(nameColumn);
TextView name = (TextView)view.findViewById(R.id.GrainName);
name.setText(getName);

int originColumn = cursor.getColumnIndex("origin");
String getOrigin = cursor.getString(originColumn);
TextView origin = (TextView)view.findViewById(R.id.GrainOrigin);
origin.setText(getOrigin);

if(units.equals("Metric")){
//Sets labels to metric.
String kilos = context.getResources().getString (R.string.kilograms);
TextView weightLabel = (TextView)view.findViewById(R.id.GrainLbsLabel);
weightLabel.setText(kilos);
}


}

@Override
public View newView(Context context, Cursor cursor, final ViewGroup parent) {
View view = View.inflate(context, R.layout.grain_list_item, null);
return view;
}

这允许我为微调器中的每个列表项构建一个自定义行。我在想我可以在我的 Activity 中使用 customspinner.setadapter() 将适配器设置为 CustomSpinner。但是,CustomSpinner 类中定义的 ListView 使用类中引用的适配器。如何将我的适配器传递给类以便它可以使用它?

这是我必须将自定义项目添加到我的布局中的方法:

<com.bluelightuniverse.android.brewmobile.CustomSpinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/GrainNameSpinner"
android:layout_toRightOf="@id/GrainOriginSpinner"
android:layout_toLeftOf="@+id/AddGrainButton">
</com.bluelightuniverse.android.brewmobile.CustomSpinner>

最佳答案

在您的 CursorAdapter 中,覆盖方法 getCount() 和 getDropDownView:例如,如果您想在末尾放置一个按钮,您可以这样做:

new SimpleCursorAdapter(...) {

@Override
public int getCount() {
return super.getCount()+1;
}

@Override
public View getDropDownView(int position, View convertView,
ViewGroup parent) {
View res=null;
if (position == getCount()-1) {
//better not try to reuse our view as it can be requested for the spinner or by the list with different kind of layouts.
View mButton= getActivity().getLayoutInflater().inflate(R.layout.create_activity_button, parent,false);
mButton.setOnClickListener(ObservationListFragment.this);
res= mButton;
} else if (convertView != null && android.R.id.button1 == convertView.getId() ){
// If the adapter is trying to recycle our footer view, we force the generation of a new view (check on the id of our custom view)
res= super.getDropDownView(position, null, parent);
} else {
res= super.getDropDownView(position, convertView, parent);
}
return res;
}
};

关于安卓微调页脚,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7005265/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com