- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
如何将 Spinner
插入到 Fragment
的 onCreateView
中?
如果我在下面写一个 Spinner
:
ViewGroup rootView = (ViewGroup)inflater.inflate(R.layout.fragment_list2, container, false);
它会导致 Spinner
的布局不正确,但它是 PlanetAdapter
中对象的正确布局。
通过这种方式,我在 ListView 下有微调器,而我希望微调器进入 ListView 的行 View 。 Spinner
进入另一个名为:single_listview_item
的布局。
您可以在下面看到我到目前为止所做的尝试:
fragment :
public class MyListFragment extends Fragment implements
android.widget.CompoundButton.OnCheckedChangeListener,AdapterView.OnItemSelectedListener {
ListView lv;
ArrayList<Planet> planetList;
static PlanetAdapter plAdapter;
private TextView txtName;
private TextView txtEmail;
private Button btnLogout;
private Spinner spinner1;
private Spinner spinner2;
Spinner spinner;
private SQLiteHandler db;
private SessionManager session;
BirraAdapter biAdapter;
PlanetAdapter.PlanetHolder holder;
private static Context context = null;
private static FragmentActivity mInstance;
Integer[] imageId = {
R.mipmap.androtuto,
R.mipmap.ic_launcher,
R.mipmap.ic_launcher,
R.mipmap.ic_launcher,
R.mipmap.ok,
/*R.drawable.image6,
R.drawable.image7*/
};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the custom_spinner_items for this fragment
//super.onDestroy();
SharedPreferences settings = getContext().getSharedPreferences("states", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.clear();
editor.commit();
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_list2, container, false);
spinner = (Spinner) rootView.findViewById(R.id.simpleSpinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this.getContext(),
R.array.country_arrays, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
context = getActivity();
mInstance = getActivity();
txtName = (TextView) rootView.findViewById(R.id.name);
txtEmail = (TextView) rootView.findViewById(R.id.numero_telefonico);
btnLogout = (Button) rootView.findViewById(R.id.btnLogout);
// SqLite database handler
db = new SQLiteHandler(context.getApplicationContext());
// session manager
session = new SessionManager(context.getApplicationContext());
/*
if (!session.isLoggedIn()) {
logoutUser();
}
*/
// Fetching user details from SQLite
HashMap<String, String> user = db.getUserDetails();
String name = user.get("name");
//String email = user.get("email");
// String email = user.get("email");
String email = user.get("numero_telefonico");
// Displaying the user details on the screen
System.out.println(name + email);
txtName.setText(name);
txtEmail.setText(email);
/* Button mButton = (Button) rootView.findViewById(R.id.button);
mButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showResult(v);
}
});*/
//return inflater.inflate(R.custom_spinner_items.fragment_list2, container, false);
btnLogout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
logoutUser();
}
});
/* Spinner spinner = (Spinner) getView().findViewById(R.id.simpleSpinner);
ArrayAdapter<String> adapter_state = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, state);
adapter_state
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter_state);
spinner.setOnItemSelectedListener(this);*/
return rootView;
}
星球适配器:
public abstract class PlanetAdapter extends ArrayAdapter<Planet> implements CompoundButton.OnCheckedChangeListener
{
private List<Planet> planetList = null;
private Context context = null;
ArrayList<Birra> objects;
public boolean Checked;
private final Integer[] imageId;
public PlanetAdapter(List<Planet> planetList, Context context,Integer[] imageId) {
super(context, R.layout.single_listview_item, planetList);
this.planetList = planetList;
this.context = context;
this.imageId = imageId;
}
public class PlanetHolder {
public TextView planetName;
public TextView distView;
public TextView valuta;
public CheckBox chkBox;
public EditText edit;
public TextView caratteristica;
public boolean checked;
public TextView id;
public ImageView imageView;
public Spinner spinner;
}
@Override
public int getCount() {
return planetList.size();
}
@Override
public Planet getItem(int position) {
return planetList.get(position);
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View row = convertView;
PlanetHolder holder = null;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(R.layout.single_listview_item, parent, false);
holder = new PlanetHolder();
holder.planetName = (TextView) row.findViewById(R.id.name);
holder.distView = (TextView) row.findViewById(R.id.dist);
holder.valuta = (TextView) row.findViewById(R.id.valuta);
holder.chkBox = (CheckBox) row.findViewById(R.id.chk_box);
holder.edit = (EditText) row.findViewById(R.id.editText);
holder.spinner = (Spinner) row.findViewById(R.id.simpleSpinner);
holder.caratteristica=(TextView) row.findViewById(R.id.caratteristica);
holder.imageView = (ImageView) row.findViewById(R.id.ivImage);
holder.edit.setVisibility(View.GONE);
holder.edit.setEnabled(false);
holder.spinner.setVisibility(View.GONE);
// holder.id = (TextView) row.findViewById(R.id.id);
row.setTag(holder);
} else {
holder = (PlanetHolder) row.getTag();
}
final Planet p = planetList.get(position);
holder.imageView.setImageResource(imageId[position]);
holder.planetName.setText(p.getName());
holder.distView.setText("" + p.getDistance());
holder.valuta.setText("" + p.getValuta());
holder.chkBox.setChecked(p.isSelected());
holder.chkBox.setTag(p);
holder.caratteristica.setText(p.getCaratteristica());
holder.edit.setEnabled(false);
SharedPreferences states = getContext().getSharedPreferences("states", Context.MODE_PRIVATE);
boolean isChecked = states.getBoolean("holder.chkBox" + holder.planetName.getText().toString(), false);
System.out.println(isChecked);
if (isChecked) {
holder.chkBox.setChecked(true);
holder.edit.setVisibility(View.VISIBLE);
holder.edit.setEnabled(true);
holder.spinner.setVisibility(View.VISIBLE);
SharedPreferences statess = getContext().getSharedPreferences("states", Context.MODE_PRIVATE);
String string = statess.getString("finalHolder.edit" + holder.planetName.getText().toString(), holder.edit.getText().toString().trim());
holder.edit.setText(string);
} else {
holder.chkBox.setChecked(false);
holder.edit.setVisibility(View.GONE);
holder.edit.setEnabled(false);
holder.spinner.setVisibility(View.GONE);
}
holder.chkBox.setOnCheckedChangeListener(PlanetAdapter.this);
// final BirraHolder finalHolder = birraHolder;
final PlanetHolder finalHolder = holder;
holder.chkBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (finalHolder.chkBox.isChecked()) {
finalHolder.edit.setVisibility(View.VISIBLE);
finalHolder.edit.setEnabled(true);
finalHolder.spinner.setVisibility(View.VISIBLE);
SharedPreferences states = getContext().getSharedPreferences("states", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = states.edit();
editor.putBoolean("holder.chkBox" + finalHolder.planetName.getText().toString(), true);
editor.commit();
finalHolder.edit.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
p.setQuantità(finalHolder.edit.getText().toString().trim());
SharedPreferences states = getContext().getSharedPreferences("states", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = states.edit();
editor.putString("finalHolder.edit" + finalHolder.planetName.getText().toString(), finalHolder.edit.getText().toString().trim());
editor.commit();
}
});
/* SharedPreferences states = getContext().getSharedPreferences("states", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = states.edit();
editor.putBoolean("holder.chkBox" + finalHolder.planetName.getText().toString(), true);
editor.commit();*/
//Utility.putPizzaItem(p);
//Utility.getPizzaItem(p);
} else {
finalHolder.edit.setVisibility(View.GONE);
finalHolder.edit.setEnabled(false);
finalHolder.edit.setText("");
finalHolder.spinner.setVisibility(View.GONE);
}
}
});
/*holder.chkBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (finalHolder.chkBox.isChecked()) {
finalHolder.edit.setVisibility(View.VISIBLE);
finalHolder.edit.setEnabled(true);
SharedPreferences states = getContext().getSharedPreferences("states", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = states.edit();
editor.putBoolean("holder.chkBox", true);
//pizzaSelected.put(p.getId(), p);
System.out.println(p.getId());
} else {
finalHolder.edit.setVisibility(View.GONE);
finalHolder.edit.setEnabled(false);
finalHolder.edit.setText(null);
pizzaSelected.remove(p.getId());
}
}
});
*/
/* finalHolder.edit.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
p.setQuantità(finalHolder.edit.getText().toString().trim());
SharedPreferences states = getContext().getSharedPreferences("states", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = states.edit();
editor.putString("finalHolder.edit" + finalHolder.planetName.getText().toString(), finalHolder.edit.getText().toString().trim());
editor.commit();
}
});
*/
// holder.planetName.setText(p.getName());
// holder.distView.setText("" + p.getDistance());
// holder.valuta.setText("" + p.getValuta());
// holder.chkBox.setChecked(p.isSelected());
// holder.chkBox.setTag(p);
// holder.edit.setEnabled(false);
// holder.id.setId(p.getId());
return row;
}
ArrayList<Planet> getBox() {
ArrayList<Planet> box = new ArrayList<Planet>();
for (Planet p : planetList) {
if (p.isSelected())
box.add(p);
}
return box;
}
}
FRAGMENT_LIST2:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1"
android:layout_marginLeft="5dp"
><!--android:onClick="showResult"-->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:gravity="center"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/welcome"
android:textSize="20dp"
android:layout_marginLeft="5dp"
/>
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:textColor="@color/lbl_name"
android:textSize="24dp"
android:layout_marginLeft="5dp"
/>
<Button
android:id="@+id/btnLogout"
android:layout_width="122dp"
android:layout_height="wrap_content"
android:layout_marginRight="40dip"
android:background="@color/btn_logut_bg"
android:text="@string/btn_logout"
android:textAllCaps="false"
android:textColor="@color/white"
android:textSize="15dp"
android:layout_marginLeft="5dp"
/>
</LinearLayout>
<TextView
android:id="@+id/numero_telefonico"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/lbl_name"
android:textSize="13dp" />
<ListView
android:id="@+id/listview"
android:layout_width="fill_parent"
android:layout_height="400dp">
</ListView>
<Spinner
android:id="@+id/simpleSpinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:entries="@array/country_arrays"/>
<!-- <Button
android:id="@+id/button"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:onClick="showResult"
android:text="get_answer">
</Button>-->
</LinearLayout>
SINGLE_LISTVIEW_ITEM:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<CheckBox
android:id="@+id/chk_box"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true" />
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:orientation="vertical"
android:layout_weight="1" >
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/chk_box"
android:textStyle="bold"
android:textColor="#000000" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/caratteristica"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/name"
android:layout_toRightOf="@id/chk_box"
android:textStyle="italic" />
</LinearLayout>
<!-- <TextView
android:id="@+id/id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:textSize="12sp"
android:textStyle="italic"/>-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/dist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/caratteristica"
android:layout_toRightOf="@id/chk_box"
android:textSize="12sp"
android:textStyle="italic" />
<TextView
android:id="@+id/valuta"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:textSize="12sp"
android:textStyle="italic" />
</LinearLayout>
<EditText
android:layout_width="196dp"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="@+id/editText"
android:hint="quantità"
>
</EditText>
<Spinner
android:id="@+id/simpleSpinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:entries="@array/country_arrays"/>
</LinearLayout>
<ImageView
android:id="@+id/ivImage"
android:layout_width="72dp"
android:layout_height="72dp"
android:layout_marginRight="5dp"
android:layout_gravity="center"
/>
</LinearLayout>
我的列表适配器:
public class MyListAdapter extends ArrayAdapter implements AdapterView.OnItemSelectedListener {
Spinner spinner;
/*public MyListAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
}*/
public MyListAdapter(Context context) {
super(context, R.layout.single_listview_item);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
System.out.println("ciao");
spinner = (Spinner) convertView.findViewById(R.id.simpleSpinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
getContext(),
R.array.country_arrays,
android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
/*
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
*/
/* @Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//Spinner spinner = null;
// String selState = (String) ((Spinner) parent).getSelectedItem();
spinner.setSelection(position);
String selState = (String) ( parent).getSelectedItem();
Toast.makeText(
parent.getContext(),
"Clicked on Planet: " + selState + "", Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});*/
return row;
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
spinner.setSelection(position);
String selState = spinner.getSelectedItem().toString();
System.out.println(selState);
Toast.makeText(
getContext(),
"Clicked on Planet: " + selState + "", Toast.LENGTH_SHORT).show();
/* Toast.makeText(parent.getContext(),
"OnItemSelectedListener : " + parent.getItemAtPosition(position).toString(),
Toast.LENGTH_SHORT).show();*/
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
/* @Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String selState = (String) spinner.getSelectedItem();
System.out.println(selState);
Toast.makeText(
getContext(),
"Clicked on Planet: " + selState + "", Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}*/
}
最佳答案
如果你想在 ListView
的每一行中重复 Spinner
,你应该把它放在 single_listview_item.xml
中,而不是 >fragment_list2.xml
。然后你通常会在你的Fragment
中膨胀fragment_list2.xml
,寻找ListView
(findViewById
),实例化Adapter
并将其设置到 ListView
(setAdapter
) 中。 Adapter
负责加载 single_listview_item.xml
(以及 Spinner
)并设置自己的 Adapter
。您不应该在 Fragment
中寻找微调器,因为一旦加载它,您就可以拥有它的多个实例(每行一个)。
因此,在 Fragment
的 onCreateView
中:
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_list2, container, false);
ListView listView = (ListView) rootView.findViewById(R.id.listview);
ListAdapter listAdapter = new MyListAdapter();
listView.setAdapter(listAdapter);
然后,在 MyListAdapter
的 getView
方法中:
Spinner spinner = (Spinner) convertView.findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
getContext(),
R.array.country_arrays,
android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
基本上你必须在另一个里面实例化一个Adapter
,所以你必须创建另一个类(MyListAdapter
)。
关于java - 如何将 Spinner 插入到 ListView 的每一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38550046/
我正在编写一个具有以下签名的 Java 方法。 void Logger(Method method, Object[] args); 如果一个方法(例如 ABC() )调用此方法 Logger,它应该
我是 Java 新手。 我的问题是我的 Java 程序找不到我试图用作的图像文件一个 JButton。 (目前这段代码什么也没做,因为我只是得到了想要的外观第一的)。这是我的主课 代码: packag
好的,今天我在接受采访,我已经编写 Java 代码多年了。采访中说“Java 垃圾收集是一个棘手的问题,我有几个 friend 一直在努力弄清楚。你在这方面做得怎么样?”。她是想骗我吗?还是我的一生都
我的 friend 给了我一个谜语让我解开。它是这样的: There are 100 people. Each one of them, in his turn, does the following
如果我将使用 Java 5 代码的应用程序编译成字节码,生成的 .class 文件是否能够在 Java 1.4 下运行? 如果后者可以工作并且我正在尝试在我的 Java 1.4 应用程序中使用 Jav
有关于why Java doesn't support unsigned types的问题以及一些关于处理无符号类型的问题。我做了一些搜索,似乎 Scala 也不支持无符号数据类型。限制是Java和S
我只是想知道在一个 java 版本中生成的字节码是否可以在其他 java 版本上运行 最佳答案 通常,字节码无需修改即可在 较新 版本的 Java 上运行。它不会在旧版本上运行,除非您使用特殊参数 (
我有一个关于在命令提示符下执行 java 程序的基本问题。 在某些机器上我们需要指定 -cp 。 (类路径)同时执行java程序 (test为java文件名与.class文件存在于同一目录下) jav
我已经阅读 StackOverflow 有一段时间了,现在我才鼓起勇气提出问题。我今年 20 岁,目前在我的家乡(罗马尼亚克卢日-纳波卡)就读 IT 大学。足以介绍:D。 基本上,我有一家提供簿记应用
我有 public JSONObject parseXML(String xml) { JSONObject jsonObject = XML.toJSONObject(xml); r
我已经在 Java 中实现了带有动态类型的简单解释语言。不幸的是我遇到了以下问题。测试时如下代码: def main() { def ks = Map[[1, 2]].keySet()
一直提示输入 1 到 10 的数字 - 结果应将 st、rd、th 和 nd 添加到数字中。编写一个程序,提示用户输入 1 到 10 之间的任意整数,然后以序数形式显示该整数并附加后缀。 public
我有这个 DownloadFile.java 并按预期下载该文件: import java.io.*; import java.net.URL; public class DownloadFile {
我想在 GUI 上添加延迟。我放置了 2 个 for 循环,然后重新绘制了一个标签,但这 2 个 for 循环一个接一个地执行,并且标签被重新绘制到最后一个。 我能做什么? for(int i=0;
我正在对对象 Student 的列表项进行一些测试,但是我更喜欢在 java 类对象中创建硬编码列表,然后从那里提取数据,而不是连接到数据库并在结果集中选择记录。然而,自从我这样做以来已经很长时间了,
我知道对象创建分为三个部分: 声明 实例化 初始化 classA{} classB extends classA{} classA obj = new classB(1,1); 实例化 它必须使用
我有兴趣使用 GPRS 构建车辆跟踪系统。但是,我有一些问题要问以前做过此操作的人: GPRS 是最好的技术吗?人们意识到任何问题吗? 我计划使用 Java/Java EE - 有更好的技术吗? 如果
我可以通过递归方法反转数组,例如:数组={1,2,3,4,5} 数组结果={5,4,3,2,1}但我的结果是相同的数组,我不知道为什么,请帮助我。 public class Recursion { p
有这样的标准方式吗? 包括 Java源代码-测试代码- Ant 或 Maven联合单元持续集成(可能是巡航控制)ClearCase 版本控制工具部署到应用服务器 最后我希望有一个自动构建和集成环境。
我什至不知道这是否可能,我非常怀疑它是否可能,但如果可以,您能告诉我怎么做吗?我只是想知道如何从打印机打印一些文本。 有什么想法吗? 最佳答案 这里有更简单的事情。 import javax.swin
我是一名优秀的程序员,十分优秀!