从android中的其他类动态地将对象添加到listview
Posted
技术标签:
【中文标题】从android中的其他类动态地将对象添加到listview【英文标题】:Adding objects to listview dynamically from other class in android 【发布时间】:2015-08-05 13:41:46 【问题描述】:我的项目包括一个“接受/拒绝传入数据”窗口,在“接受”上应将传入数据动态添加到列表视图和列表视图从中获取数据的数据库。我的 listadapter 是一个内部类。
我在主类中的代码与列表视图的填充有关:
public class MainActivity extends Activity
private List<Assignment> allDeliveries= new ArrayList<Assignment>();
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
populateDeliveryList();
populateListView();
registerClickCallBack();
final mysqliteHelper db = new MySQLiteHelper(this);
accept.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
RelativeLayout newdeliverylayout= (RelativeLayout) findViewById(R.id.newdeliverylayout);
allDeliveries.add(incomingAssignment);
db.addAssignment(incomingAssignment);
MyListAdapter adapter = new MyListAdapter();
adapter.notifyDataSetChanged();
);
deny.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
RelativeLayout newdeliverylayout= (RelativeLayout) findViewById(R.id.newdeliverylayout);
newdeliverylayout.setVisibility(RelativeLayout.GONE);
);
private void populateDeliveryList()
MySQLiteHelper db = new MySQLiteHelper(this);
List<Assignment> list = db.getAllAssignments();
for (int i = 0; i < list.size();i++)
allDeliveries.add(list.get(i));
private void populateListView()
ArrayAdapter<Assignment> adapter = new MyListAdapter();
ListView list = (ListView) findViewById(R.id.deliveriesListView);
list.setAdapter(adapter);
这是我的内部类,listadapter:
private class MyListAdapter extends ArrayAdapter<Assignment>
public MyListAdapter()
super(MainActivity.this, R.layout.item_view, allDeliveries);
@Override
public View getView(int position, View convertView, ViewGroup parent)
View itemView = convertView;
if (itemView == null)
itemView = getLayoutInflater().inflate(R.layout.item_view, parent, false);
Assignment nyDelivery = allDeliveries.get(position);
TextView adressText = (TextView) itemView.findViewById(R.id.item_adressView);
adressText.setText(nyDelivery.getAdress());
TextView zipText = (TextView) itemView.findViewById(R.id.item_zipcodePlaceView);
zipText.setText(nyDelivery.getZipcode());
TextView companyText = (TextView) itemView.findViewById(R.id.item_companyView);
companyText.setText(nyDelivery.getSenderreceiver());
TextView typeText = (TextView) itemView.findViewById(R.id.item_driveTypeView);
typeText.setText(nyDelivery.getType() + nyDelivery.getID());
return itemView;
“accept”中的代码似乎除了将传入的赋值添加到数据库之外什么都不做。
【问题讨论】:
您似乎在 populateListView 调用中的 onCreate 方法上创建了两次 listAdapter,然后当用户单击 onClick 方法中的接受按钮时。但我仍然不清楚到底是什么问题。 我尝试调用adapter.notifyDataSetChanged(),但是程序没有运行,它找不到符号适配器。我想我必须创建一个新的类实例来“重建”itemview,但我不确定它是如何工作的,我对 android 相当陌生 如果在 populateListView 方法中本地定义它是找不到的,您需要在您的 ArrayAdapter 适配器中将其定义为您的类中的全局变量。 如果不清楚,请告诉我,我会将我建议的更改发布给您的 MainActivity 类的答案 如果您不介意编写更改,我很乐意看到它们。我有更多的事情要做这个项目,如果我编程不切实际,我很想知道 【参考方案1】:这是一个简单的变化,在你的 MainActivity 中声明 MyListAdapter 并在 populateListView 中实例化一次,
public class MainActivity extends Activity
private List<Assignment> allDeliveries= new ArrayList<Assignment>();
MyListAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
populateDeliveryList();
populateListView();
registerClickCallBack();
final MySQLiteHelper db = new MySQLiteHelper(this);
accept.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
RelativeLayout newdeliverylayout= (RelativeLayout) findViewById(R.id.newdeliverylayout);
allDeliveries.add(incomingAssignment);
db.addAssignment(incomingAssignment);
adapter.notifyDataSetChanged();
);
deny.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
RelativeLayout newdeliverylayout= (RelativeLayout) findViewById(R.id.newdeliverylayout);
newdeliverylayout.setVisibility(RelativeLayout.GONE);
);
private void populateDeliveryList()
MySQLiteHelper db = new MySQLiteHelper(this);
List<Assignment> list = db.getAllAssignments();
for (int i = 0; i < list.size();i++)
allDeliveries.add(list.get(i));
private void populateListView()
adapter = new MyListAdapter();
ListView list = (ListView) findViewById(R.id.deliveriesListView);
list.setAdapter(adapter);
【讨论】:
以上是关于从android中的其他类动态地将对象添加到listview的主要内容,如果未能解决你的问题,请参考以下文章
JavaFX 动态地将标签添加到窗格,在 fxml 文件中定义