2012-05-04 109 views
0

你好,我没有代码问题,因为我还没有任何关于如何去做的事情。我的问题是:ListView in <include>

我有我的Activity创建ListViewListAdapter,我想这Activity显示与此ListView新的布局。

为什么同样的活动?

  • 因为我想保留我的OnItemClickListener并有完整的工作列表。

为什么我想列出?

  • 因为我想为我的应用程序更好地查看UI视图。

如果我调用另一个Activity与包含ListView,好,ListView将是空的,因为没有活动获取列表内容的新布局。

+1

可以理解的是,新活动将使其中的代码与第一个活动中的代码一样填充其列表。你的问题也不是很清楚。我们很难说出你在问什么。 – FoamyGuy

回答

0

所以...你希望同一个活动在用户交互后显示不同的视图?也许ViewFlipper可能会有所帮助。

0

我认为你是错的Android的理解是如何工作的?

你可以有多个列表视图多个活动。列表视图可以全部使用相同的布局,也可以使用不同的布局。他们也可以分别拥有自己的onItemClickListener。

从我的一个项目一个例子:

public class BrowseSetups extends ListActivity { 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     mDBHelper = new DBAdapter(this); 
     mDBHelper.open(DBAdapter.MAIN_DATABASE_NAME, DBAdapter.MAIN_DATABASE_VERSION); 
     setupsCursor = mDBHelper.fetchSearchSetups(find, column); 
     this.startManagingCursor(setupsCursor); 
     String[] from = new String[] { DBAdapter.SETUP_PART }; 
     int[] to = new int[] { R.id.ListItem1 }; 
     SimpleCursorAdapter tools = new SimpleCursorAdapter(this, 
      R.layout.listlayout, setupsCursor, from, to); 
     setListAdapter(tools); 
} 
    @Override 
    protected void onListItemClick(ListView l, View v, int position, long id) { 
     setupsCursor.moveToPosition(position); 
     Intent myIntent = new Intent(BrowseSetups.this, BrowseTools.class); 
     BrowseSetups.this.startActivity(myIntent); 
    } 
} 

public class BrowseTools extends ListActivity { 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    mDBHelper = new DBAdapter(this); 
    mDBHelper.open(DBAdapter.MAIN_DATABASE_NAME, DBAdapter.MAIN_DATABASE_VERSION); 
    toolsCursor = mDBHelper.fetchAllTools(); 
    this.startManagingCursor(toolsCursor); 
    String[] from = new String[] { DBAdapter.TOOL_ROWID, 
      DBAdapter.TOOL_DESCRIPTION }; 
    int[] to = new int[] { R.id.ListItem1, R.id.ListItem2 }; 
    SimpleCursorAdapter tools = new SimpleCursorAdapter(this, 
      R.layout.listlayoutdouble, toolsCursor, from, to); 
    setListAdapter(tools); 
    } 
@Override 
protected void onListItemClick(ListView l, View v, int position, long id) { 
    toolsCursor.moveToPosition(position); 
    String Tool = "Tool #: " + id; 
    String Description = "Description: " 
      + toolsCursor.getString(toolsCursor 
        .getColumnIndex(DBAdapter.TOOL_DESCRIPTION)); 
    String Location = "Location: " 
      + toolsCursor.getString(toolsCursor 
        .getColumnIndex(DBAdapter.TOOL_LOCATION)); 
    AlertDialog locationAlert = new AlertDialog.Builder(this).create(); 
    locationAlert.setTitle("Tool Information"); 
    locationAlert.setMessage(Tool + "\n" + Description + "\n" + Location); 
    locationAlert.setButton("OK", new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int which) { 
      return; 
     } 
    }); 
    locationAlert.show(); 
} 

所以这是两种不同的行为,用两种不同的列表,使用两种不同的列表视图,每个都有自己的onListItemClick方法是做完全不同的事情。