我已经创建了3个编辑文本和一个按钮,我将所有3个编辑文本保存在数组列表中,并将其显示在列表中:但问题是我想
1. first they are showing in vertically manner i want it in horizontal manner so that if i add more items it should be separated from the previous one .
2. I want to add as many as list i want to add please provide me a hint either i should use for loop or what to get as many list as i want.
i was thinking to try for loop to get as many input to be added in listview.用于java的Oncreate的主代码
公共类secondActivity扩展了AppCompatActivity {
EditText edit1,edit2,edit3;
Button button;
ListView listView;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
edit1=findViewById(R.id.name);
edit2=findViewById(R.id.desigination);
edit3=findViewById(R.id.post);
listView = findViewById(R.id.list_view);
button = findViewById(R.id.list_button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ArrayList<String> arryList = new ArrayList<String>();
arryList.add(edit1.getText().toString());
arryList.add(edit2.getText().toString());
arryList.add(edit3.getText().toString());
ArrayAdapter list = new ArrayAdapter(secondActivity.this,android.R.layout.simple_list_item_1,arryList);
listView.setAdapter(list);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(secondActivity.this, "hello "+ position, LENGTH_SHORT).show();
}
});
}
});}
没有错误开始只是它在垂直我的列表播种,我想它在同一行中,并希望添加尽可能多的项目在列表中的同时它不工作,只有我能够添加它为一次
发布于 2019-10-23 15:36:54
可以使用HorizontalListView属性或将orientation属性添加到ListView或RecyclerView,而不是ArrayList。
发布于 2022-01-04 08:03:18
您可以使用此代码模型随时将项目添加到列表视图中:
public class MyListView extends ListActivity {
ArrayList<String> listItems = new ArrayList<String>();
ArrayAdapter<String> adapter;
int myCounter = 0;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
listItems);
setListAdapter(adapter);
}
//METHOD WHICH WILL HANDLE DYNAMIC INSERTION
public void addItemsToList(View v) {
listItems.add("Clicked : " + MyCounter++);
adapter.notifyDataSetChanged();
}}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="@+id/addBtn"
android:text="Add New Item"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="addItemsToList"/>
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="false"
/>
</LinearLayout>您还可以使用HorizontalListView将项目水平添加到列表中
https://stackoverflow.com/questions/58517640
复制相似问题