2016-07-21 23 views
-2

我需要一個示例程序,其中具有適配器的自定義列表視圖,當我單擊列表視圖上的項目時,必須選擇它並將其應該在其他活動中可見。這應該沒有意圖完成。如何將自定義列表視圖從一個活動傳遞到Android中的另一個活動,而無需使用意圖

+0

你可以使用SharedPreferences或SQLite來保存你選擇的項目。 – babadaba

+1

當你說一個項目在列表視圖中被點擊,然後應該在另一個活動中顯示時,你的意思是整個視圖改變了嗎?列表視圖作爲主要活動的一部分顯示在不同的活動或片段中嗎?我需要進一步澄清。有許多方法可以在不使用「意圖」的情況下實現此目標,但取決於轉換髮生的位置和方式, –

回答

0

讓你的ArrayList中,如下一個活動全球:

public class ActivityOne extends Activity{ 

    public static ListView list; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.YOUR_LAYOUT); 

     list=(ListView)findViewById(R.id.YOUR_LIST_ID); 


    .................. 
    .................. 
    } 
} 

而在其他活動如下取,

public class ActivityTwo extends Activity{ 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

     ListView list=ActivityOne.list.getListView(); 


     .................. 
     .................. 
    } 
} 
0

如果你不想使用untent擴展應用程序類,使財產,持有你在列表視圖中選擇的對象

//pseudocode 
//Application class 
class GlobalApp extends Application { 

//you need to overirde this method 
public void oncreate(parameter) { 
super.onCreate(parameter); 
} 
public MyClass myObject; // replace MyClass with datatype need 
} 

// In activity where you are handling listview make class variable of type GlobalApp 

GloabalApp app = (GlobalApp) getApplication(); 
//in your item selected method of your list view 

app.myobject = selectedValue 

// In another activity on activity method 
GloabalApp app = (GlobalApp) getApplication(); 
Myclass Selectedobj = app.myobject 

在清單文件在你的應用程序標籤中添加值爲「yourpackagename.GlobalApp」的android:name屬性

相關問題