简体   繁体   中英

android ListActivity onListItemClick to open data in another activity

Hey I have tried to make a ListActivity that show the internal saved data, there have been saved from another activity. And when I click on an item, it should open a new activity with the internal data, and then open the data.

Here is the first activity. there shall show the listview and when clicked on an item, the user should be send to second activity whith more details

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);   




}

Here is the other activity there shall recived the data and open it.

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]);

}

I have trouble with sending the data between the activies and open it, so it will split up in my many differents textviews. Any ideas ? And sorry for my bad English

Try this way,hope this will help you to solve your problem.

Instead of geting product from ListView item TextView just declare your ArrayList object outside oncreate() and try to get your particular product using position from list in ListView item click listener.

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);
    }
}

You can check what you are sending to second activity, mean using System.out.println() you can check on logcat . Also i done small change in your code please try this.

  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();

}

Ans also in other activity mean second activity you can add System.out.println to check list item value is coming or not.

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

    System.out.println("product in second activity  : "+product);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM