【问题标题】:Need to CORRECT the FOR loop. Android. need for loop to work for dyamic items需要纠正 FOR 循环。安卓。需要 for 循环来处理动态项目
【发布时间】:2014-05-12 11:32:43
【问题描述】:

我想要的是获得与项目数量一样多的文本视图(列表行)。表示for循环。请帮我解决CODERS。我只得到最后一项,所以我“硬编码”了它。

公共类 AllNotes 扩展 Activity {

Button button;
TextView textview;

// trial copy code from other from net
private ListView mainListView ;
private ArrayAdapter<String> listAdapter ;

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

    String path = Environment.getExternalStorageDirectory().toString()+ "/" + FirstOneAct.foldername;
    Log.d("Files", "Path: " + path);
    File f = new File(path);        
    File file[] = f.listFiles();
    Log.d("Files", "" + file.length);
    Toast.makeText(this, "" + file.length ,Toast.LENGTH_SHORT).show();

    for (int i=0; i < file.length; i++)
    {

     // trial copy code from other from net
        // Find the ListView resource. 
        mainListView = (ListView) findViewById( R.id.mainListView );

        ArrayList<String> planetList = new ArrayList<String>();     
        // Create ArrayAdapter using the planet list.
        listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, planetList);

        listAdapter.add(file[0].getName());
        listAdapter.add(file[1].getName());
        listAdapter.add(file[2].getName());
        listAdapter.add(file[3].getName());
        listAdapter.add(file[4].getName());

     // Set the ArrayAdapter as the ListView's adapter.
        mainListView.setAdapter( listAdapter ); 

        Log.d("Files", "FileName:" + file[i].getName());
        Toast.makeText(getApplicationContext(), file[i].getName(),Toast.LENGTH_LONG).show();
    }

    /*// Set the ArrayAdapter as the ListView's adapter.
    mainListView.setAdapter( listAdapter ); */     
  }
}

【问题讨论】:

  • listAdapter.add(file[i].getName()); 有什么问题?
  • 先生,这一行只显示最后一项。这就是我硬编码最多 5 个项目的原因。
  • 这是因为您每次在循环中重新分配适配器:这一行 mainListView.setAdapter( listAdapter ); 应该在循环之后
  • 我之前剪掉了那条线并粘贴了,(现在在哪里)。但没有发生任何变化。
  • 不,它需要在之后。切割之前的位置。

标签: android for-loop arraylist


【解决方案1】:

也许问题出在 for 循环中,您总是在创建新的 Listview 和 arrayadapter

尝试跟随

String path = Environment.getExternalStorageDirectory().toString()+ "/" + FirstOneAct.foldername;
        Log.d("Files", "Path: " + path);
        File f = new File(path);        
        File file[] = f.listFiles();
        Log.d("Files", "" + file.length);
        Toast.makeText(this, "" + file.length ,Toast.LENGTH_SHORT).show();
        ListView mainListView = (ListView) findViewById( R.id.mainListView );
        ArrayList<String> planetList = new ArrayList<String>(); 


        for (int i=0; i < file.length; i++)
        {   
            planetList.add(file[i].getName());
        }
        // Create ArrayAdapter using the planet list.
        listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, planetList);
        // Set the ArrayAdapter as the ListView's adapter.
        mainListView.setAdapter( listAdapter ); 

        Log.d("Files", "FileNames:" + planetList);
        Toast.makeText(getApplicationContext(), planetList.toString(),Toast.LENGTH_LONG).show();

【讨论】:

    【解决方案2】:

    正如 Der Golem 所说,只使用 file[i] 但是您还需要在 for 循环之前声明 listAdapter ,否则它会在您每次遍历循环时重新分配值!

    这里有一个可以帮助你的快速指南:

        mainListView = (ListView) findViewById( R.id.mainListView );
    
        ArrayList<String> planetList = new ArrayList<String>();     
        // Create ArrayAdapter using the planet list.
        listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, planetList);
    
        for (int i=0; i < file.length; i++)
        {
           listAdapter.add(file[i].getName());
        }
    
     // Set the ArrayAdapter as the ListView's adapter.
        mainListView.setAdapter( listAdapter ); 
    
        Log.d("Files", "FileName:" + file[i].getName());
        Toast.makeText(getApplicationContext(), file[i].getName(),Toast.LENGTH_LONG).show();          
    

    希望这会有所帮助。

    【讨论】:

    • 谢谢。这有帮助。但无论如何,我将变量 i 更改为 j。比如: for (int j=0; j
    • 没问题,很高兴能帮上忙。
    【解决方案3】:
    Button button;
    TextView textview;
    
    // trial copy code from other from net
    private ListView mainListView ;
    private ArrayAdapter<String> listAdapter ;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.all_notes);
    
        String path = Environment.getExternalStorageDirectory().toString()+ "/" + FirstOneAct.foldername;
    
        File f = new File(path);        
        File file[] = f.listFiles();
    
        ArrayList<String> planetList = new ArrayList<String>();     
    
        for (int i=0; i < file.length; i++) {
            planetList.add(file[i].getName());
        }
    
        // Create ArrayAdapter using the planet list.
        listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, planetList);
    
        // Find the ListView resource. 
        mainListView = (ListView) findViewById( R.id.mainListView );
    
        // Set the ArrayAdapter as the ListView's adapter.
        mainListView.setAdapter( listAdapter ); 
    
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-23
      • 2011-05-27
      • 2015-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多