2013-09-22 71 views
2

我正在实现一个列表,该列表需要由活动显示并且由用户修改(添加项目,移除项目,排序)。目前我有两个不同的类 - 活动类和列表类,其中包含列表中的所有操作。但是,活动类将需要访问列表以适应和显示它,并且复制活动列表或公开列表似乎很笨拙。我觉得我可能不了解哪些活动是正确的 - 我的两个班级是否应该是一个班级?我认为活动主要是针对UI,而不是修改底层数据结构。关于活动和类的困惑

+2

我不认为任何人都可以没有你正在尝试做的:) – davidkonrad

+1

@davidkonrad检查你的代码的实例或回答我的答案。看看是否需要改进。 :) –

+1

+1为改善您的问题:) – davidkonrad

回答

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>接口象add定义的简单操作,删除等,您可以编写自定义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实例是。通过创建一个包含列表的类来在多个活动中共享您的列表。公开声明列表。这样,你就可以分享清单,不会重复。请记住:如果您多次复制数据。将它们保持同步将是一个难题。

3

活动是Android的主要构建模块,您在应用程序中看到的所有不同屏幕都是Activities。您应该明白,并非您的Android应用中使用的所有Java Classes都是活动(仅限那些extends Activity为活动的Java类)。

Java类不在活动(在你的代码中使用)可以简单朴素Data ModelsCustom AdaptersCustom ViewsDatabase HandlersServices,等等。

所有这些Java文件都与Activity类分开使用,以提供模块性,并避免通过在单个Activity类中实现所有功能来创建Mess和缺陷。

您可以在Activity类中使用这些其他Java类的实例(或静态使用它们)。

对于显示简单列表,您可以使用ListView小部件,并且实际上并不需要单独的类。同样,如果您准备使用删除,添加,更新等功能来实现ListView,那么Custom ListView是您可以使用的替代选项。

你不能在一个Activity类中实现一个自定义列表视图,你需要一个Custom Adapter Class,一个Custom Data Model Class和其他相关的类来实现它。

这里是实现简单和自定义列表视图一些有用的教程列表:

  1. Vogella's ListView Tut
  2. The Open Tutorials
  3. Android Example 1
  4. Android Example 2

我希望这有助于。

2

想想那样。你的创建者被调用一次。如果你需要列表上的事情,他们很可能会在你的onItemClick,onItemLongClick类事件。当发生这种情况时,您应该调用一个AsyncTask,并在同一活动中进行编码,以便onPostExecute可以修改其UI元素和列表。下面的一些例子。

注意,下面的代码已经降低BIGTIME,所以借口语法

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; 
    } 

}