2014-10-11 44 views
0

嘿我試圖讓一個ListActivity顯示內部保存的數據,已經從另一個活動保存。當我點擊一個項目時,它應該用內部數據打開一個新的活動,然後打開數據。android ListActivity onListItemClick在另一個活動中打開數據

這是第一項活動。有應顯示列表視圖,並在項目上點擊後,用戶應發送到第二活動蒙山詳情

public class ShowListActivity extends ListActivity { 

private ArrayAdapter<String> list; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_show_data); 
    getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE); 
    getListView().setSelector(android.R.color.holo_blue_bright); 
    String[] filenames = getApplicationContext().fileList(); 
    List<String> list = new ArrayList<String>(); 
    for(int i = 0; i<filenames.length; i++){ 
     //Log.d("Filename", filenames[i]); 
     list.add(filenames[i]); 
    } 

    setListAdapter(new ArrayAdapter(this, 
       android.R.layout.simple_list_item_1, list)); 



} 
protected void onListItemClick(ListView l, View v, int position, long id) { 
    super.onListItemClick(l, v, position, id); 

    String product = ((TextView)v).getText().toString(); 

    Intent i = new Intent(getApplicationContext(), MainActivity.class); 
    i.putExtra("product", product); 
    startActivity(i); 




} 

下面是其他活動,須recived數據並打開它。

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    ActionBar actionBar = getActionBar(); 
    actionBar.setDisplayHomeAsUpEnabled(true); 
    actionBar.setDisplayShowTitleEnabled(true); 

    TextView showCloseRange = (TextView) findViewById(R.id.show_close_range); 
    TextView showToRight = (TextView) findViewById(R.id.show_to_right); 
    TextView showToCenterRight = (TextView) findViewById(R.id.show_to_center_right); 
    TextView showToCenter = (TextView) findViewById(R.id.show_to_center); 
    TextView showToCenterLeft = (TextView) findViewById(R.id.show_to_center_left); 
    TextView showToLeft = (TextView) findViewById(R.id.Show_to_left); 
    TextView showFT = (TextView) findViewById(R.id.show_ft); 
    TextView showTreRight = (TextView) findViewById(R.id.show_tre_right); 
    TextView showTreCenterRight = (TextView) findViewById(R.id.show_tre_center_right); 
    TextView showTreCenter = (TextView) findViewById(R.id.show_tre_center); 
    TextView showTreCenterLeft = (TextView) findViewById(R.id.show_tre_center_left); 
    TextView showTreLeft = (TextView) findViewById(R.id.show_tre_left); 
    TextView showAssist = (TextView) findViewById(R.id.show_assist); 
    TextView showSteals = (TextView) findViewById(R.id.show_steals); 
    TextView showFouls= (TextView) findViewById(R.id.show_fouls); 
    TextView showTotalPt = (TextView) findViewById(R.id.show_total_pt); 
    TextView showDataBlocks = (TextView) findViewById(R.id.show_data_blocks); 
    TextView showDataRebounds = (TextView) findViewById(R.id.show_data_rebounds); 
    TextView showDataTurnOcers = (TextView) findViewById(R.id.show_data_turn_overs); 
    TextView showDataPlayerTime = (TextView) findViewById(R.id.show_data_player_time); 
    TextView showNote = (TextView) findViewById(R.id.show_note); 
    String value = ""; 
    FileInputStream fis; 

    Intent i = getIntent(); 
    String product = i.getStringExtra("product"); 
    ActionBar actionBar1 = getActionBar(); 
    actionBar1.setTitle(product); 

    try { 
     fis = openFileInput(product); 

     byte[] input = new byte[fis.available()]; 
     while(fis.read(input) != -1){ 

      value += new String(input); 
     fis.close(); } 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    String[] strArray = value.split(";"); 
    showCloseRange.setText(strArray[0]); 
    showToRight.setText(strArray[1]); 
    showToCenterRight.setText(strArray[2]); 
    showToCenter.setText(strArray[3]); 
    showToCenterLeft.setText(strArray[4]); 
    showToLeft.setText(strArray[5]); 
    showFT.setText(strArray[6]); 
    showTreRight.setText(strArray[7]); 
    showTreCenterRight.setText(strArray[8]); 
    showTreCenter.setText(strArray[9]); 
    showTreCenterLeft.setText(strArray[10]); 
    showTreLeft.setText(strArray[11]); 
    showDataPlayerTime.setText(strArray[12]); 
    showTotalPt.setText(strArray[13]); 
    showAssist.setText(strArray[14]); 
    showSteals.setText(strArray[15]); 
    showDataBlocks.setText(strArray[16]); 
    showDataRebounds.setText(strArray[17]); 
    showDataTurnOcers.setText(strArray[18]); 
    showFouls.setText(strArray[19]); 
    showNote.setText(strArray[20]); 

} 

我與活動比較之間發送數據並打開它的麻煩,所以它會在我的許多型動物textviews分手了。 有什麼建議嗎? 對不起,我的英文不好

+0

我不明白你在最後想達到什麼 – Panther 2014-10-11 10:56:54

回答

0

試試這樣,希望這會幫助你解決你的問題。

而不是從ListView項目創建產品TextView只是在oncreate()之外聲明ArrayList對象,並嘗試使用ListView項目的列表中的位置來獲取您的特定產品單擊偵聽器。

public class ShowListActivity extends ListActivity { 

    private List<String> list = new ArrayList<String>(); 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_show_data); 
     getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE); 
     getListView().setSelector(android.R.color.holo_blue_bright); 

     list = Arrays.asList(getApplicationContext().fileList()); 

     setListAdapter(new ArrayAdapter(this,android.R.layout.simple_list_item_1, list)); 
    } 

    protected void onListItemClick(ListView l, View v, int position, long id) { 
     super.onListItemClick(l, v, position, id); 
     Intent i = new Intent(this, MainActivity.class); 
     i.putExtra("product", list.get(position)); 
     startActivity(i); 
    } 
} 
0

您可以檢查您要發送到第二個活動是什麼,意味着使用的System.out.println()您可以檢查logcat的。我也做了你的代碼中的小改動,請試試這個。

private ArrayAdapter<String> list; 

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_show_data); 
    getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE); 
    getListView().setSelector(android.R.color.holo_blue_bright); 
    String[] filenames = getApplicationContext().fileList(); 
    ArrayList<String> arrlist = new ArrayList<String>(); 
    for(int i = 0; i<filenames.length; i++){ 
    //Log.d("Filename", filenames[i]); 
    arrlist.add(filenames[i]); 
    } 

    setListAdapter(new ArrayAdapter(this, 
      android.R.layout.simple_list_item_1, arrlist)); 



} 
    protected void onListItemClick(ListView l, View v, int position, long id) { 
    super.onListItemClick(l, v, position, id); 

    String product = arrlist.get(position); 

    //Here you can check what yo are sending to second activity using System.out.println 

    System.out.println("click product is : "+product); 

    Intent i = new Intent(getApplicationContext(), MainActivity.class); 
    i.putExtra("product", product); 
    startActivity(i);  
    finish(); 

} 

答也在其他活動意味着第二個活動,你可以添加的System.out.println檢查列表項的值來不來。

String product = getIntent().getStringExtra("product"); 

    System.out.println("product in second activity : "+product); 
相關問題