2012-05-04 146 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方法是做完全不同的事情。