我在for循环中以编程方式添加TextViews并将它们添加到ArrayList.
我该怎么用TextView.setId(int id)?我提出了什么整数ID,所以它不与其他ID冲突?
正如我在之前提出的问题中所看到的,在自定义适配器类(例如,MyAdapter扩展ArrayAdapter)中,它们总是使用膨胀的xml列表项布局.我希望做的是完全使用Java创建所有内容,而不是使用XML ...
// for example
String[] wordlist = new String[] {a, b, c};
LinearLayout list_item_layout = new LinearLayout(this);
list_item_layout.setId(5000);
TextView listText = new TextView(this);
listText.setId(5001);
listLayout.addView(listText);
ListView list = new ListView(this);
// ** QUESTION ** do I declare a programmatic .setAdapter() like this?
// ** TAKE NOTE ** I passed 'wordlist' here..
list.setAdapter(new MyAdapter(this, list_item_layout.getId(), listText.getId(), wordlist));
Run Code Online (Sandbox Code Playgroud)
然后为MyAdapter ......
private class MyAdapter extends ArrayAdapter<String> {
public MyAdapter(Context context, int resource, int textViewResourceId, String[] strings) {
super(context, resource, textViewResourceId, strings);
} …Run Code Online (Sandbox Code Playgroud) 我创建了包含TextView和EditText调用的复合自定义视图LabledEditText,因为我将在一个片段中有很多 EditText 字段。
我创建了一个包含以下内容的 XML 文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayoutLabeledEditText"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/label_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/label"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/value_editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:freezesText="true"
android:saveEnabled="true"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
在 View 类中如下
public class LabeledEditText extends LinearLayout {
private EditText editText;
private TextView label;
public LabeledEditText(Context context, AttributeSet attrs) {
super(context, attrs);
inflate(context,R.layout.labeled_edit_text, this);
label = (TextView) this.findViewById(R.id.label_textView);
label.setText("some label");
editText = (EditText) this.findViewById(R.id.value_editText);
}
protected void onRestoreInstanceState(Parcelable state) {
String id= this.getId()+" ";
if (state instanceof …Run Code Online (Sandbox Code Playgroud) 我想用重置按钮创建一个唯一的ID。我上课ViewId。ViewId类包含AtomicInteger。AtomicInteger成功创建唯一ID。但是我想使用一个按钮重设唯一ID。因此,请帮助我重设并重新创建唯一ID。
ViewId 类:
import java.util.concurrent.atomic.AtomicInteger;
public class ViewId {
private static ViewId INSTANCE = new ViewId();
private AtomicInteger seq;
private ViewId() {
seq = new AtomicInteger(0);
}
public int getUniqueId() {
return seq.incrementAndGet();
}
public static ViewId getInstance() {
return INSTANCE;
}
}
Run Code Online (Sandbox Code Playgroud)
MainActivity 类:
public class MainActivity extends AppCompatActivity {
EditText editValue;
ViewId viewId = ViewId.getInstance();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editValue = (EditText)findViewById(R.id.editText);
}
// Increment Button …Run Code Online (Sandbox Code Playgroud)