我正在实现一个列表,该列表需要由活动显示并由用户修改(添加项目,删除项目,排序)。目前我有两个不同的类 - 活动类和列表类,列表中包含所有操作。但是,活动类需要访问列表才能适应和显示它,并且复制活动列表或将列表公开似乎有点笨拙。我觉得我可能不理解什么活动是正确的 - 我的两个班级真的应该是一个单独的班级吗?我假设活动主要是用于UI,而不是用于修改基础数据结构。
答案 0 :(得分:3)
活动是Android的主要构建块,您在应用程序中看到的所有不同屏幕都是Activities。您应该了解并非Android应用中使用的所有Java Classes
都是活动(只有那些extends Activity
是活动的Java类。)
非活动的Java类(在您的代码中使用)可以是简单的Data Models
,Custom Adapters
,Custom Views
,Database Handlers
,Services
,等等。
所有这些Java文件都与Activity Class分开使用,以提供Modularity并避免通过在单个活动类中实现所有功能来创建混乱和缺陷。
您可以在Activity Class中使用这些其他Java类的实例(或静态使用它们)。
对于显示简单列表,您可以使用ListView
窗口小部件,而您实际上并不需要单独的类。同样,如果您准备实现具有删除,添加,更新等功能的ListView,则可以使用Custom ListView作为替代选项。
您无法在单个活动类中实施自定义列表视图,您需要Custom Adapter Class
,Custom Data Model Class
以及其他相关类来实现它。
以下列出了一些实现简单和自定义ListView的有用教程:
答案 1 :(得分:2)
I'm implementing a list which needs to be displayed by an activity and modified (add items, remove items, sort) by the user.
您可以执行List<T>
接口定义的简单操作,如添加,删除等。您可以编写自定义Comparator<T>
来执行排序操作。然后使用Collections.sort()
方法完成您的工作。
Currently I have two different classes - the activity class and the list class which has all the operations on the list.
这取决于。我通常更喜欢创建列表的单例实例,让我的活动在ListView
的回调中修改它。让你的Activity处理列表中的添加或删除没有什么笨拙
However, the activity class is going to need access to the list in order to adapt and display it, and it seems kind of clumsy to either duplicate the list for the activity or make the list public.
就像我说的那样,查看 Singleton Instance 是什么。通过创建包含列表的类来跨多个活动共享列表。将列表公开。这样,您共享列表,不要复制它。请记住:如果您多次复制数据。让它们保持同步将是一个难以破解的难题。
答案 2 :(得分:2)
这样想。你的oncreate被调用一次。如果你需要在列表上完成的事情,他们很可能会在你的onItemClick,onItemLongClick有点事件。当发生这种情况时,您应该调用在同一活动中编码的AsyncTask,以便onPostExecute可以修改其UI元素和列表。下面是一些例子。
注意,下面的代码已经减少了很长时间,所以借口语法
package com.taxeetaregistration;
public class Bookings extends ListActivity implements OnClickListener, OnItemClickListener {
private static LayoutInflater inflater = null;
private LinkedList<BookingRecord> bookingRecord;
private ListView customerList;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bookings);
Log.d("Taxeeta", "Entered BookingExperience");
bookingRecord = new LinkedList<BookingRecord>();
customerList = (ListView) findViewById(android.R.id.list);
customerList.setAdapter(new CustomerList());
customerList.setOnItemClickListener(this);
getBookings = new GetBookings();
getBookings.execute();
}
public class CustomerList extends BaseAdapter implements OnClickListener {
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null || convertView.getTag() == null) {
convertView = inflater.inflate(R.layout.bookingresponse_row, null);
final CustomerViewHolder viewHolder = new CustomerViewHolder();
viewHolder.customerRow = (LinearLayout) convertView.findViewById(R.id.customerRow);
viewHolder.customerName = (TextView) convertView.findViewById(R.id.customerName);
viewHolder.customerPhoneNumber = (TextView) convertView
.findViewById(R.id.customerPhoneNumber);
convertView.setTag(viewHolder);
}
// Setting all values in listview
String temp = bookingRecord.get(position).customer.getFullName();
((CustomerViewHolder) (convertView.getTag())).customerName.setText(temp);
temp = bookingRecord.get(position).customer.getPhoneNumber();
((CustomerViewHolder) (convertView.getTag())).customerPhoneNumber.setText(temp);
return convertView;
}
public class GetBookings extends AsyncTask<Object, Integer, Object> {
@Override
protected Object doInBackground(Object... params) {
connectToServer();
//Do all network related work here, and update
publishProgress(j);
}
}
return null;
}
@Override
public void onPostExecute(Object result) {
super.onPostExecute(result);
if (bookingRecord != null && bookingRecord.size() > 0) {
busy.setVisibility(View.GONE);
((BaseAdapter) customerList.getAdapter()).notifyDataSetChanged();
} else {
progressBarUpper.setVisibility(View.GONE);
Log.d("Taxeeta", "No cabbies found");
}
}
@Override
protected void onProgressUpdate(Integer... i) {
super.onProgressUpdate(i);
boolean found = false;
customersFound.setText("" + totalCabbiesSubscribed);
BookingRecord newRecord = new BookingRecord();
newRecord.customerJourney = customerJourney;
newRecord.customer = customer;
bookingRecord.addLast(newRecord);
customersConfirmed.setText("" + bookingRecord.size());
}
}
private class CustomerViewHolder {
public LinearLayout customerRow;
public TextView customerName;
public TextView customerPhoneNumber;
public TextView customerFrom, customerTo;
public ListView cabbieList;
public float distanceFromCustomer = -1.0f;
}
public class BookingRecord {
public BookingRecord() {
cabbies = new ArrayList<CabbieDetails>();
}
public IJourneyDetails customerJourney;
public IUserDetails customer;
public SearchResultsConcrete cabbieList;
public ArrayList<CabbieDetails> cabbies;
}
}