2014-02-25 33 views
0

這是ListView的如何從一個發送的ArrayList <HashMap中<字符串,對象>>的值到另一個活動

<LinearLayout 
    … > 
    <ListView 
     android:id="@+id/listView1" > 
    </ListView> 
</LinearLayout> 

和3項在сell

<TextView 
      android:id="@+id/text1" /> 
<TextView 
      android:id="@+id/text2" /> 
<Button 
      android:id="@+id/button1"/> 

這是代碼

public class Spisok extends Activity { 

    private ArrayList<HashMap<String, Object>> Irr; 
    private static final String Form1 = "form1"; // Форма1 
    private static final String Form2 = "form2"; // Форма2 
    private static final String Form3 = "form3"; // Форма3 

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

     setContentView(R.layout.activity_spisok); 
     setTitle("Irr"); 

     ListView listView = (ListView) findViewById(R.id.listView1); 

     // создаем массив списков 
     Irr = new ArrayList<HashMap<String, Object>>(); 
     HashMap<String, Object> hm; 

     hm = new HashMap<String, Object>(); 
     hm.put(Form1, "1"); 
     hm.put(Form2, "2"); 
     hm.put(Form3, "3"); 
     Irr.add(hm); 

     hm = new HashMap<String, Object>(); 
     hm.put(Form1, "1"); 
     hm.put(Form2, "2"); 
     hm.put(Form3, "3"); 
     Irr.add(hm); 

     SimpleAdapter adapter = new SimpleAdapter(this, Irr, 
       R.layout.list_item, new String[] { Form1, Form2, Form3 }, 
       new int[] { R.id.text1, R.id.text2, R.id.text3 }); 
     listView.setAdapter(adapter); 

    listView.setOnItemClickListener(new OnItemClickListener() { 

    @Override 
    public void onItemClick(AdapterView<> arg0, View arg1, int arg2, 
      long arg3) { 
    Intent intent = new Intent(this, New.class); 
    intent.putExtra("arraylist", Irr); 
    startActivity 
    } 
}); 

這:

ArrayList<HashMap<String, String>> arl = (ArrayList<HashMap<String, String>>)  getIntent().getSerializableExtra("arraylist"); 

    TextView Info = (TextView)findViewById(R.id.text9); 
    Info.setText("view" +Irr); } 

但它是作爲工作的錯誤。當我使用它,我在新的活動所有的ArrayList

intent.putExtra("arraylist", Irr); 

我需要的只有 「hm.put(Form3, 」3「);」 (正確地只有「3」值)在我的NewActivity。 幫助我正確組成它。

+0

你的問題是什麼?將數據從一個「活動」轉移到另一個?或者創建一個新的「活動」? – owe

+0

「將數據從一個活動傳輸到另一個」這是問題。新活動必須按照單元格中的「文本3」具有「文本3」。 – Ibir

+0

「有問題」太模糊了。你必須描述你的問題或顯示你的錯誤。這裏是一個描述如何問:http://stackoverflow.com/questions/how-to-ask。 – owe

回答

0

你的問題是什麼?目前尚不清楚,只是我認爲你在Spisok活動中有一個Button。所以你需要在你的noCreate()方法來創建一個Button對象像

Button button1 = (Button)findViewById(R.id.button1); 
    button1.setOnClickListener(new onClickListener() { 
    @Override 
    public void onClick(){ 
    Intent intent = new Intent (this, SecondActivity.class); 
    startActivity(intent); 
    } 
} 

SecondActivity.class是你的新活動。您需要使用IDE手動創建此Activity類,並且還需要在Manefist文件中添加此活動。你可以把你的TextView text3 SecondActivity的佈局文件。

+0

是的,但我應該怎麼做我的新活動由按照在單元格按下的botton組成的「text3」組成。根據我的回答 – Ibir

+0

。現在如果你按下button1然後打開第二個活動。第二個活動已經有一個文本視圖(text3)。現在你想在text1中顯示任何信息嗎?當用戶按下button1時?請清除它 –

+0

list:text1 text2 botton1/text(1)3 text(2)4 botton1(2)/ text(1)5 text(2)6 botton(1)3 /如果我按botton1應該打開新的活動「text2」,如果我按botton(1)3 - >打開「文本(2)6」 – Ibir

0

您必須將您的R.id.text3的文本與方法putExtra(String key, String dataToTransfer)一起放入您的意圖中。

OnItemClickListener看起來是這樣的:

listView.setOnItemClickListener(new OnItemClickListener() { 

@Override 
public void onItemClick(AdapterView<> arg0, View arg1, int position, 
     long arg3) { 
    Intent intent = new Intent (this, SecondActivity.class); 
    intent.putExtra("key for my item 3", "put the text you want to transfer here") 
    startActivity(intent); 

} 
}); 

,並在新的活動,你得到這樣的傳輸的數據:

@Override 
public void onCreate(Bundle savedInstanceState){ 
    Intent intent = getIntent(); 
    String transferedText = intent.getStringExtra("key for my item 3"); 
} 
+0

請不要將代碼放入註釋中!用評論更新您的問題。 – owe

0

您有一個自定義的ListView(不coustom列表視圖,你不能管理單行中有更多項目),每行都有兩個文本視圖和一個按鈕。你想,如果用戶單擊該按鈕,然後打開新的活動,顯示第二的TextView

在您的適配器類的getView()方法編寫代碼

@Override 
public View getView(final int position, View convertView, ViewGroup parent) 
{ 
View row = convertView; 
Button button1 = (Button) convertView.findViewById(R.id.button1); 
TextView text1 = (TextView)convertView.findViewById(R.id.textview1); 
TextView text2 = (TextView)convertView.findViewById(R.id.textview2); 

button1.setOnClickListener(new OnClickListener() 
{ 
deleteButton.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 
       Intent intent = new Intent (this, SecondActivity.class); 
       String text = text2.getText().toString(); 
       intent.putExtra("name", "text");     
       startActivity(intent); 
      } 
     }); 

} 

和SecondActivity編寫代碼

@Override 
protected void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.intent); 
    Textv = (TextView)findViewById(R.id.tv2); 
    Intent iin= getIntent(); 
    Bundle b = iin.getExtras(); 

    if(b!=null) 
    { 
     String j =(String) b.get("name"); 
     Textv.setText(j); 
    } 
} 

希望這會幫助你

+0

對不起,我忙於其他任務。 –

相關問題