2012-11-24 128 views
1

需要將活動數據傳遞給android應用程序的listactivity或listview。即時通訊數據傳遞給列表視圖時遇到問題。將數據從活動傳遞到列表活動或列表視圖

什麼程序做的是從addact類,用戶可以輸入要做的事情 並在viewact類,這將顯示活動比較ListView中

public class addact extends Activity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.newact); 
     Button btn1 = (Button)findViewById(R.id.btnsave); 
     final EditText et1 = (EditText)findViewById(R.id.etactivity); 
     btn1.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 

      Intent it = new Intent(addact.this, viewact.class); 
      it.putExtra("thekey", et1.getText().toString()); 
      startActivity(it); 
     } 
    }); 
    } 
} 



public class viewact extends ListActivity { 

    String addToDo =getIntent().getExtras().getString("thekey"); 
    String[] toDoAct = new String[] {addToDo }; 

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



     setListAdapter(new ArrayAdapter<String>(this, R.layout.viewact,toDoAct)); 

     ListView listView = getListView(); 

     listView.setTextFilterEnabled(true); 

     listView.setOnItemClickListener(new OnItemClickListener() { 
      public void onItemClick(AdapterView<?> parent, View view, 
        int position, long id) { 

       for (int i=0; i < 2; i++) 
       { 
        Toast.makeText(getApplicationContext(), 
          ((TextView) view).getText(), Toast.LENGTH_LONG).show(); 
       } 

      } 
     }); 

    } 

} 

回答

0

您不能訪問你的意圖,直到裏面添加用戶onCreate(),並且您應該執行一些檢查以確保您的應用程序不會在數據不存在時崩潰:

String addToDo = ""; 
String[] toDoAct; 

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

    Intent intent = getIntent(); 
    if(intent != null) { 
     addToDo = intent.getStringExtra("thekey", ""); 
    } 
    toDoAct = new String[] { addToDo }; 
相關問題